This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance/Polymorphism 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 "Inheritance/Polymorphism question" Watch "Inheritance/Polymorphism question" New topic
Author

Inheritance/Polymorphism question

sam pitt
Ranch Hand

Joined: Mar 07, 2001
Posts: 48
Hi,
The following code gives compile error. It says "NO method matching aMethod() found in class Base()"
What I do not understand is that there is a method aMethod() in class Base albeit with access modifier of private. Has this anything to do with the error ? If yes please explain me.
-Sam

Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Yes, you are right. In another class, private method is invisible . And private method doesn't join in overriding. So reference "b" doesn't know anything about the public method in Derived class which happens to have same name as the Base class. Hope it helps
Jason


green horn
sam pitt
Ranch Hand

Joined: Mar 07, 2001
Posts: 48
OK... I understood half of it i.e private method do not participate in overriding. But then there is a method aMethod() defined in Derived class. Why is that not considered by the compiler ?
Oh ya.. btw congrats for clearing SCJP. Real
score
-Sam
[This message has been edited by sam pitt (edited July 27, 2001).]
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
That's the point. Reference "b" is a Base type, out of class Base, "b" doesn't know anything about a method named "amethod". So by "b" you can't call the mehtod. That's what the compiler thinks.
Jason.
sam pitt
Ranch Hand

Joined: Mar 07, 2001
Posts: 48
I see your point, but again isn't the concept of Dynamic Method Binding says that 'When a method is invoked using a reference, the method definition which actually gets executed is determined by the class of the object denoted by reference' and in my example, the class denoted is that of object Derived.. so aMethod() of Derived class should be the chosen one!!
What is that I am missing ?
-Sam
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Yes, i know what you mean. But private method doesn't take part in overriding, and we can't call the private method in other classes. Even if the object referenced by "obj" is type Derived, the compiler solve the dynamic binding by the reference type, not by the real object. In this case, the compiler only see a private method, it doesn't know anything about dynamic binding of the method in Base. The only thing compiler knows at Derived is there is no named "amethod" in Base which is visible. That's the reason you got the complain.
Jason
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What makes this interesting is that this is a compile time problem... not a runtime problem. private methods can only be run from inside the class so the compiler doesn't allow it.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Sam,
The method aMethod() defined in the Base class is not accessible in the Derived class.Hence you get the compile-time error.
BTW, let us modify this code to the following and see what you get :


Do we still get compile-time error.If not, why?
-- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
We won't get compiler error. Because now main is in the Base class and the private method is visible to Base b.
b.amethod()--> will invode Base class's amethod.
Am I right?
Vanitha.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
That's correct - again signifying that the private method is accessible in the class in which it is defined.That was the reason, why you were getting an error in the previous code snipplet.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
I have modified the above code snippet, as follows
==================================================
class Base {
int i = 99;
private void amethod(){
System.out.println("Base.amethod()");
}
Base(){
amethod();
}
}

public class Derived extends Base{
int i = -1;
public static void main(String argv[]){
Base b = new Derived();
//System.out.println(b.i);
//b.amethod(); // can't call the private method.....
}
public void amethod(){
System.out.println("Derived.amethod()");
}
Derived(){
amethod();
}
}
=================================================
Prints:
Base.amethod()
Derived.amethod()
=================================================
Here I am just creationg an object and its calling both the constructors in the base class as well as Derived class, and when I remove the constructor in the Derived class it just calls the aMethod and print both the statements. Can somebody explain why?
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Muhammad,
In the code snipplet you posted, you are creating a Derived class instance.Since Base class is the superclass of Derived, implicitly the call is first given to the Base class.Hence, Base.amethod() gets executed.Then the control returns to the Derived class and Derived.amethod() is called.
When you remove the constructor of the Derived class, the default constructor is called when you instantiate this class.Again, the control goes to super class Constructor first, which executes the Base.amethod().In this case, since the default constructor of Derived class doesn't call Derived.amethod(), this method is not executed.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritance/Polymorphism question
 
Similar Threads
Method from constructor
Mock Exam question.
How is this??
Doubt in Mock Question
Confusion!!