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
What is the difference between throw and throws in java?
What is the difference between notify and notifyall method?
What is the difference between arraylist and hashset in java?
What is core java called?
Explain differences between checked and unchecked exceptions in java?
What is difference between Heap and Stack Memory?
What is currentthread()?
What does exclamation mean in java?
What is bytecode verifier?
What is the difference between superclass and subclass?
What is an argument in java?
What do you understand by copy constructor in java?
What is locale in java?
Explain the difference between private, public, package and protected in java?
What does isempty () do in java?