Results 1 to 4 of 4
- 02-09-2013, 02:21 PM #1
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 570
- Rep Power
- 6
what really happens with extend and inheritance?
I constructed three class: Age, Father and Son (extending father) and
I wonder what will happen (see code down)?
Sure the Son's class should call the constructor of the Father's (default constructor), right!
But why get's also the Father's age initialized?
Here is the output:
56 is father's age
13 is son's age
BUILD SUCCESSFUL (total time: 0 seconds)
Java Code:package willemlearningjava; class Age { int a=0; Age(int a) { // System.out.println(" inside constructor Age"); this.a = a; System.out.print("" + a); } } package willemlearningjava; class Father { Age age = new Age(56); Father() { System.out.print(" is father's age "); System.out.println(); } } package willemlearningjava; public class Son extends Father { Age age = new Age(13); Son() { System.out.print(" is son's age "); System.out.println(); } public static void main(String[] arc) { Son s = new Son(); } }
- 02-09-2013, 02:28 PM #2
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
Re: what really happens with extend and inheritance?
INHERITANCE
INHERITANCE
Hope the links clear the concept.
- 02-09-2013, 02:44 PM #3
Re: what really happens with extend and inheritance?
Son extending father is a bad design, and a misuse of inheritance. Is a son a specialized type of father? No, I thought not.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-11-2013, 11:51 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: what really happens with extend and inheritance?
Slight abuse of inheritance aside, trace the code.
Both Father and Son have an 'age' attribute.
This attribute is initialised in their respective constructors.
So follow the code through by hand and see what gets called.Please do not ask for code as refusal often offends.
Similar Threads
-
Inheritance using extend class
By vigalegiadom in forum New To JavaReplies: 2Last Post: 11-16-2012, 07:37 AM -
to Extend or not to Extend
By fatabass in forum New To JavaReplies: 4Last Post: 12-21-2011, 04:28 PM -
How to extend two classes
By man4ish in forum AWT / SwingReplies: 4Last Post: 12-31-2009, 02:31 PM -
Extend Jscroll
By cowboy in forum New To JavaReplies: 10Last Post: 12-13-2009, 08:07 PM -
extend a button
By Omarero in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks