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
How to split a string in java?
When throws keyword is used?
What kind of variables a class can consist of?
How define set in java?
Which keyword specify that a variable is effectively final ?
Why do I need to declare the type of a variable in java?
What language is an assembler written in?
What is polymorphism in java? What are the kinds of polymorphism?
When should a function throw an exception?
why are there separate wait and sleep methods? : Java thread
Explain inheritance in java?
We are seeing so many videos/audios as many web sited. But question is these videos or audios are stored in Databases ( Oracle, Mysql, Sybase,... ) or stored any file directory from there they will give the link for that? Pls explain and give sample code to achieve this one? Thanks, Seenu.
Can you override static methods?
What is class??
How to make a class or a bean serializable?