Composition Interfaces Polymorphism Inheritance in Java part 2 Multiple inheritance abstract class Electronics class clock extends Electronics class radio extends Electronics ID: 539716
Download Presentation The PPT/PDF document "Multiple inheritance" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Multiple inheritanceCompositionInterfacesPolymorphism
Inheritance in Java (part 2)Slide2
Multiple inheritanceabstract class Electronics { … }
class clock extends Electronics {
…
}class radio extends Electronics { …}class clock-radio extends clock, radioSlide3
Multiple InheritanceAllowed in C++ (1985)
Not Allowed in
Java (1995),
C# (2000)IssuesData in common base class is duplicated (i.e. Serial Number)Methods in base classes are overloaded (i.e.
powerUp
() )
Two
parent classes may have different implementations for the same
feature
(i.e.
makeLouder
() )Slide4
Composition in Javaclass Clock-Radio
extends
Electronics
{ …}{ Clock c = new Clock(); Radio r = new Radio();}Slide5
Rapper methods for composition
class clock extends Electronics
{
void setTime(int h, int m) { hours=h; mins=m;} void
setAlarm
(
int
h,
int
m) {
almH
= h;
almM
= m;}
}
class clock-radio extends
Electronics
{
Clock c = new Clock();
Radio r = new Radio();
void
setTime
(
int
h,
int
m) {
c.setTime
(
h,m
); }
}Slide6
Interfaces
An
interface is a description of a capability. It lists methods that a class must implement to carry out that capability.
A class may implement multiply interfaces.An interface may be implemented by many classes. Slide7
Example of an interface
interface
autoShutOff
{ boolean deviceTooHot(int
temperture
);
void
autoShutOff
();
}
class R
adio
extends
Electronics
implements
autoShutOff
()
{ ….
boolean
deviceTooHot
(
int
t)
{ if (temp > 98) {return true} else { return false; }
void
autoShutOff
{ if (
deviceTooHot
()) { … } }
}Slide8
Interfaces are like Classes
Is
complied into bytecode file, named
xyz.class public, protected, private or packageCannot be public unless same name as filenameServes as a type for declaring variablesSlide9
Interfaces are not like Classes…
Declares
only method
headers (no method body allowed)Cannot declare data storage, only public constants.Has no constructorsCannot be instantiatedCan be implemented by a classCannot implement an interface (but can extend an interface)Cannot extend a classCan extend several other interfacesSlide10
Java typesInterface
Class
Array
PrimitiveSlide11
Interface extends interface
i
nterface
abc{ // method signatures of abc}interface xyz extends abc
{
// method signatures of
xyz
}
class C
implements
xyz
{
//
bodies of
abc
and xyz methods
}Slide12
Multiple Interface examplei
nterface
abc
{ // method signatures of abc}interface xyz {
// method signatures of
xyz
}
class C
implements
abc
, xyz
{
//
bodies of
abc
and xyz methods
}Slide13
Ex) Java’s Comparable interfaceThe standard libraries in Java define over 600 interfaces
.
j
ava.lang.Comparable interface in one.It has one method int compareTo(Object o) { … }
Usage:
int
result =
x.compareTo
(y
)
Returns a negative integer when x < y
Returns 0 when x == y
Returns a positive number when x > y Slide14
Class object may need ordering
People, clock-radio, etc. do not have a natural ordering
(<
, == . > )class Student extends Person implements Comparable{ …
public
int
compareTo
(Object o)
{
Student that = (Student)o;
if (
this.age
<
that.age
) return -1;
if (
this.age
>
that.age
) return
1
;
return 0;
}
}Slide15
Polymorphism in JavaThe word ‘polymorphism’ literally means ‘a state of having many shapes’ or ‘the capacity to take on different forms
’.
An object may be referred to by a reference:
of it’s typeof any type it inherited fromof any interface it’s class implementsof any interface implemented by a class it inherits fromSlide16
Polymorphism of Classesabstract class Electronics
implements
powerOn
{ … }class clock extends Electronics { …}class radio extends Electronics { …}class clock-radio extends Electronics { …}Electronics
ec
=
new Clock();
Electronics
er
=
new Radio();
Electronics
ecr
= new Clock-Radio
();
PowerOn
po
=
ec
;
ec.powerUp
();
er.printSerialNumber
();
ecr.setTime
(3,20); // not allowedSlide17
Polymorphism of Interfaces
abstract class
Shapes implements
Drawables { … }class Circle extends Shapes { …}class Square extends Shapes { …}
Drawables
dc
= new
Circle();
Drawables
ds
= new
Square();
dc.draw
(); // calls the
drawable
method in Circle
d
s.draw
();
// calls the
drawable
method in
Circle
i
nt
a =
d
s.area
() (
3,20); // not allowed