Unit VI
Interfaces: Multiple Inheritance in
Java
Unit contents
• Defining interfaces
• Extending interfaces
• Implementing interfaces
• Accessing interface variables
INTRODUCTION TO INTERFACE
• C++ implemented the multiple inheritance but java
cannot.
• Java dose not support multiple inheritance.
• That means java cannot have more than one superclass.
• For instance consider following example:
Class A extends B extends C //this is not permitted in java
{…………..
………..}
• That’s why java provides an alternative approach known as
INTERFACES to support the concept of multiple inheritance
HOW TO DO MULTIPLE INHERITANCE IN JAVA?
 Actually, java does not support multiple inheritance
 However, you can achieve partial multiple inheritance with the
help of interfaces.
Ex: public class FerrariF12011 extends Ferrari implements Car,
Automobile {…}
And this is under the assumption that Car and Automobile are
interfaces.
INTERFACES IN JAVA
 Interface is like classes.
 An interface is a contain only abstract methods and final fields.
 abstract methods = Methods that are declared, with no implementation
 abstract class = A class with abstract methods, not meant to be instantiated
 interface = A named collection of method definitions (without
implementations)
 This is means interface do not specify any code to implement these
method and data field contains only constant.
 It is the responsibility of class that implements an interface to define
the code for implementation of these method.
Continued ……..
It allows Java to implement Multiple Inheritance,
because a class can't have more than one superclass in
Java, but can implements many interfaces.
Methods are just declared in interface, but not defined.
The class which implements an interface must have to
define the method declared in the interface.
Access modifiers and return type must be same as
declared in the interface.
Private and static methods can't be declared in the
interface.
WHY DOESN'T JAVA ALLOW MULTIPLE INHERITANCE?
Let us say the Automobile Class has a drive() method and the Car class
has a drive() method and the Ferrari class has a drive() method too.
Let us say we create a new class FerrariF12011 that looks like below:
Public class FerrariF12011 extends Ferrari, Car, Automobile {…}
And at some point of time we need to call the drive() method, what
would happen?
Our JVM wouldn't know which method to invoke and we may have to
instantiate one of the classes that you already inherit in order to call its
appropriate method.
To avoid this why the creators of java did not include this direct multiple
inheritance feature.
Syntax for declaring interface
Interface InterfaceName
{
variable declaration;
methods declaration;
}
Interface is key word and InterfaceName is any
valid java variable(just like class name)
Variable declaration
• Syntax:
Static final type VariableName = values ;
Methods without any body statement :
• Syntax :
return-type methodName1(parameter_List);
EXAMPLE
interface Suzuki
{
public abstract void body();
}
interface Ford
{
public abstract void engine();
}
public class MotorCar implements Suzuki, Ford
{
public void body()
{
System.out.println("Fit Suzuki body");
}
Suzuki
MotorCar
Ford
CONT..
public void engine()
{
System.out.println("Fit Ford engine");
}
public static void main(String args[])
{
MotorCar mc1 = new MotorCar();
mc1.body();
mc1.engine();
} }
Class Interface
The member of class can be constant
or variable
The member of interface are always
declared as constant i.e their values
are final
The class definition can contain the
code for each of its methods. That is
the methods can be abstract or non-
abstract .
The method in an interface is abstract
in nature i.e there is no code
associated with them. It is later
defined by class that implemented the
interface
It can be instantiated by declaring
objects .
It cannot be used to declare object . It
can only be inherited .
It can use various access specifies like
public,private,protected .
It can only use the public access
specifies.
How it works???
In the above code there are two interfaces – Suzuki and
Ford.
Both are implemented by MotorCar because it would like to
have the features of both interfaces
 Just to inform there are multiple interfaces and not classes,
tell the compiler by replacing "extends" with "implements”.
 MotorCar, after implementing both the interfaces, overrides
the abstract methods of the both – body() and engine(); else
program does not compile.
Extending Interface
• Interface also can be extended .
• This is an interface can be sub interfaced from other interface.
• The new subinterface will inherit all members of the
superinterface in the similar to subclass.
• This is done using keyword extends
• Syntax:
Interface name2 extends name1
{
body of name2
}
Example of extending interface
Interface constant
{
int code=1001;
String name=“div D”;
}
Interface div_D extends constant
{
void display();
}
 Interface div_D inherites both code and name
