wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in 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 "doubt in inheritance..." Watch "doubt in inheritance..." New topic
Author

doubt in inheritance...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
can attributes in the subclass override the attributes in the super class?

can attributes inherited?


can you explain me with an example?


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Originally posted by Ganeshkumar cheekati:
can attributes in the subclass override the attributes in the super class?

can attributes inherited?


can you explain me with an example?


I think so, subclass can override the attribute of the superclass, here is a simple programme which could prove that line.

The answer of it is 3
and,
class Super
{
public int arr=34;
public void method()
{
}
}
class Sub extends Super
{

Sub()
{
System.out.println(arr);
}

public static void main(String[] args)
{
Sub e=new Sub();
}
}

The answer of this program is 34
[ October 24, 2008: Message edited by: Pawan Arora ]
grigoris philippoussis
Greenhorn

Joined: Oct 22, 2007
Posts: 16
I think, in the above example, this is a question of scope. The class sees its own variable before that of it's base-class (I might be wrong here!)

Otherwise, the SCJP book by Sierra and Bates has a very good discussion on the rules for inheritance.
grigoris philippoussis
Greenhorn

Joined: Oct 22, 2007
Posts: 16
I tried this, you can print out super.arr as well!
So the class appears to have two variables called arr.
Can someone explain this further?
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Instance variables cannot be overridden. In this case, the 'arr' variable in 'Sub' class is a new declaration done in Sub, and is NOT an overridden instance variable.

As per rules of method overriding, the overridden method in the subclass should not be more restrictive than the superclass version, right?

But, If you have the subclass code as private int arr = 45; ,it will compile, even though 'arr' has more restrictive privilege in the subclass. This means that 'arr' is not overridden.

On the other hand, if you override public void method() as private void method() in the subclass, you will get a compiler error.

So, this proves that only instance methods can be overridden and not instance variables.
But, both instance methods and instance variables are inherited. The property of superclass is the property of subclass also (unless the member is not private)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

well fields act just as static members. you cannot override them. however you can declare a field with the same name in a sub-class as a field in the super class. I have changed the example by pawan



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
so variables depends upon the reference variable which can be used to call the variable if both variables are having same name in super as well as sub class..

am i right?
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I think this is referred to as "hiding" or "shadowing".

If you have a superclass and subclass that each contain a variable with the same name, the access is controlled by the type of the reference variable. If the reference variable is super you will access the superclass' variable. If the reference variable is subclass you will access the subclass' variable. Now BOTH of these cases are talking about direct access (refVar.varName style). If a superclass variable is used to reference a method that is overridden in the subclass and it uses the same variable name.... IT will use the subclass variable.


------------------------
Bob
SCJP - 86% - June 11, 2009
Gabriel Ozeas
Ranch Hand

Joined: Jul 04, 2008
Posts: 30

Be careful with methods!!



the a.getArr() will print 3 because the compiler add this. to the variable...

So pay attention

[ October 24, 2008: Message edited by: Gabriel Ozeas ]
[ October 24, 2008: Message edited by: Gabriel Ozeas ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in inheritance...