Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sadegh Aliakbary Sharif University of Technology Fall 2010.

Similar presentations


Presentation on theme: "Sadegh Aliakbary Sharif University of Technology Fall 2010."— Presentation transcript:

1 Sadegh Aliakbary Sharif University of Technology Fall 2010

2 Agenda Inheritance protected members super keyword Initialization in inheritance Fall 2010Sharif University of Technology2

3 Inheritance Example Fall 2010Sharif University of Technology3

4 Inheritance Example (2) Fall 2010Sharif University of Technology4

5 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

6 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

7 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

8 More Examples Fall 2010Sharif University of Technology8

9 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

10 UML Class Diagram Fall 2010Sharif University of Technology10 Note to notation

11 Subclasses may… Add new functionality New members Use inherited functionality Software reuse Override inherited functionality Change parent methods Fall 2010Sharif University of Technology11

12 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

13 Bad smell Avoid Copy & Paste! Please, Avoid Copy & Paste! Fall 2010Sharif University of Technology13

14 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

15 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

16 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

17 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

18 Object class methods equals() toString() finalize() … Fall 2010Sharif University of Technology18

19 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

20 Practice it… Fall 2010Sharif University of Technology20

21 Quiz! Fall 2010Sharif University of Technology21

22 Fall 2010Sharif University of Technology22


Download ppt "Sadegh Aliakbary Sharif University of Technology Fall 2010."

Similar presentations


Ads by Google