"We cannot create an object of interface but we can create a
variable of it". Discuss the statement with the help of an
example.

(Plz help us to provide immediately.)

Answers were Sorted based on User's Feedback



"We cannot create an object of interface but we can create a variable of it". Discuss the..

Answer / nick

In layman terms it can be said that a interface basicaly
provides a template. Since it does not have any concreet
functions we cannot create its object, as during
implemetation(runtime) there would be no definition found
for the particular function that would have been hardcoded
into the program. Where as using it as a refernce variable
is possible because of the technique provided by java
i.e. 'Virtual Method Invocation'. This enables us to create
a variable of a interface, but at runtime the function
defenition that would be called would be the one of the
object of the class actually being referred. It would get
clear from the following example:

interface Color
{
void green();
void red();
void blue();
}

here if we create a object of interface Color like

Color clr= new Color();
clr.green();

then it would give an error as there is no implementation
available for the function green();

whereas this senario is valid where we have another class

class Implemeter
{
public void green()
{
System.out.println("You selected green color");
}
public void red()
{
System.out.println("You selected red color");
}
}

and now we use like this

Color clr = new Implementer();
clr.green();

then this is absolutely valid as on runtime the function
definition of green() from class Implementer would be
called and not of interface Color. This technique is called
virtual method invocation.

Just let me know if this information was helpful, or if you
have any other doubts.

Is This Answer Correct ?    29 Yes 4 No

"We cannot create an object of interface but we can create a variable of it". Discuss the..

Answer / prashant sharma

1.Interface myInterface{....};
2.Class myClass{......}

3.myInterface vInterace;
4.vInterface = new myClass();

In line 3 we are creating a var of type myInterface. It
holds an memory space for var type myInterface. it is just
like any other var type. Why there can't be ann object of
interface ..ans to this lies in post 1

In line 4 we create a object of class myClass. This object
returns the reference of space hold by class object on heap.
Thanks to JAVA inheritence structure, now variable
vInterface can refer to object of myClass as they are
interrelated

Interrelated in the sense taht when a class implements an
interface its object create definition of the interface in
the object on heap.

Hope it helps....

Is This Answer Correct ?    4 Yes 0 No

"We cannot create an object of interface but we can create a variable of it". Discuss the..

Answer / simran

ur program defintly right but i cannot understand theory
write by u.

Is This Answer Correct ?    4 Yes 2 No

Post New Answer

More Core Java Interview Questions

what is ennumaration?

2 Answers   Amdocs,


Explain the overview of UDP messaging.

0 Answers  


What is the use of set in java?

0 Answers  


What are the different types of inner classes?

0 Answers  


What is the difference between static (class) method and instance method?

0 Answers  






what are the oops concept in java explain with real time examples

24 Answers   Accenture, Bosch, Consummate Technologies, CTS, Current Technologies, iNautix, Infosys, Kekran Mekran, QA InfoTech, RTRT, SofTech, Tech Mahindra, Thorogood, Timios, Wipro,


What is the method in java?

0 Answers  


Definition for connection pooling?

3 Answers  


Explain list interface?

0 Answers  


What is OOP Language?

0 Answers   Atos Origin,


What do you mean by ordered and sorted in collections in java?

0 Answers  


What is meant by final class, methods and variables?

3 Answers  


Categories