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 actual and formal parameters?
What is finally block?
How do you sort a string in alphabetical order in java?
What do you understand by Header linked List?
When super keyword is used?
Is java jre still free?
What is final class?
What is OOP's Terms with explanation?
What is a void in java?
What are the 5 types of research methods?
What is a default constructor and also define copy contrucyor?
Explain the difference between string, stringbuffer and stringbuilder in java?
What is prefix of a string?
Are arrays passed by reference in java?
What are three advantages of using functions?