• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making sure I get Polymorphism  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my understanding of it has boiled down to the following:

For Variables:
It's 100% about the reference regardless of static vs non-static. Whatever the reference type is will determine what version of the variable is called. References types of the parent class call the parent class value for that variable. Child class ref type calls the child class value.

Methods:
Static vs Non-Static
Static Methods -
Static methods call the parent class version of the method when the reference variable type is the parent class.
Static methods call the child class version of the method when the reference variable type is the child class (which wouldn't really be polymorphism b/c both reference and object type are the child class)

Non-Static Methods -
Non-Static methods call the child class version of the method even when the reference type is of the parent class.


Can someone confirm that this is the correct understanding of Polymorphism? I made a program using Eclipse to test all this out after being fooled by some trick questions on practice exams. I can post my program if needed?
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by not calling superclasses and subclasses parent and child. Biological inheritance is different from computer inheritance, so the analogy is poor.
Fields: fields are never polymorphic. Don't give a subclass a field with the same name as a superclass field. Not unless you want no end of confusion.
Static methods: never polymorphic. You are right about which version is used. You can't override a static method, but trying so to do is another certain recipe for confusion. We have an FAQ.
Non‑private instance methods: Always polymorphic if you try to override them. Always use the @Override annotation if you think you are overriding a method. The version called is from the object, and it goes from there to the Class<XYZ> object which that object was created from. So, yes, if the subclass has an overridden version it is that version which is called. We have a story, only legible to dogs.
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . .

Thanks.

I am studying for the exam, which unfortunately is full of HORRIBLE coding and piss poor authoring. So ultimately, your tips and link will definitely help me be a better programmer, but I still have to watch out for the horrible exam questions! Haha!
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Which exam? There are some exams where they seem to give you incorrectly‑indented code in the hope of confusing you.

I removed the quote; there is usually no need to quote the whole of a preceding post. That simply makes the thread longer and longer.
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the official exam is the one I will be taking sometime very soon.
But I just did Mala Gupta's book, now I'm working through a practice exam book by Deshmukh. Any other books or free online tests I should do before I take the real thing?
I was recommended those by fellow board members (thanks again! They've been really helpful so far in clarifying things!)
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell was asking WHICH certification exam you are taking. There are several. Or perhaps you are taking a college class and have an exam there, but I assume not.

If you look, we have a whole section here devoted to the various certification exams. There are about 8-10 related to Java.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Non‑private instance methods: Always polymorphic if you try to override them. Always use the @Override annotation if you think you are overriding a method. The version called is from the object, and it goes from there to the Class<XYZ> object which that object was created from. So, yes, if the subclass has an overridden version it is that version which is called. We have a story, only legible to dogs.


To give more detail, if the subclass can access the instance method of the super class, then it is overriding -- otherwise, it is not. This means that private instance methods of the super class are not overridden. Likewise, instance methods of the super class, with default access level, are not overridden by subclasses in a different package.

Henry
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Campbell was asking WHICH certification exam you are taking. There are several. Or perhaps you are taking a college class and have an exam there, but I assume not.

If you look, we have a whole section here devoted to the various certification exams. There are about 8-10 related to Java.


Oh, I didn't know there were multiple Java exams for beginners? I am planning to take the Programmer Level I Exam. Sorry for the confusion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!