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

Answers were Sorted based on User's Feedback



"we cannot create an object of interface but we can create a variable of it".diacuss th..

Answer / guest

1. Suppose interface interfaceDemo.java is as below

interface interfaceDemo {
public void methodA();
public void methodB();
}

2. Suppose class InterfaceDemoMain.java is as below

class InterfaceDemoMain implements interfaceDemo {
public void methodA() {
System.out.println("Inside methodA");
}

public void methodB() {
System.out.println("Inside methodB");
}

public void methodC() {
System.out.println("Inside methodC");
}

public static void main(String[] ar) {
InterfaceDemoMain idm = new InterfaceDemoMain();
interfaceDemo id;
id = idm;
id.methodA();
id.methodB();
id.methodC();// error
idm.methodC();
}
}

3. Here id.methodC(); will give error, only methods declared
inside interface are accessible to interface reference.

Is This Answer Correct ?    13 Yes 0 No

"we cannot create an object of interface but we can create a variable of it".diacuss th..

Answer / ami singh

yes we can create a variable in inerface
and i will be constant
mans to say that hey will be static and final
inerface Xyz
{
public static final i = 12;
public static final j = 20;
}
class Amit implements Xyz
{
psvm(String [] args)
{
Xyz k = new Amit();
System.out.println("value of var1 " + k.i);
Sysem.out.prinln("value ofd var 2 " + k.j);
}
}
Thanks ami singh
(amitsing2008@gmail.com)

Is This Answer Correct ?    11 Yes 0 No

Post New Answer

More Core Java Interview Questions

program to find 25 square = 625 here the 625 of last two digits is equal to 25, i don't know excatly what this type of number is called

1 Answers   Huawei,


What is adapter in java?

0 Answers  


What is the difference between the synchronized() & static synchronized()?

2 Answers   HP, SparkTG,


Can we call a non-static method from inside a static method?

0 Answers  


What is yielding and sleeping? how they different?

2 Answers  






What is the name of the java compiler?

0 Answers  


State differences between C and Java?

0 Answers   Deloitte, JPMorgan Chase,


Explain the difference between hashmap and hashtable in java?

0 Answers  


What is the use of pattern in java?

0 Answers  


Can an exception be rethrown?

4 Answers   Wipro,


Can we call thread start () twice?

0 Answers  


What will happen if static modifier is removed from the signature of the main method?

0 Answers  


Categories