"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.)
Answer Posted / 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 |
Post New Answer View All Answers
Are strings immutable in java?
How do you remove an object from an arraylist in java?
What is structure of java heap? What is perm gen space in heap?
When a lot of changes are required in data, which one should be a preference to be used? String or stringbuffer?
Is heap stored in ram?
Explain constructors and types of constructors in java.
If we don’t want some of the fields not to serialize how to do that?
How do you check if two given string are anagrams?
Why declare Main() method as a static in java ?
why Interface used?
What is a parent class in java?
Can we write multiple catch blocks under single try block?
Can a static class have a constructor?
What is getclass () getname () in java?
When will you define a method as static in Java?