Question

I have a program code like this.

My Interface is

public interface MyInterface {
        public void m1(String name);
        public void m2(int num);
}

I have a class that implements above Interface.

    public class World implements MyInterface {
        public void m1(String name) {
               System.out.println(name);
        }

        public void m2(int num){
              System.out.println("Number is: "+num);
        }

        public static void main(String args[]) {
                MyInterface ob1  = new World(); //How it is instantiated
                MyInterface ob2  = new World();   //How this one too is instantiated
                ob1.m1("Jaguar"); 
                ob2.m2(5);

        }
}
Was it helpful?

Solution

In java when we write some thing as

Class1 a= new Class2();

what exactly happens is we are calling the constructor of class2 , which means we are creating a object on the heap of class2 and assigning it to reference variable of type class1 (i.e a). Think of the left part as a remote control and the right part as tv. The left part only control the functions that can be called on the object created in the right part (Since there maybe more functions in the implementing class than declared in the interface).

If we write something like this :-

Class1 a= new Class1();

Both reference and object variables are of type Class1 and hence all methods of Class1 can be called.

If we write something like this:-

Class1 a= new Class2();

All methods of Class2 will be called but restricting to those declared in Class1.

Therefore your inteface is never being instantiated and it cant , Its just providing a type of restriction to the objects created (ob1 and ob2) to call only the two methods m1 and m2 on the objects no matter how many functions are extra added in the World class.

OTHER TIPS

Thing is Interface can not be instantiated directly whether it is done by its implemented classes. Your code is perfectly instantiating and assigning to interface.

Actually here you are instantiating the Object of 'class World' ( with default constructor ) and refering it with the reference of 'MyInterface'.. can be refer as Upcasting , link below will be more helpful in understanding.

http://www.coderanch.com/t/513479/java-programmer-SCJP/certification/Upcasting-Class-interface

   MyInterface ob1  = new World(); //How it is instantiated

and it is perfectly valid. But instantiating an interface is not possible like,

   INVALID XXXX---> MyInterface obj = new MyInterface(); <----XXXX INVALID 

Yes, there is an implicit argument-less constructor that is added to a class when no other constuctor is explicitly declared.

Technically you cannot instantiate any object of an interface since it is an abstract class. But you can instantiate any concrete class that implements it. So as Abimaran Kugathasan wrote MyInterface ob1 = new World(); is valid but

MyInterface ob1 = new MyInterface(); 

is not. Of course

World obj2 = new World();

is also valid but uses a narrower variable (containing objects of class World or of any of its subclasses)

first thing to note - interface will never ever have constructor or any methods defined inside it.

so we can never ever create an object of interface.

but we can use interface varibale to hold its child object.

 MyInterface ob1  = new World(); //How it is instantiated

the leftside ob1 is a reference variable not an Object, and its a reference variable of interface.

in java ,upcasting(assigning child object to parent's reference variable) is legal and allowed

so above code works fine.

now will see how its instantiated.

right side we are calling new World() means World class default constrcutor.

Note: when there is no constructor is defined in a class then by default no arg constrcutor will be defined. and if any other constrcuotr is alrady defined then it wont define default no arg constrctor.

so in your case it is using default no arg constrcutor and creating an object of World not interface. and assigning it to interface variable.

so answer to your question is "Nooooooooooooo".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top