Implementing Interfaces
• Interface are used as “superclasses” whose properties are inherited by class.
• It is therefore necessary to create a class that inherits the given interface.
• This is done as follows
• Syntax:
Class classname implements interfacename
{
body of classname
}
• Classname “implements” the interface interfacename
• Following are the more general form of implementation is:
Class classname extends superclass implements interface1, interface2…….
{
body of classname;
}
Interface Area
{
Final static float pi=3.14f;
Float compute(float x, float );
}
Class rectangle implements Area
{
Public float compute(float x, float y);
{
return (x*y);
}
}
Class circle implements Area
{
Public float compute(float x, float y);
{
return (pi*x*x);
}
}
Class interface test
{
public static vid main(String srgs[])
{
rectangle rect =new rectangle();
circle cir = new circle();
Area= area; //interface obect
area=rect; //area ref. to rect
object
system.out.println(“area of rectangle
is”+area.compute(10,20));
area=cir; //area ref. to cir objects
system.out.println(“area of circle
is”+area.compute(10,0));
}
}
Accessing interface variables
Interface A
{
int m=10;
Int n=50;
}
Class B implements A
{
int x=m;
void method B(int size)
{
…………………………………
………………………………….
If (size <n)
}
}
Difference between Interface and abstract
class
Interface Abstract Class
Multiple inheritance A class may inherit several interfaces. A class may inherit only one
abstract class.
Default
implementation
An interface cannot provide any code, just
the signature.
An abstract class can provide
complete, default code and/or
just the details that have to be
overridden.
Access Modifiers An interface cannot have access modifiers
for the subs, functions, properties etc
everything is assumed as public.
An abstract class can contain
access modifiers for the subs,
functions, properties.
Core vs. Peripheral Interfaces are used to define the peripheral
abilities of a class. In other words both
Human and Vehicle can inherit from a
IMovable interface.
An abstract class defines the core
identity of a class and there it is
used for objects of the same
type.
Homogeneity If various implementations only share
method signatures then it is better to use
Interfaces.
If various implementations are of
the same kind and use common
behavior or status then abstract
class is better to use.
Interface Abstract Class
Speed Requires more time to find the actual
method in the corresponding classes.
Fast
Adding functionality If we add a new method to an Interface
then we have to track down all the
implementations of the interface and
define implementation for the new
method.
If we add a new method to an
abstract class then we have the
option of providing default
implementation and therefore
all the existing code might work
properly.
Fields and Constants No fields can be defined in interfaces. An abstract class can have fields
and constants defined.
Terseness The constant declarations in an
interface are all presumed public static
final.
Shared code can be added into
an abstract class.
Constants Static final constants only, can use them
without qualification in classes that
implement the interface.
Both instance and static
constants are possible. Both
static and instance initialiser
code are also possible to
compute the constants.
Third Party Convenience An interface implementation may be
added to any existing third party class.
A third party class must be
rewritten to extend only from
the abstract class.
Interface Abstract Class
Multiple inheritance A class may inherit several
interfaces.
A class may inherit only one
abstract class.
Default implementation An interface cannot provide any
code, just the signature.
An abstract class can provide
complete, default code and/or
just the details that have to be
overridden.
Access Modifiers An interface cannot have access
modifiers for the subs, functions,
properties etc everything is
assumed as public.
An abstract class can contain
access modifiers for the subs,
functions, properties.
Core vs. Peripheral Interfaces are used to define the
peripheral abilities of a class. In
other words both Human and
Vehicle can inherit from a
IMovable interface.
An abstract class defines the
core identity of a class and ther
it is used for objects of the same
type.
Homogeneity If various implementations only
share method signatures then it
is better to use Interfaces.
If various implementations are
of the same kind and use
common behavior or status the
abstract class is better to use.
Speed Requires more time to find the
actual method in the
corresponding classes.
Fast
Adding functionality If we add a new method to an
Interface then we have to track
down all the implementations of
If we add a new method to an
abstract class then we have the
option of providing default
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx

