Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Inheritance

Similar presentations


Presentation on theme: "An Introduction to Inheritance"— Presentation transcript:

1 An Introduction to Inheritance
Lecture 11

2 What is inheritance? Inheritance is the process in which a new class is derived from an existing one

3 What is the core of inheritance?
At the heart of inheritance is the idea of software reuse

4 What is a class? A class is a set of elements
The set is defined using the characteristics of the elements rather than by listing the elements

5 What are the characteristics
the attributes the methods Javadocs describes the class in terms of its characteristics

6 What do we call the original class?
the parent class or the super class the base class

7 What do we call a derived class?
a child class or a sub class

8 What is the relationship between the base class and a derived class?
The relationship is an is-a-relationship A derived class is-a more specific version of the original or base class A derived class does everything the base class does and more

9 What else can we say about the is-a-relationship?
A derived class is a specialization of its base class. A base class is a generalization of its derived class(es).

10 How can we graphically indicate the is-a-relationship?
We can use UML diagrams UML stands for Unified Modeling Language In a UML diagram, the arrow points from the derived class to the base class (i.e. from the child class to the parent class). See Class Hierarchies.doc

11 What does a child class inherit from its parent class?
The child class inherits methods and attributes (variables) from the parent class. The child class can use public methods and access public attributes of the parent class The child class can use protected methods and access protected attributes of the parent class. The child class can not use private methods nor access private attributes of the parent class

12 What can a parent class inherit from one of its child classes?
NOTHING!!! The is-a-relationship is NOT symmetric Children inherit from parents but parents DON’T inherit from children except for insanity

13 How do we indicate that a class is a derived class?
We use the Java reserved word extends. The word extends indicates that a new class is being derived from an existing class. Many child classes may be derived from a single parent but in Java, each child only has one parent. in Java, no child can be derived from more than one parent. Java uses single inheritance.

14 How does a child access characteristics of the superclass?
by using the reserved word super

15 What about constructors?
Constructors are not inherited by the child class. The constructor of the parent class can be explicitly invoked by the child class using the reserved word super along with the appropriate parameter list. The constructor of the parent class has to be called first (before anything else in the child class) Only one of the parent class’ constructors can be called. Child must choose.

16 What are some of the uses of derived classes?
to add characteristics to what exists in the base class to modify behavior

17 How does a derived class modify the behavior of its base class?

18 by overriding existing methods
by adding additional methods a method in the derived class which overrides a method in its base class has the same signature (name and parameter list) but behaves differently.

19 What is the difference between the terms overloading and overriding as applied to methods?
overloaded methods have different signatures and are in the same class. overriden methods have the same signature and are in different classes.

20 How else can we explain the difference between overloaded and overriden methods?
With an overloaded method choice of which method is being invoked is determined by which method’s number, order and type of parameters match the call. With an overridden method, access to the method in the parent class is blocked (the parent class’ method is hidden and can’t be seen unless the word super is applied) The word super can only be used INSIDE the child class.


Download ppt "An Introduction to Inheritance"

Similar presentations


Ads by Google