Search code examples
javaclassinterfaceinstanceparent-child

Meaning of syntax of new object creation of a child class in Java?


I have below java program which i wrote while going through a java tutorial.

public class Primitive_prac {

   public static void main(String[] args) {
    // TODO code application logic here

    Parent obj1 = new Child();
    Child obj2 = new Child();

    System.out.println("ojb1 is instance of Parent: " + (obj1 instanceof Parent));
    System.out.println("ojb1 is instance of Parent: " + (obj1 instanceof Child));
    System.out.println("ojb1 is instance of Parent: " + (obj1 instanceof Interface1));

    System.out.println();

    System.out.println("ojb2 is instance of Parent: " + (obj2 instanceof Parent));
    System.out.println("ojb2 is instance of Parent: " + (obj2 instanceof Child));
    System.out.println("ojb2 is instance of Parent: " + (obj2 instanceof Interface1));

    }
}

class Parent{}
interface Interface1{}
class Child extends Parent implements Interface1{}

As you can see I have created a class Parent, an interface Interface1, and a subclass Child which extends Parent and implements Interface1.

However, I have a doubt in below statement:

 Parent obj1 = new Child();

In this case obj1 is an instance of Parent or Child ? What is the meaning of the above statement really ? What is the meaning of Parent obj1 which is on the left side of = and what is the meaning of new Child()which is on the right side of =. And what is the meaning of Parent obj1 = new Child(); as a statement ?

Below is the output of the program.

run:
ojb1 is instance of Parent: true
ojb1 is instance of Parent: true
ojb1 is instance of Parent: true

ojb2 is instance of Parent: true
ojb2 is instance of Parent: true
ojb2 is instance of Parent: true
BUILD SUCCESSFUL (total time: 0 seconds)

I got kind of confused because the output shows that both obj1 and obj2 are instances of Parent, Child and Interface1. Didnt the Parent word while creating obj1 make any difference ? What was the significance of writing Parent while creating obj1 ?

I found it hard to express in words but i hope you get my question.

Thanks.


Solution

  • In this case obj1 is an instance of Parent or Child ?

    It is an instance of Child, and an instance of Parent.

    What is the meaning of the above statement really ?

    That obj1's a child, as you defined it all instances of Child are also instances of Parent, so obj1 is also an instance of parent.

    What is the meaning of Parent obj1 which is on the left side of = and what is the meaning of new Child()which is on the right side of =. And what is the meaning of Parent obj1 = new Child(); as a statement ?

    Parent obj1 means that obj1 is a local variable that can hold any object that is an instance of Parent. The fact that you construct a Child means that it can in fact be assigned, since all Child objects are Parents.

    Didnt the Parent word while creating obj1 make any difference ?

    Yes, if you add a method to Child but not to Parent you won't be able to call it on obj1 since obj1 could hold any Parent, not just one that is a Child.

    What was the significance of writing Parent while creating obj1 ?

    It allows any Parent to be stored, even one that isn't a Child.

    Perhaps the following naming of classes (as an analogy) might make more sense:

    public class Primitive_prac {
    
       public static void main(String[] args) {
        // TODO code application logic here
    
        Animal obj1 = new Bird();
        Bird obj2 = new Bird();
        obj1.move(); //valid, since obj1 is declared `Animal`
        obj1.fly(); // invalid, since obj1 could be any animal and not all animals implement fly();
        obj2.move(); // Valid, all birds are animals and all animals (as defined here) move, therefore birds can move
        obj2.fly(); //valid, obj2 can only be a bird or a subclass, and all such objects can fly()
    
        System.out.println("ojb1 is instance of Animal: " + (obj1 instanceof Animal));
        System.out.println("ojb1 is instance of Bird: " + (obj1 instanceof Bird));
        System.out.println("ojb1 is instance of Flying: " + (obj1 instanceof Flying));
    
        System.out.println();
    
        System.out.println("ojb2 is instance of Bird: " + (obj2 instanceof Animal));
        System.out.println("ojb2 is instance of Animal: " + (obj2 instanceof Bird));
        System.out.println("ojb2 is instance of Flying: " + (obj2 instanceof Flying));
    
        }
    }
    
    class Animal{ void move("Moving...") {} }
    interface Flying{ void fly(); }
    class Bird extends Animal implements Flying{
        void fly() {System.out.println("flap flap");}
    }