"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

What is listnode in java?

0 Answers  


What is ternary operator?

0 Answers  


where to use join method and explain with real time senario?and programatical explenation also..

1 Answers   TCS,


how to convert Java project into Maven ?

0 Answers   Infosys,


What is a constructor overloading in java?

0 Answers  






Write down program for following scenario. Use java coding standard. You have array list with some words in it..we will call it as dictionary….and you have a arbitrary string containing some chars in it. You have to go through each word of dictionary and find out if that word can be constructed with the help of chars from arbitrary string given. If you find the word print it else print none.

0 Answers   Rolta,


What is the difference between Trusted and Untrusted Applet ?

2 Answers   IBM,


What is %02d?

0 Answers  


Is java hashset ordered?

0 Answers  


Explain what are final variable in java?

0 Answers  


Is passing by reference faster?

0 Answers  


What is null data type?

0 Answers  


Categories