File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Explanation needed:  Method can be overridden , Attribute can not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Explanation needed:  Method can be overridden , Attribute can not" Watch "Explanation needed:  Method can be overridden , Attribute can not" New topic
Author

Explanation needed: Method can be overridden , Attribute can not

saxena vicky
Greenhorn

Joined: Sep 05, 2005
Posts: 18
I have a doubt in this question ?
I know that "Method can be overriden , Attribute can not" so the
answer will be : ----A----Vicky
but my question is if I override the method printname() in ClassB1
the out put is : ----B----praveen
can any one explain me?

class ClassA1{
public String name ="Vicky";
public void printName() {
System.out.print("----A----"+name);
}
}

class ClassB1 extends ClassA1{
public String name ="Praveen";
/*public void printName() {
System.out.print("----B----"+name);
}*/
}

public class XYZ {

public static void main(String[] args) {
ClassB1 B =new ClassB1();
B.printName();

}

}
[ October 04, 2005: Message edited by: saxena vicky ]
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi Vicky,

First of all, this can't be called an example of overriding after removing the comments from the printName of ClassB1. I may suggest to create the variable of ClassA1 and initialise that with ClassB1 i.e. ClassA1 objClass1 = new ClassB1(); and then try the same. You will get -------B----Vicky printed.

I can tell you the reason for this. Methods are overriden but attributes are not in the case when you create a variable of the base class and not that of the sub class. That is declaration in other words.

I hope you could understand by the exaplanation given by me. If not feel free to come back


Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
saxena vicky
Greenhorn

Joined: Sep 05, 2005
Posts: 18
Hi Bimal,
Still not it is not clear to me. Correct me If I am wrong
If we do not have printName() method in the sub class. Then the concept �Method can be overriden , Attribute can not� applies.
And we do
ClassB1 B =new ClassB1();
B.printName();
In main
The output is ----A----Vicky
In this case the method printName() in the super class is executed, because there is no method In the sub class.
----------------------------------------------------------------------
Now if I override the printName() method in sub class. �Uncomment the code�
The output is ----B----praveen
As I have overridden the method printName() so the method printName() in the sub class get executed.
BUT ,now the variable "name" is also overridden WHY???
anya nav
Greenhorn

Joined: Oct 04, 2005
Posts: 4
Try this :
In parent class:


public class AttributesInherited {
protected String parent = "MommyAndDaddy";
public void getParent(){
C.out("----Parent Class----"+parent);
}
}

In child class:

public class AttributesChild {

protected String parent;
public void getParent(){
C.out("----Child Class----"+parent);
}

public static void main(String [] arg){
AttributesChild a = new AttributesChild();
a.getParent();
}
}

NOTE:
C.out corresponds to System.out.println...(jst dont like to use that loong structure)

Gives:

----Child Class----null

---------
What does this mean?:
1.Instance variables are not inherited..
2.Methods are..

Hope this helps..
Regards,
Anya
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi Vicky,

Originally posted by saxena vicky:
And we do
ClassB1 B =new ClassB1();
B.printName();
In main
The output is ----A----Vicky


Let me clear the concept of overriding first of all. If you have the same method with the same signature in the sub class, it is the concept of overriding. While calling the method in this case, the compiler will look for the object type. If you write ClassB1 B ... object B has type ClassB1 and not ClassA1. So, here, it'll go and check whether there is any method named printName(). If there is, it'll call or it'll call the super class' method. In the method, a variable is being used, i.e. name. For this again, the same funda is applied. It'll go and collect the value of name variable declared in ClassB1 because the variable can be found there. If it is not there, it'll go and check in the super class whether any non-private variable available for the name given or not. This is called Compiletime Polymorphysm. Because this is decided at the compile time. Overloading is also an example of Compiletime Polymosphysm

Let me tell you the second scenario i.e. ClassA1 objA1 = new ClassB1(). In this case, the type is ClassA1 but the object is of ClassB1. So, in this case, whenever it needs to get variables, it'll always go to ClassA1 and not to ClassB1. In other words it'll go to the "type" and not to the "object" class. While calling a method, it'll go to the subclass' method if any. This is called Runtime Polymorphism because this is decided by JVM and not by the compiler.

If you still have doubt, let me know
saxena vicky
Greenhorn

Joined: Sep 05, 2005
Posts: 18
Hi Bimal,
Thanks for the Explanation. But still I have a doubt.
class ClassA1{
public String name ="Vicky";
public void printName() {
System.out.print("----A----"+name);
}
}

class ClassB1 extends ClassA1{
public String name ="Praveen";
public void printName() {
System.out.print("----B----"+name);
}
}

If I do :
ClassA1 objA1 = new ClassB1();
objA1.printName();

The Output is: ----B----Praveen

As you say that objA1 is of type ClassA1 the it should first search for the method printName() in the class ClassA1 if it is found the that method is executed. But the output is ----B----Praveen
Why???
According to your explanation it should be ----A----Vicky

[ October 04, 2005: Message edited by: saxena vicky ]
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199

As you say that objA1 is of type ClassA1 the it should first search for the method printName() in the class ClassA1 if it is found the that method is executed. But the output is ----B----Praveen
Why???
According to your explanation it should be ----A----Vicky


Answer to this statement is NO...

In your code, u r invoking a method which in internally accessing a var. Thus var availabale to method is printed.

Access and print var from main itself.

refer following code for this


class ClassA1
{ public String name ="Vicky";
public void printName()
{System.out.println("----A----"+name);
}

public void printVar()
{System.out.println("----printVar----"+name);
}
}

class ClassB1 extends ClassA1
{ public String name ="Praveen";
public void printName()
{System.out.println("----B----"+name);
}

public void printVar()
{System.out.println("----printVar----"+name);
}

}

public class XYZ
{public static void main(String[] args)
{ClassB1 B =new ClassB1();
B.printName();
System.out.println(B.name);
B.printVar();

ClassA1 B1 =new ClassB1();
B1.printVar();
System.out.println(B1.name);


}
}


In case you still have any doubts, let me know


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi All,

First of all I appologize for my previous mistake . I forgot that the method will first search the variable in the class itself. Dev is correct. If one tries to print the variable, superclass' variable will get printed. Thanks Dev .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explanation needed: Method can be overridden , Attribute can not