can you create interface instance ?
Answer Posted / yousif mustafa
I think UDAY is right and he gave the right answer.
We actually couldn't instantiate the interface but we could
instantiate object from class implements this interface and
override all of its methods due to interfaces could only
have abstract methods, but also we can use anonymous inner
class to do that and must override all the abstract method
within creating the object. for ex:
interface Test {
void printA();
void printB();
}
class Inter implements Test {
@Override
void printA() {System.out.println("A from implement");}
void printB() {System.out.println("B from implement");}
}
public class A {
public static void main (String arg[]) {
Test t = new Test() {
void printA() { System.out.println("A form inner"); }
void printB() { System.out.println("B from inner"); }
};
Inter i = new Inter();
t.printA();
i.printA();
t.printB();
i.printB();
}
}
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
Is integer passed by reference in java?
Under what conditions is an object’s finalize() method invoked by the garbage collector?
What is classes in java?
What are the advantages of exception handling in java?
What does the exclamation mark mean in java?
What is the this keyword?
Can we override singleton class?
How do you calculate square roots?
What is a static method in java?
What is the difference between length and length() method in java?
What is overloading and overriding in java?
Differentiate between class and structure.
What are keywords in programming?
How listener identify that the event came from a particular object?
What is java’s garbage collected heap?