Whats the difference between subClass sc = new subClass() and superClass sc = new subClass?
Asked Answered
W

7

5
class superClass {}

class subClass extends superClass{}

public class test
{

    public static void main()

{

    superClass sc1 = new subClass();
    subClass sc2 = new subClass();
    //whats the difference between the two objects created using the above code?

}
}
Wiskind answered 24/3, 2013 at 7:34 Comment(1)
possible duplicate of What does it mean to "program to an interface"?Heartthrob
F
9

Simple explanation : When you use

SuperClass obj = new SubClass();

Only public methods that are defined in SuperClass are accessible. Methods defined in SubClass are not.

When you use

SubClass obj = new SubClass(); 

public methods defined in SubClass are also accessible along with the SuperClass public methods.

Object created in both cases is the same.

Ex:

public class SuperClass {

  public void method1(){

  }
}

public class SubClass extends SuperClass {
  public void method2()
  {
 
  }
}

SubClass sub = new SubClass();
sub.method1();  //Valid through inheritance from SuperClass
sub.method2();  // Valid

SuperClass superClass = new SubClass();
superClass.method1();
superClass.method2(); // Compilation Error since Reference is of SuperClass so only SuperClass methods are accessible.
Flor answered 24/3, 2013 at 7:37 Comment(0)
A
2

whats the difference between the two objects created using the above code?

The two objects are exactly the same. What's different is the type of the variable where the object reference is stored. In practice, this means that if there are any methods specific to subClass, you'll be able to access them through sc2 but not through sc1 (the latter would require a cast).

Ahrens answered 24/3, 2013 at 7:36 Comment(0)
O
2

In both the cases, objects of subClass will be created, but the references will be different.

With the reference of superClass i.e sc1 , you won't be able to invoke the methods present in the subClass but not in the superClass. You will require casting to invoke the subClass methods.

Like :

class superClass {
   public void superClassMethod(){
   }
}

class subClass extends superClass{
   public void subClassMethod(){
   }
}

Now :

public class test
{
  public static void main(){

    superClass sc1 = new subClass();
    subClass sc2 = new subClass();
    //whats the difference between the two objects created using the above code?


    sc2.subClassMethod(); //this is valid

    sc1.subClassMethod(); // this is a compiler error, 
                          // as no method named subClassMethod
                          // is present in the superClass as the
                          // reference is of superClass type

   // for this you require a type cast

   (subClass)sc1.subClassMethod(); // Now this is valid

  }
}
Ottilie answered 24/3, 2013 at 7:38 Comment(0)
L
1

The objects are the same. However, because sc1 is declared as type superClass you cannot use subClass methods on it without casting.

Luminous answered 24/3, 2013 at 7:36 Comment(0)
P
1

In addition to the above answers, because the constructed object is really an object of type SubClass you can cast it to SubClass and call the methods:

SuperClass superClass = new SubClass();
superClass.method1();
((SubClass)superClass).method2();
Phrase answered 14/6, 2014 at 10:31 Comment(0)
B
0

just think like this. when you using the parent (super class) reference you will get access to only the things(variable , methods) that parent has. but if you use the reference of child you will get everything that the parent and child have (child inherit properties from parent).

Banausic answered 24/3, 2013 at 8:44 Comment(0)
C
0
public class SuperClass {
    
      public void method1(){
    
      }

 public void method3(){
    
      }


    }
    
    public class SubClass extends SuperClass {
      public void method2()
      {
    
      }
@override
 public void method3(){
    
      }
    }
    
    SubClass sub = new SubClass();
    sub.method1();  //Valid through inheritance from SuperClass
    sub.method2();  // Valid 
    sub.method3();//valid 


   superClass sc1 = new subClass();
    superClass.method1();
superClass.method2();////compilation error: cannot find symbol method
superClass.method3();//run over ridden version

superClass sc1 = new subClass();

You can invoke all the methods defined in theSuperClass for the reference sc1, (which is actually holding a Subclass object), This is because a subclass instance possesses all the properties of its superclass.

However, you CANNOT invoke methods defined in the subclass for the reference sc1, .

// CANNOT invoke methods in Subclass as sc1 is a superclass reference

This is because sc1 is a reference to the Superclass, which does not know about methods defined in the subclass .

sc1 is a reference to the Superclass, but holds an object of its subclass . The reference sc1, however, retains its internal identity. In our example, the subclass overrides methods Method3 . sc1.Method3 invokes the overridden version defined in the subclass , instead of the version defined in superclass. This is because sc1 is in fact holding a subclass object internally.

Cotsen answered 26/12, 2021 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.