Results 1 to 4 of 4
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default 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();
        }
    }

  2. #2
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: what really happens with extend and inheritance?

    INHERITANCE
    INHERITANCE

    Hope the links clear the concept.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Inheritance using extend class
    By vigalegiadom in forum New To Java
    Replies: 2
    Last Post: 11-16-2012, 07:37 AM
  2. to Extend or not to Extend
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 12-21-2011, 04:28 PM
  3. How to extend two classes
    By man4ish in forum AWT / Swing
    Replies: 4
    Last Post: 12-31-2009, 02:31 PM
  4. Extend Jscroll
    By cowboy in forum New To Java
    Replies: 10
    Last Post: 12-13-2009, 08:07 PM
  5. extend a button
    By Omarero in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •