• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in inheritance...

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can attributes in the subclass override the attributes in the super class?

can attributes inherited?


can you explain me with an example?
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gabriel Ozeas
Ranch Hand
Posts: 30
Google App Engine Hibernate jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic