#Hey having small doubt in inheritance(multi-level Inheritence)

12 messages · Page 1 of 1 (latest)

wild hawk
#

Basically why am here because when i try to understand this code i come up with the same doubt like why cant i just call the function in the main method itll generate the same output right why to use the inheritence like here there are three classes if i call the fucntion in the main itll generate same output then what is the use of using it. it may feel dumb doubt and sorry for my english. expecting a good explanation or redirect to the place where i can find the answer . thank you class Animal {
public void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("I can bark");
}
}
class Labrador extends Dog {
public void color() {
System.out.println("I am brown in color");
}
}
public class Main {
public static void main(String[] args) {
Labrador myLabrador = new Labrador();
myLabrador.eat();
myLabrador.bark();
myLabrador.color();
}
} /close

dire fiberBOT
#

This post has been reserved for your question.

Hey @wild hawk! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

warm sequoia
#

Hey, i'd like to help. But your question seems a little confusing could you please rephrase it.

My guess is that you're confused as to why you need to use inheritance when you could just write the print statements in main method?

wild hawk
#

ya like see in that code i have given you there is three classes right and has three fucntion bark();eat() and color();why cant i just call the funciton in the main ?

austere scaffold
#

I'm sorry, but you are calling them in the main. Seriously, what do you mean?

wild hawk
#

idk whar am speaking lol i have exams tomorrow and am having these doubts now

#

can i acces the another class in the main?

austere scaffold
#

Normally sure. Stuff tend to be useless when they can't be used by anything else. But everything depends on details

wild hawk
#

idk sorry lol having these wierd doubts idk am half fked coz amm giving the final exam in java

#

tq anyway

wet moss
#

Looks like the point of this code is to learn HOW inheritance works, not to lose your mind thinking about how you could write it different ways.