20
Advanced Programming in Java Salman Marvasti Sharif University of Technology Winter 2015

Salman Marvasti Sharif University of Technology Winter 2015

Embed Size (px)

Citation preview

Advanced Programming in Java

Salman MarvastiSharif University of Technology

Winter 2015

Sharif University of Technology 2

AgendainterfaceMultiple Inheritance

Fall 2014

Sharif University of Technology 3

Review : AbstractAbstract Methods

No ImplementationSub-classes may implement abstract methods

Abstract ClassesCan not be instantiated(Usually) A class with one or more abstract

methodsA class which extends an abstract class

Is abstract unless it implements all the abstract methods

Concrete class Not abstract classFall 2014

Sharif University of Technology 4

Abstract Example

Fall 2014

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 2014

Sharif University of Technology 6

InterfaceSometimes we have an abstract class, with no

concrete methodinterface : pure abstract classno implemented method

Fall 2014

Sharif University of Technology 7

Interface

Fall 2014

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 2014

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 becomes an abstract class

Fall 2014

Sharif University of Technology 10Fall 2014

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 2014

Sharif University of Technology 12Fall 2014

Sharif University of Technology 13

A QuestionWhy multiple inheritance is not supported for

classes?Why multiple inheritance is supported for

interfaces?

Fall 2014

Sharif University of Technology 14

What About Name Collision?

Fall 2014

The return types are incompatible for the inherited methods B.f(), A.f()

Sharif University of Technology 15Fall 2014

Sharif University of Technology 16

Interface ExtensionInterfaces may inherit other interfacesCode reuseIs-a relation

Fall 2014

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 2014

Sharif University of Technology 18

Example

Fall 2014

Sharif University of Technology 19

Interfaces ApplicationsPure Abstract classesDescribe the design

The architect designs interfaces, the programmer implements them

Only interfaces are delivered to code consumers

More powerful inheritance and polymorphism

Fall 2014

Sharif University of Technology 20Fall 2012