Results 1 to 2 of 2
- 03-22-2012, 10:27 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Very interesting Abstract Class behavior. Please Help :)
When I run this code, the output is:Java Code:public abstract class MyAbstractClass { int age = 15; public void printAge() { System.out.println(age); } } public class ConcClassA extends MyAbstractClass { int age = 0; ConcClassA(){ age = 45; } @Override public void printAge() { System.out.println(age); } } public class TestLearningPackage { public static void main(String[] args) { MyAbstractClass testA = new ConcClassA(); System.out.println("testA.age : " + testA.age); System.out.print("testA.printAge(): "); testA.printAge(); } }
testA.age : 15
testA.printAge(): 45
The question is:
When I comment out: int age = 0 the output is now: ( on line 9 )
testA.age : 45
testA.printAge(): 45
How does commenting int age = 0 changes the testA.age from 15 to 45 ?
-
Re: Very interesting Abstract Class behavior. Please Help :)
The field 'age' in ConcClassA hides the field 'age' in its super class. If you remove this field from the sub-class, then the variable 'age' will have to refer to the field in the super class. This is nothing out of order, its normal behaviour.
Please read over:
Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
Similar Threads
-
abstract method-abstract class
By durgaprasad1407 in forum New To JavaReplies: 1Last Post: 04-29-2011, 06:58 PM -
Class is not abstract and does not override abstract method run(com.
By rgeurts in forum New To JavaReplies: 4Last Post: 04-14-2011, 11:42 AM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 10:51 AM -
Object class's equals() method behavior????
By skyineyes in forum New To JavaReplies: 4Last Post: 07-19-2008, 11:58 PM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks