• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance Error  RSS feed

 
char Something
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Error:




My code seems to be receiving an error when I try to run it. If someone could point out the error, that would be great. Thanks!

 
Adarsh Bhat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because Class D is in no way related to Class B.  Hence class D object cannot access the method of class B.  This follows the inheritance structure of Java. Each of classes B, C and D are individual children of A and have no connection between them.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
point out the error,

The classes  D  (and A by extension) do not have the methods that the code is trying to access.

Look at the classes that have been defined and see where these methods are defined:
Elephant()
Shark()

To call a method, you need an instance of the class that contains the method.

Note: Java naming conventions say method names should begin with a lowercase letter.
 
char Something
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you @NormRadder and @AdarshBhat . I am new to java and think I can understand what you are saying. My teacher gave this code as an example but I could not get it to run. Is there a difference between his and mine? https://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/practice_activity_images/JV_TN.4.5.JPG
 
char Something
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what do I need to fix it? I can't really learn how to fix it if this was the first example, which was incorrect. Thanks!
 
Adarsh Bhat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char Something wrote:Thank you @NormRadder and @AdarshBhat . I am new to java and think I can understand what you are saying. My teacher gave this code as an example but I could not get it to run. Is there a difference between his and mine? https://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/practice_activity_images/JV_TN.4.5.JPG



Hi,

What your tutor has written is incorrect and will not compile.

The thing you can do to solve this is Make class B extend A,  class C extend B,  class D extend C.  Then create an object of class D. This object will contain a copy of all the methods of its ancestors.

Do some research on how inheritance works and you will understand more about what I'm telling. 

Note: pay attention to what Norm has said in his note.  Following coding conventions is very important to make your code readable.
 
char Something
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much @AdarshBhat !
 
char Something
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am realizing that you @AdarshBhat want me to use Multi-level inheritance. However, my teacher is trying to teach us how to use Hierarchical inheritance where the other classes extend off of class A. https://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/unit4/resources/images/JV_4.4.10.1.jpg
 
Adarsh Bhat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char Something wrote:I am realizing that you @AdarshBhat want me to use Multi-level inheritance. However, my teacher is trying to teach us how to use Hierarchical inheritance where the other classes extend off of class A. https://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/unit4/resources/images/JV_4.4.10.1.jpg


Yes your teacher's example is hierarchical inheritance, however, the calling of the methods in that example program is wrong.  Should you wish to implement that,  you would have to learn a concept of polymorphism.

Polymorphism is the ability of an object to behave differently in different situations. This is only a layman's example.  you should research more on this to get better understanding

In the example program, to achieve polymorphism such that it helps build a hierarchical inheritance structure, you can create an object of type A with the implementation of B.

For example : A object = new B() ;

This object can handle bother A and B methods. Say if both classes A and B have  the same method add() i. e.  Class B has overridden class A method.  Then if you create the object of above type then the compiler decides on run time which method should be called. In this case, since B has the method add()  it will implement B class's method.

All this might be confusing unless you understand polymorphism. So I suggest you look into more of it so you get what I say.
 
Code Black
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you write your code like this, it would also work:

A
B extends C
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char Something wrote:...Is there a difference between his and mine...


Of course the code(by your instructor) will not compile and i think the reason for it is now crystal clear to you.Though don't misunderstand your teacher she could have written such code for strengthening your concept on inheritance and polymorphism in java.
i think your instructor must have explained something about the code like what every part does (or try) in main method,which part will compile or not,if not,what is the error you will got,description of error.Didn't she?
Even if she didn't explained about it,then you are at write place .

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to CodeRanch! @Adarsh Bhat and Code Black.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!