Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding??!

 
Ernest Lee
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
======================================
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys, u guys are all right?
Just wonder... did I study too much....?
haha...
good luck to myself!
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrong! Private methods cannot be accessed by object.
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rashmi. I was in a hurry so could'nt explain it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic