Q)What is an implementation class?
=>Child class(sub class) of an interface is nothing but an implementation class.
=>Implementation class should implement/override all the abstract methods of the
interface.
=>An implementation class can inherit from any number of interfaces.
For eg.
interface I1{
}
interface I2{
}
class A implements I1,I2{
}
Q)Can a class inherit from more than one interface?
=>Yes.
=>But a class can inherit from only one class.
Q)Can a class inherit from a class and an interface at the same time?
=>Yes.
For eg.
interface I1{
}
interface I2{
}
class B{
}
class A extends B implements I1,I2{
}
Q)What is wrong with the following code?
interface I{
int a;
void y(){
}
protected void z();
}
Ans:- An interface can have only constants. Therefore, "a" should be initialized.
=>interface methods can’t have body.
=>"protected" modifier is not allowed for the members of an interface.
interface I{
int a=30;
void y();
public void z();
}
Q)What is wrong with the following code?
interface I{
void x();
}
class A implements I{
}
Ans:- Either declare class A abstract as x() method is not implemented in class A.
=>Or, implement x() method in class A.
=>Child class(sub class) of an interface is nothing but an implementation class.
=>Implementation class should implement/override all the abstract methods of the
interface.
=>An implementation class can inherit from any number of interfaces.
For eg.
interface I1{
}
interface I2{
}
class A implements I1,I2{
}
Q)Can a class inherit from more than one interface?
=>Yes.
=>But a class can inherit from only one class.
Q)Can a class inherit from a class and an interface at the same time?
=>Yes.
For eg.
interface I1{
}
interface I2{
}
class B{
}
class A extends B implements I1,I2{
}
Q)What is wrong with the following code?
interface I{
int a;
void y(){
}
protected void z();
}
Ans:- An interface can have only constants. Therefore, "a" should be initialized.
=>interface methods can’t have body.
=>"protected" modifier is not allowed for the members of an interface.
interface I{
int a=30;
void y();
public void z();
}
Q)What is wrong with the following code?
interface I{
void x();
}
class A implements I{
}
Ans:- Either declare class A abstract as x() method is not implemented in class A.
=>Or, implement x() method in class A.