Multiple Inheritance is not allowed in JAVA,so we will use interface to overcome the Inheritance, if we use inheritance in java the programmer becomes confuse that from which class member is taken,Eg:If class A has a member ‘r’ and B also has a member ‘r’,if another class C extends both the classes then which copy of ‘r’ will be available in C, so we will use interface.
Interface uses implements keyword, inheritance uses extends keyword.
Here is an example:
interface Stallion // Adult Male horse
{
Float HT=6.8f;
void height();
}
interface Dam //Adult Female horse
{
Float HT=5.2f;
void height();
}
class Colt implements Stallion,Dam // Colt Young male horse
{
public void height()
{
float ht=(Colt.HT+Filly.HT)/2;
System.out.print("Height of Colt is :"+ht);
}
}
class MultipleInheritance
{
public static void main(String args[])
{
Colt ch=new Colt ();
ch.height();
}
}
O/P: Height of Colt is :6.0
No comments:
Post a Comment