8

Can you please tell me difference between these three in respect to their usage :-

Superclass p1 = new Superclass();
Superclass p2 = new Subclass();
Subclass p3 = new Subclass();
pranavj95
  • 151
  • 9

2 Answers2

6

The first one and the 3rd one make respective objects of the type declared - usage is perfect, and you get all the expected methods.

The second one gives you the functionality of only the Superclass even though you actually have a Subclass object, because you declared it as a Superclass.

eg:

Superclass p1 = new Superclass(); Your p1 object has access to Superclass methods.

Superclass p2 = new Subclass(); Your p2 object only has access to Superclass methods. The program doesn't know p2 has Subclass specific methods because you declared it as a Superclass.

Subclass p3 = new Subclass(); Your p3 object has access to all Subclass and Superclass methods.

Note for using p2 method of declaration: Given the following classes A and B

public class A {
    public void print(){
        System.out.println("yyy");
    }
}

public class B extends A{
    public B() {
        super();
    }

    public void print() { // this overrides the print() method in A
        System.out.println("xxx");
    }

    public void printb() {
        System.out.println("second");
    }
}

This happens in code:

public static void main(String[] args) {
    A x =  new B();

    x.print(); // prints xxx because you actually have a B object even though you have an A reference
    x.printB(); // doesn't compile. You can't call B specific methods through an A reference.
}
Aify
  • 3,543
  • 3
  • 24
  • 43
  • So accordingly, 1st and 2nd one are same? – pranavj95 May 13 '15 at 20:43
  • @Aify you're saying that the p2 object only has access to Superclass methods. Cannot the subclass methods get invoked polymorphically though? – AtomHeartFather May 13 '15 at 20:46
  • As suggested by @Aify what is the use of subclass object in second if it cannot access the subclass methods and variables? – pranavj95 May 13 '15 at 20:47
  • To be frank, I would almost always declare my objects using the first/third methods. Actually, I've never used the second method before. @Atomheartfather I'm not sure I understand what you mean. Can you provide an example? – Aify May 13 '15 at 20:49
  • @pranavj95 no, they are different. p2 will hold a reference to an instance of the subclass. – AtomHeartFather May 13 '15 at 20:49
  • As far as I know, I think that p2 can access subclasses method too, though I am not sure. The only difference which I know is that p2 will by default access Superclass method even if they are being overloaded in Subclass. Please correct me if I am wrong – pranavj95 May 13 '15 at 20:51
  • @Aify Subclss can override some of the methods defined in Superclass. If you invoke one of those on p2, the method from the subclass gets executed. – AtomHeartFather May 13 '15 at 20:51
  • Your p2 (Subclass) object can't access Subclass methods if you're using a Superclass reference. And Atom, yes - I'll edit my answer to differentiate between pure superclass/sublcass methods and add a point on overriding methods. – Aify May 13 '15 at 20:53
  • @pranavj95 Java is polymorphic, hence the overridden method (the one from the Subclass will be executed). – AtomHeartFather May 13 '15 at 20:54
1

The first and the last are just usual variable declaration and class instantiation.

The second

Superclass p2 = new Subclass();

declares a variable p2 of type Superclass that can either store a Superclass instance or any instance of a subclass of Superclass. From p2 you can only access members that are already provided in Superclass, although their behavior can be different due to overriding in Subclass.

Also, you can check what actual type is stored in p2 by using instanceof. If you know the actual type, you could cast p2 to another variable of the sub-class type.

if (p2 instanceof Subclass)
    Subclass p4 = (Subclass)p2;
adjan
  • 13,371
  • 2
  • 31
  • 48
  • p2 could access the method provided in the subclass and not in it's super class..Please correct me if I'm wrong.. – Tom Taylor Jan 16 '18 at 08:00