"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

when i write string s1="java"; in one program(application) and string s2="java"; in another application on the same jvm will both objects s2,s2 will refer to same memory location where "java" is stored in string pool.

2 Answers  


What is the difference between superclass and subclass?

0 Answers  


What is Java Package and which package is imported by default?

0 Answers  


What is the function of http?

0 Answers  


What is a boolean structure?

0 Answers  


what is Hashmap & Hashtable wirh example?

5 Answers   CTS, Saksoft,


How many types of design patterns are there?

0 Answers  


What is a Wrapper class?

14 Answers  


Why do we need singleton?

0 Answers  


how to handle exceptions in ejb?

1 Answers   HCL,


Hi buddy, well i got that there is always a default constructor with abstract class. OK. But why not with interface? Thanks in advance.

2 Answers  


What is compareto?

0 Answers  


Categories