sadegh aliakbary sharif university of technology fall 2010

21
Advanced Programming in Java Sadegh Aliakbary Sharif University of Technology Fall 2010

Upload: sherilyn-lester

Post on 18-Dec-2015

223 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Sadegh Aliakbary Sharif University of Technology Fall 2010

Advanced Programming in Java

Sadegh AliakbarySharif University of Technology

Fall 2010

Page 2: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 2

AgendainterfaceMultiple Inheritance

Fall 2010

Page 3: Sadegh Aliakbary Sharif University of Technology 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

Page 4: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 4

Abstract Example

Fall 2010

Page 5: Sadegh Aliakbary Sharif University of Technology 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

Page 6: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 6

InterfaceSometimes we have an abstract class, with no

concrete methodinterface : pure abstract classno implemented method

Fall 2010

Page 7: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 7

Interface

Fall 2010

Page 8: Sadegh Aliakbary Sharif University of Technology 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

Page 9: Sadegh Aliakbary Sharif University of Technology 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

Page 10: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 10Fall 2010

Page 11: Sadegh Aliakbary Sharif University of Technology Fall 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

Page 12: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 12Fall 2010

Page 13: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 13

A QuestionWhy multiple inheritance is not supported for

classes?Why multiple inheritance is supported for

interfaces?

Fall 2010

Page 14: Sadegh Aliakbary Sharif University of Technology 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()

Page 15: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 15Fall 2010

Page 16: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 16

Interface ExtensionInterfaces may inherit other interfacesCode reuseIs-a relation

Fall 2010

Page 17: Sadegh Aliakbary Sharif University of Technology 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

Page 18: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 18

Example

Fall 2010

Page 19: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 19

Interfaces ApplicationsPure Abstract classesDescribe the designMore powerful inheritance and polymorphism

Fall 2010

Page 20: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 20

Quiz!

Fall 2010

Page 21: Sadegh Aliakbary Sharif University of Technology Fall 2010

Sharif University of Technology 21Fall 2010