Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Inheritance Question : Assigning a subclass example in a superclass variable

    Hi,
    I understand that a subclass object can be assigned to a superclass variable. The superclass
    variable will not have only the superclass properties (instance variable and methods). Anything
    specific to subclass should be discarded.

    I wrote this small piece of code where I declared 2 classes. TestInheritance : the superclass
    and InheritedClass. Both the classes has a method show with different code.
    Now when I reference InheritedClass object to a TestInheritance variable and call the show
    method, It should execute the show version of the superclass that is TestInheritance , but instead
    it's running the version of the method of the subclass, InheritedClass.

    It's seems contradictory from my understanding. Can someone kindly explain this to me.
    Thanks in Advance.

    ** I am pasting my code here. There are 3 classes.
    JDK Version :: jdk1.6.0_18

    Java Code:
       /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package com.ustrosoft.Tutorial.Revise;
    
    /**
     *
     * @author Administrator
     */
    public class TestInheritance {
      int a;
      int b;
      String c;
    
      TestInheritance(int a,int b,String c)
      {
       this.a=a;
       this.b=b;
       this.c=c;
      }
     public void show()
     {
      System.out.println("Class1 :: Var a:"+a+";Var b:"+b+";Var c:"+c);
     }
    
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package com.ustrosoft.Tutorial.Revise;
    
    /**
     *
     * @author Administrator
     */
    public class InheritedClass extends TestInheritance {
      int a;
      int b;
      String c;
      int d;
    
      InheritedClass(int a,int b,String c)
      {
       super(++a,++b,c+"Extra");
       this.a=a;
       this.b=b;
       this.c=c;
       d=1;
      }
        @Override
     public void show()
     {
      super.show();
      System.out.println("Class2 :: Var a:"+a+";Var b:"+b+";Var c:"+c+" var d: "+d);
     }
    }
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    Java Code:
    package com.ustrosoft.Tutorial.Revise;
    
    /**
     *
     * @author Administrator
     */
    public class TestTutorial {
    
       public static void main(String[] args)
       {
           TestInheritance a;
           InheritedClass b=new InheritedClass(1,2,"Hello");
           b.show();
           a=b;
           a.show();
    
       }
    
    }
    The output of the code is ::

    run:
    Class1 :: Var a:2;Var b:3;Var c:HelloExtra
    Class2 :: Var a:2;Var b:3;Var c:Hello var d: 1
    Class1 :: Var a:2;Var b:3;Var c:HelloExtra
    Class2 :: Var a:2;Var b:3;Var c:Hello var d: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    where it should have been

    Class1 :: Var a:2;Var b:3;Var c:HelloExtra
    Class2 :: Var a:2;Var b:3;Var c:Hello var d: 1
    Class1 :: Var a:2;Var b:3;Var c:HelloExtra
    Last edited by mayukh.banerjee; 06-19-2013 at 06:44 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inheritance Question : Assigning a subclass example in a superclass variable

    That is one of the fundamental mechanisms of an object oriented programming system: even if you think you're dealing with a super class object while it is an object from a derived class, you call methods from the derived class; that is true overriding and the gory details are often implemented with a 'virtual function table' which is constructed when the class itself is initialized. Variables aren't overridden though (but they can be shadowed).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Simple subclass, superclass question
    By jenny27 in forum New To Java
    Replies: 3
    Last Post: 10-03-2012, 03:49 PM
  2. Replies: 3
    Last Post: 05-04-2012, 07:46 AM
  3. Superclass and Subclass Constructor Question
    By achiu17 in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 05:05 AM
  4. Superclass and Inheritance question.
    By Dark in forum New To Java
    Replies: 6
    Last Post: 05-06-2011, 07:45 AM
  5. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM

Posting Permissions

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