aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding??! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding??!" Watch "Overriding??!" New topic
Author

Overriding??!

Ernest Lee
Ranch Hand

Joined: Aug 20, 2001
Posts: 145
======================================
class Base {
String a = "Base String";
}
public class Subclass extends Base{
String a = "Subclass String";
public void MethodA() {
System.out.println(a);
}
public static void main(String args[]) {
Base objBase = new Subclass();
objBase.MethodA();
}
}
I bet the above code can't compile because Base doesn't
have MethodA at all!!
======================================
class Base {
String a = "Base String";
public void MethodA() {
System.out.println(a);
}
}
public class Subclass extends Base{
String a = "Subclass String";
public void MethodA() {
System.out.println(a);
}
public static void main(String args[]) {
Base objBase = new Subclass();
objBase.MethodA();
}
}
I bet the above code will print "SubClass Class" because
the MethodA in SubClass is being called! The result will be the same
even if the String a is declared as private! However, if the MethodA in Base is declared as private ... the the program will printed "Base string"!
======================================

Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
No , It u declare methodA() in base class as private, it does not print "Base String" as u said.
the program won't even compile!

The reason is : methodA() being private,subclass doesn't inherit it, so methodA() in subclass is a normal method and not the overridden method.
So When u use a base class ref. to invoke a subclass method, the compiler complains. As this is not overriding.It is same as the first case u stated.
Rashmi
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello folks,
I am writing this to check my understanding on this topic...
In the Second code...
Here, the baseclass reference found a method A() in the baseclass...but on finding that the class of the object it is denoting is of the type subclass, invoked the method A() in the subclass.
In the First code...
What I understand is in this case is... since the reference is of type baseclass, it searches for the method A() in the baseclass and since it does not find one, promptly fails to compile.
And regarding the title of this post, don't you think it is necessary for a special syntax(eg. super()) for a subclass to override a method in the superclass?
Please do correct me if I am wrong.
Thanks in Advance...
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 05, 2001).]
Ernest Lee
Ranch Hand

Joined: Aug 20, 2001
Posts: 145
Rashmi,
are u sure.. i will compile and run the program tonight...
i will copy and paste the result here.. watch out!
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Hey Ernest,
I tested the code right now and it does not compile!!!This is what i tried...
class Base {
String a = "Base String";
private void MethodA() {
System.out.println(a);
}
}
public class Subclass extends Base{
String a = "Subclass String";
public void MethodA() {
System.out.println(a);
}
public static void main(String args[]) {
Base objBase = new Subclass();
objBase.MethodA();
}
}
The compiler complains as---
\Subclass.java:19: MethodA() has private access in Base
objBase.MethodA();
^
1 error
Tell me what u tried, so it compiled.
Rashmi
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Earnest,
This is what I understood,
As we all know
in java methods are binded at runtime ,i.e. call to method are associated with the body of method at runtime.
First code:
When you assign a, base class reference with an object of subclass,you are tricking the compiler.Compiler,unobvious about whats going on simply looks the method in the reference type that it found.In this case,Base objBase.So we have a compile time error.
Second code:
As I explained above, the compiler again looks the method in the base class.So when you declare the MethodA as private,as Rashmi said,there is an access error.And remember one thing Polymorphic calls or late binding happens only with methods.So it is not of great meaning whether you declare the data member as private or something else.
THANKS
[Please correct if wrong]
[This message has been edited by Bindesh Vijayan (edited September 05, 2001).]
Ernest Lee
Ranch Hand

Joined: Aug 20, 2001
Posts: 145
sorry guys, u guys are all right?
Just wonder... did I study too much....?
haha...
good luck to myself!
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Why I am getting error with following code saying..No method matching found in class Base..

Base class object can CALL it's private methods ,rite??
thanx


Swati Kale
SCJP
SCWCD
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Wrong! Private methods cannot be accessed by object.
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Then ,How do you access the private methods of a class...?? By creating Object of that class......??
God, it's such a basic but seem to got into loop..
Thanx
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Swati one cannot access any private members of the class.If there is any such need he should rethink the design pattern for the program,such as making the meth as public or static or accessing it thru some public meth.You should'nt confuse yourself.
THANKS
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Swati,
The purpose of a private member is to make it visible only within the class, ie. all class methods can access it. So if u want to access the private var from outside , write a method for it. (Eg. getBalance()-> that returns a balance of an a/c which is private var).
Now when a method itself is a private method , it can be used only by other methods in class, as it is private to class.
All objects of class has a their own copy of a private variable. But that does not mean that ,
obj.i =1;//i is private
This is never possible!!!
So
obj.method(); //method is private
is also never possible.
Hope this clears ur confusion!
Rashmi
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Thanx a Bunch Bindesh and Rashmi....I had really got confused..I guess ,such silly mistakes lead to low score...I shall take care..
Thanx
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Thanks Rashmi. I was in a hurry so could'nt explain it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding??!