java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx

  • 1.
    Unit VI Interfaces: MultipleInheritance in Java
  • 2.
    Unit contents • Defininginterfaces • Extending interfaces • Implementing interfaces • Accessing interface variables
  • 3.
    INTRODUCTION TO INTERFACE •C++ implemented the multiple inheritance but java cannot. • Java dose not support multiple inheritance. • That means java cannot have more than one superclass. • For instance consider following example: Class A extends B extends C //this is not permitted in java {………….. ………..} • That’s why java provides an alternative approach known as INTERFACES to support the concept of multiple inheritance
  • 4.
    HOW TO DOMULTIPLE INHERITANCE IN JAVA?  Actually, java does not support multiple inheritance  However, you can achieve partial multiple inheritance with the help of interfaces. Ex: public class FerrariF12011 extends Ferrari implements Car, Automobile {…} And this is under the assumption that Car and Automobile are interfaces.
  • 5.
    INTERFACES IN JAVA Interface is like classes.  An interface is a contain only abstract methods and final fields.  abstract methods = Methods that are declared, with no implementation  abstract class = A class with abstract methods, not meant to be instantiated  interface = A named collection of method definitions (without implementations)  This is means interface do not specify any code to implement these method and data field contains only constant.  It is the responsibility of class that implements an interface to define the code for implementation of these method.
  • 6.
    Continued …….. It allowsJava to implement Multiple Inheritance, because a class can't have more than one superclass in Java, but can implements many interfaces. Methods are just declared in interface, but not defined. The class which implements an interface must have to define the method declared in the interface. Access modifiers and return type must be same as declared in the interface. Private and static methods can't be declared in the interface.
  • 7.
    WHY DOESN'T JAVAALLOW MULTIPLE INHERITANCE? Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say we create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…} And at some point of time we need to call the drive() method, what would happen? Our JVM wouldn't know which method to invoke and we may have to instantiate one of the classes that you already inherit in order to call its appropriate method. To avoid this why the creators of java did not include this direct multiple inheritance feature.
  • 8.
    Syntax for declaringinterface Interface InterfaceName { variable declaration; methods declaration; } Interface is key word and InterfaceName is any valid java variable(just like class name)
  • 9.
    Variable declaration • Syntax: Staticfinal type VariableName = values ; Methods without any body statement : • Syntax : return-type methodName1(parameter_List);
  • 10.
    EXAMPLE interface Suzuki { public abstractvoid body(); } interface Ford { public abstract void engine(); } public class MotorCar implements Suzuki, Ford { public void body() { System.out.println("Fit Suzuki body"); } Suzuki MotorCar Ford
  • 11.
    CONT.. public void engine() { System.out.println("FitFord engine"); } public static void main(String args[]) { MotorCar mc1 = new MotorCar(); mc1.body(); mc1.engine(); } }
  • 12.
    Class Interface The memberof class can be constant or variable The member of interface are always declared as constant i.e their values are final The class definition can contain the code for each of its methods. That is the methods can be abstract or non- abstract . The method in an interface is abstract in nature i.e there is no code associated with them. It is later defined by class that implemented the interface It can be instantiated by declaring objects . It cannot be used to declare object . It can only be inherited . It can use various access specifies like public,private,protected . It can only use the public access specifies.
  • 13.
    How it works??? Inthe above code there are two interfaces – Suzuki and Ford. Both are implemented by MotorCar because it would like to have the features of both interfaces  Just to inform there are multiple interfaces and not classes, tell the compiler by replacing "extends" with "implements”.  MotorCar, after implementing both the interfaces, overrides the abstract methods of the both – body() and engine(); else program does not compile.
  • 14.
    Extending Interface • Interfacealso can be extended . • This is an interface can be sub interfaced from other interface. • The new subinterface will inherit all members of the superinterface in the similar to subclass. • This is done using keyword extends • Syntax: Interface name2 extends name1 { body of name2 }
  • 15.
    Example of extendinginterface Interface constant { int code=1001; String name=“div D”; } Interface div_D extends constant { void display(); }  Interface div_D inherites both code and name
  • 16.
    Implementing Interfaces • Interfaceare used as “superclasses” whose properties are inherited by class. • It is therefore necessary to create a class that inherits the given interface. • This is done as follows • Syntax: Class classname implements interfacename { body of classname } • Classname “implements” the interface interfacename • Following are the more general form of implementation is: Class classname extends superclass implements interface1, interface2……. { body of classname; }
  • 17.
    Interface Area { Final staticfloat pi=3.14f; Float compute(float x, float ); } Class rectangle implements Area { Public float compute(float x, float y); { return (x*y); } } Class circle implements Area { Public float compute(float x, float y); { return (pi*x*x); } } Class interface test { public static vid main(String srgs[]) { rectangle rect =new rectangle(); circle cir = new circle(); Area= area; //interface obect area=rect; //area ref. to rect object system.out.println(“area of rectangle is”+area.compute(10,20)); area=cir; //area ref. to cir objects system.out.println(“area of circle is”+area.compute(10,0)); } }
  • 18.
    Accessing interface variables InterfaceA { int m=10; Int n=50; } Class B implements A { int x=m; void method B(int size) { ………………………………… …………………………………. If (size <n) } }
  • 19.
    Difference between Interfaceand abstract class Interface Abstract Class Multiple inheritance A class may inherit several interfaces. A class may inherit only one abstract class. Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden. Access Modifiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public. An abstract class can contain access modifiers for the subs, functions, properties. Core vs. Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and there it is used for objects of the same type. Homogeneity If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
  • 20.
    Interface Abstract Class SpeedRequires more time to find the actual method in the corresponding classes. Fast Adding functionality If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. Fields and Constants No fields can be defined in interfaces. An abstract class can have fields and constants defined. Terseness The constant declarations in an interface are all presumed public static final. Shared code can be added into an abstract class. Constants Static final constants only, can use them without qualification in classes that implement the interface. Both instance and static constants are possible. Both static and instance initialiser code are also possible to compute the constants. Third Party Convenience An interface implementation may be added to any existing third party class. A third party class must be rewritten to extend only from the abstract class.
  • 24.
    Interface Abstract Class Multipleinheritance A class may inherit several interfaces. A class may inherit only one abstract class. Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden. Access Modifiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public. An abstract class can contain access modifiers for the subs, functions, properties. Core vs. Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and ther it is used for objects of the same type. Homogeneity If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behavior or status the abstract class is better to use. Speed Requires more time to find the actual method in the corresponding classes. Fast Adding functionality If we add a new method to an Interface then we have to track down all the implementations of If we add a new method to an abstract class then we have the option of providing default