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
#Hey having small doubt in inheritance(multi-level Inheritence)
12 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @wild hawk! Please use
/closeor theClose Postbutton 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.
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?
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 ?
I'm sorry, but you are calling them in the main. Seriously, what do you mean?
idk whar am speaking lol i have exams tomorrow and am having these doubts now
can i acces the another class in the main?
Normally sure. Stuff tend to be useless when they can't be used by anything else. But everything depends on details
idk sorry lol having these wierd doubts idk am half fked coz amm giving the final exam in java
tq anyway
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.