can interface have objects in java

Solutions on MaxInterview for can interface have objects in java by the best coders in the world

showing results for - "can interface have objects in java"
Emie
20 Oct 2018
1No, you can never instantiate an interface in java. You can, however, refer to an object that implements an interface by the type of the interface.
2
3interface Animal{
4
5void eat();
6
7}
8
9You cannot do this “Animal animal = new Animal();” Bcoz ininterface” class don’t have Constructor .
10
11Now consider.
12
13class Lion implements Animal
14
15{
16
17@Override
18
19void eat(){ System.out.println(“I eat only non-veg”);}
20
21}
22
23class Test(){
24
25Animal animal = new Lion();
26
27animal.eat();
28
29}
30
31Object of “Lion” is created and “Animal” hold the reference(address) of the Lion’s object.
similar questions
queries leading to this page
can interface have objects in java