Sadegh Aliakbary Sharif University of Technology Fall 2010
Agenda Inheritance protected members super keyword Initialization in inheritance Fall 2010Sharif University of Technology2
Inheritance Example Fall 2010Sharif University of Technology3
Inheritance Example (2) Fall 2010Sharif University of Technology4
Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Subclass extends superclass Subclass More specialized group of objects Behaviors inherited from superclass Can customize Additional behaviors Fall 2010Sharif University of Technology5
Class Hierarchy Direct superclass Inherited explicitly (one level up hierarchy) Indirect superclass Inherited two or more levels up hierarchy Single inheritance Inherits from one superclass Multiple inheritance Inherits from multiple superclasses Java does not support multiple inheritance Fall 2010Sharif University of Technology6
is-a relationship Object of subclass “is an” object of super-class Example: Rectangle is quadrilateral. Class Rectangle inherits from class Quadrilateral Quadrilateral: superclass Rectangle: subclass Superclass typically represents larger set of objects than subclasses superclass: Vehicle subclass: Car Smaller, more-specific subset of vehicles Fall 2010Sharif University of Technology7
More Examples Fall 2010Sharif University of Technology8
Protected Members Intermediate level of protection between public and private protected members accessible by subclass members Class members in the same package protected members are also package accessible friendly Protected variables and methods are shown with a # symbol in UML diagrams Fall 2010Sharif University of Technology9
UML Class Diagram Fall 2010Sharif University of Technology10 Note to notation
Subclasses may… Add new functionality New members Use inherited functionality Software reuse Override inherited functionality Change parent methods Fall 2010Sharif University of Technology11
Software reuse Software reuse is at the heart of inheritance Using existing software components to create new ones Capitalize on all the effort that went into the design, implementation, and testing of the existing software Fall 2010Sharif University of Technology12
Bad smell Avoid Copy & Paste! Please, Avoid Copy & Paste! Fall 2010Sharif University of Technology13
Access modifier in inheritance You can not override a public method as a private method Why? It violates the “is-a” rule You can not reduce accessibility of methods in subclasses Fall 2010Sharif University of Technology14
super keyword Access to parent members Why we need it? The super reference can be used to refer to the parent class often is used to invoke the parent's constructor Fall 2010Sharif University of Technology15
Initialization Constructors are not inherited even though they have public visibility We often want to use the parent's constructor to set up the "parent's part" of the object Fall 2010Sharif University of Technology16
Constructors A child’s constructor is responsible for calling the parent’s constructor The first line of a child’s constructor should use the super reference to call the parent’s constructor Otherwise, default constructor is implicitly invoked If default constructor does not exist (how?!) You should explicitly call an appropriate parent constructor using super keyword Fall 2010Sharif University of Technology17
Object class methods equals() toString() finalize() … Fall 2010Sharif University of Technology18
Multiple Inheritance Java supports single inheritance Derived class can have only one parent class Multiple inheritance allows a class to be derived from two or more classes inheriting the members of all parents Collisions, such as the same variable name in two parents, have to be resolved Java does not support multiple inheritance The use of interfaces usually gives us aspects of multiple inheritance `without the overhead Fall 2010Sharif University of Technology19
Practice it… Fall 2010Sharif University of Technology20
Quiz! Fall 2010Sharif University of Technology21
Fall 2010Sharif University of Technology22