wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tricky inheritance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tricky inheritance question" Watch "Tricky inheritance question" New topic
Author

Tricky inheritance question

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
From Enthuware SCJP 6 preparation:

Consider the following class hierarchy shown in the image. (B1 and B2 are subclasses of A and C1, C2 are subclasses of B1) Assume that method public void m1(){ ... } is defined in all of these classes EXCEPT B1 and C1. Which of the following statements are correct?

1. objectOfC1.m1(); will cause a compilation error.
2. objectOfC2.m1(); will cause A's m1() to be called.
3. objectOfC1.m1(); will cause A's m1() to be called.
4. objectOfB1.m1(); will cause an exception at runtime.
5. objectOfB2.m1(); will cause an exception at runtime.

Which of these statements is true?


All code in my posts, unless a source is explicitly mentioned, is my own.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Option 3 is true.

Now tell me what did i missed. Whats the tricky thing here :roll:


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You can override a concrete method in a superclass with an abstract method in a subclass. The tricky part is to realize that method definition and method implementation are different things, so you can define an abstract method overriding a concrete method. However, you can't instantiate an abstract class, which makes it easier to see that B1 and C1 are not abstract.

I find terminology like "is not defined in" very misleading. It should be worded as "is not overriden in." You can "undefine" a concrete method of the superclass by an abstract override.

Hopefully there won't be too many questions like this on the real exam.

Here's a question for you, James: Doesn't overriding a concrete method with an abstract method go against the public contract of the superclass?
[ December 22, 2008: Message edited by: Ruben Soto ]
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
@Ruben
The original question posted by you does not mention the keyword abstract anywhere. It talks of the method public void m1() being defined in some of the subclasses. Given the class hierarchy it is easy to understand the relationship between the classes and thereby the overriding of the method m1().
I think while preparing for the exam it is very easy to get too much into the details and ignore the concept being tested upon.
IMHO, the abstract thing that you have brought in seems to complicate a simple question by introducing an unrelated perspective.

Doesn't overriding a concrete method with an abstract method go against the public contract of the superclass?


Firstly, i really am not able to visulaize when i would need a concrete class as a parent of an abstract class.
Secondly, even if i have that class hierarchy defined as follows:



How would i ever be able to use a reference to class B without ever creating a concrete subclass of class B?
Now in order to invoke the method m1() in class A i can say,



Similarly, to invoke the method m1() in class C i can say,

OR


Also i can use the reference to class B as follows:



How am I violating the public contract of the method m1() in superclass by defining an abstract method m1() in a subclass, when i know that i *have* to instantiate the abstract class indirectly by creating a concrete subclass and then invoke method m1()?


thanks
Harvinder
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

I don't understand...How would it be possible for C not to inherit the method...I mean, surely B will inherit from A and then C will inherit from B...


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Originally posted by Duran Harris:
I don't understand...How would it be possible for C not to inherit the method...I mean, surely B will inherit from A and then C will inherit from B...


Duran where did you find these lines?


SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
In response to Harvinder:
@Ruben
The original question posted by you does not mention the keyword abstract anywhere. It talks of the method public void m1() being defined in some of the subclasses. Given the class hierarchy it is easy to understand the relationship between the classes and thereby the overriding of the method m1().
I agree, but I still believe that if "overridden" had been used instead of "defined" it would have been clearer and more precise. Regardless, the names of the reference variables used to call the method, and the fact that you can't instantiate an abstract class make this question perfectly viable.


I think while preparing for the exam it is very easy to get too much into the details and ignore the concept being tested upon.
IMHO, the abstract thing that you have brought in seems to complicate a simple question by introducing an unrelated perspective.

Yes, I have to agree again. I think the reason why I do that is because I am on "paranoid" mode to see where the question is going to trick me.


Firstly, i really am not able to visulaize when i would need a concrete class as a parent of an abstract class.
Secondly, even if i have that class hierarchy defined as follows:



How would i ever be able to use a reference to class B without ever creating a concrete subclass of class B?
Now in order to invoke the method m1() in class A i can say,



Similarly, to invoke the method m1() in class C i can say,

OR


Also i can use the reference to class B as follows:



How am I violating the public contract of the method m1() in superclass by defining an abstract method m1() in a subclass, when i know that i *have* to instantiate the abstract class indirectly by creating a concrete subclass and then invoke method m1()?
That's an excellent explanation, and that's exactly why you can override a concrete method by an abstract method. As to the practical uses of this, I am sure whatever you are trying to achieve could be done some other way, but I think this could be useful, especially considering that Java has single inheritance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tricky inheritance question
 
Similar Threads
Overload method
Overloading
Casting
Overloading
RemoteException and transaction roll back