Download presentation
Presentation is loading. Please wait.
Published byPaula Booker Modified over 8 years ago
1
Multiple inheritance Composition Interfaces Polymorphism Inheritance in Java (part 2)
2
Multiple inheritance abstract class Electronics { … } class clock extends Electronics { …} class radio extends Electronics { …} class clock-radio extends clock, radio
3
Multiple Inheritance Allowed in C++ (1985) Not Allowed in Java (1995), C# (2000) Issues Data 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() )
4
Composition in Java class Clock-Radio extends Electronics { …} { Clock c = new Clock(); Radio r = new Radio(); }
5
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); } }
6
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.
7
Example of an interface interface autoShutOff { boolean deviceTooHot(int temperture); void autoShutOff(); } class Radio extends Electronics implements autoShutOff() { …. boolean deviceTooHot(int t) { if (temp > 98) {return true} else { return false; } void autoShutOff { if (deviceTooHot()) { … } } }
8
Interfaces are like Classes Is complied into bytecode file, named xyz.class public, protected, private or package Cannot be public unless same name as filename Serves as a type for declaring variables
9
Interfaces are not like Classes… Declares only method headers (no method body allowed) Cannot declare data storage, only public constants. Has no constructors Cannot be instantiated Can be implemented by a class Cannot implement an interface (but can extend an interface) Cannot extend a class Can extend several other interfaces
10
Java types Interface Class Array Primitive
11
Interface extends interface interface abc { // method signatures of abc } interface xyz extends abc { // method signatures of xyz } class C implements xyz { // bodies of abc and xyz methods }
12
Multiple Interface example interface abc { // method signatures of abc } interface xyz { // method signatures of xyz } class C implements abc, xyz { // bodies of abc and xyz methods }
13
Ex) Java’s Comparable interface The standard libraries in Java define over 600 interfaces. java.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
14
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; }
15
Polymorphism in Java The 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 type - of any type it inherited from - of any interface it’s class implements - of any interface implemented by a class it inherits from
16
Polymorphism of Classes abstract 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 allowed
17
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 ds.draw(); // calls the drawable method in Circle int a = ds.area() (3,20); // not allowed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.