sadegh aliakbary sharif university of technology fall 2010
TRANSCRIPT
Advanced Programming in Java
Sadegh AliakbarySharif University of Technology
Fall 2010
Sharif University of Technology 2
AgendainterfaceMultiple Inheritance
Fall 2010
Sharif University of Technology 3
Review : AbstractAbstract Methods
No ImplementationSub-classes may implement abstract methods
Abstract ClassesA class with one or more abstract methodsCan not be instantiated
A class which extends an abstract classIs abstract unless it implements all the abstract
methodsConcrete class Not abstract class
Fall 2010
Sharif University of Technology 4
Abstract Example
Fall 2010
Sharif University of Technology 5
Abstract MethodAll subclasses have the methodBut we can not implement the method in
super-classSo why we define it in super-class?
Polymorphism
Fall 2010
Sharif University of Technology 6
InterfaceSometimes we have an abstract class, with no
concrete methodinterface : pure abstract classno implemented method
Fall 2010
Sharif University of Technology 7
Interface
Fall 2010
Sharif University of Technology 8
InterfaceAll the methods are implicitly abstract
No need to abstract specifier
All the methods are implicitly publicNo need to public specifier
Fall 2010
Sharif University of Technology 9
Interface ImplementationSome classes may inherit abstract classesSome classes may implement interfaces
Is-a relation existsIf a class implements an interfaceBut does not implement all the methodsWhat will happen?
The class became an abstract class
Fall 2010
Sharif University of Technology 10Fall 2010
Sharif University of Technology 11
Multiple Inheritance in JavaA class can inherit one and only one classA class may implement zero or more
interfaces
Fall 2010
Sharif University of Technology 12Fall 2010
Sharif University of Technology 13
A QuestionWhy multiple inheritance is not supported for
classes?Why multiple inheritance is supported for
interfaces?
Fall 2010
Sharif University of Technology 14
What About Name Collision?
Fall 2010
The return types are incompatible for the inherited methods B.f(), A.f()
Sharif University of Technology 15Fall 2010
Sharif University of Technology 16
Interface ExtensionInterfaces may inherit other interfacesCode reuseIs-a relation
Fall 2010
Sharif University of Technology 17
Interface propertiesNo member variable
Variables : implicitly final and staticUsually interfaces declare no variable
Only operations are declaredNo constructor
Why?
Fall 2010
Sharif University of Technology 18
Example
Fall 2010
Sharif University of Technology 19
Interfaces ApplicationsPure Abstract classesDescribe the designMore powerful inheritance and polymorphism
Fall 2010
Sharif University of Technology 20
Quiz!
Fall 2010
Sharif University of Technology 21Fall 2010