230 likes | 325 Views
Advanced Programming in Java. Sadegh Aliakbary Sharif University of Technology Fall 2012. Agenda. interface Multiple Inheritance. Review : Abstract. Abstract Methods No Implementation Sub-classes may implement abstract methods Abstract Classes Can not be instantiated
E N D
Advanced Programming in Java SadeghAliakbary Sharif University of Technology Fall 2012
Agenda • interface • Multiple Inheritance Sharif University of Technology
Review : Abstract • Abstract Methods • No Implementation • Sub-classes may implement abstract methods • Abstract Classes • Can not be instantiated • (Usually) A class with one or more abstract methods • A class which extends an abstract class • Is abstract unless it implements all the abstract methods • Concrete class Not abstract class Sharif University of Technology
Abstract Example Sharif University of Technology
Abstract Method • All subclasses have the method • But we can not implement the method in super-class • So why we define it in super-class? • Polymorphism Sharif University of Technology
Interface • Sometimes we have an abstract class, with no concrete method • interface : pure abstract class • no implemented method Sharif University of Technology
Interface Sharif University of Technology
Interface • All the methods are implicitly abstract • No need to abstract specifier • All the methods are implicitly public • No need to public specifier Sharif University of Technology
Interface Implementation • Some classes may inherit abstract classes • Some classes may implement interfaces • Is-a relation exists • If a class implements an interface • But does not implement all the methods • What will happen? • The class becomes an abstract class Sharif University of Technology
Multiple Inheritance in Java • A class can inherit one and only one class • A class may implement zero or more interfaces Sharif University of Technology
A Question • Why multiple inheritance is not supported for classes? • Why multiple inheritance is supported for interfaces? Sharif University of Technology
What About Name Collision? The return types are incompatible for the inherited methods B.f(), A.f() Sharif University of Technology
Interface Extension • Interfaces may inherit other interfaces • Code reuse • Is-a relation Sharif University of Technology
Interface properties • No member variable • Variables : implicitly final and static • Usually interfaces declare no variable • Only operations are declared • No constructor • Why? Sharif University of Technology
Example Sharif University of Technology
Interfaces Applications • Pure Abstract classes • Describe the design • The architect designs interfaces, the programmer implements them • Only interfaces are delivered to code consumers • More powerful inheritance and polymorphism Sharif University of Technology
Quiz! Sharif University of Technology
OK References Compiler Error d = new D(); d = new E(); c= new E(); b = new E(); a = b; b.f(); A a; B b; C c; D d; c = d; d = c; b = d; d = b; a.f(); Sharif University of Technology