File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Inheritance 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 "Regarding Inheritance" Watch "Regarding Inheritance" New topic

Regarding Inheritance

Akshay Singhvi
Ranch Hand

Joined: Nov 08, 2005
Posts: 93
hello all ranchers,

while solving Rule Roundup game i came across this problem:-

The answer is :- Akshay

well my doubt is when method call() is called at 1 ,since subclass doesn't override call() method the superclass call() method is called.That is ok.
Also the reference of the subclass is also passed implicitly to the method.
So i can correctly rewrite the code at 2 as:
public void call(){System.out.println(this.s);}

Then why doesn't "Singhvi" gets printed.

Hope i must have clearly jotted down my doubt
[ November 20, 2005: Message edited by: Barry Gaunt ]

Regards,<br />Akshay Singhvi<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (86%)
Mausam M Kakkad
Ranch Hand

Joined: Oct 27, 2005
Posts: 40
Hi Akshay,

Good question....

The case here is of "Member Hiding". In your subclass you have hidden String object of Superclass by giving the same name. But in JAVA, only methods can be overridden and not the attributes. So, your superclass doesnt have any idea about Overridden String. When the control goes into the superclass, it just know its own copy of the variable. So it prints like that.

Again, want to quote one thing "Methods can be overridden, not the attributes".

Hope it will help

I agree. Here's the link:
subject: Regarding Inheritance
It's not a secret anymore!