Lecture 6 - Inheritance
Lecture 6 - Inheritance
International University
School of Computer Science and Engineering
Inheritance
(IT069IU)
🌐 leduytanit.com 1
Previously,
- Increment and Decrement Operators
- Scope of Declarations
- this keyword
- Array:
- Declare and Create Array
- Loop through Array
- Pass Arrays to Methods
- Pass by Value vs Pass by Reference
- Multidimensional Arrays
- Class Arrays for helper methods
- ArrayList (Collections Class)
- Array vs ArrayList
2
Agenda
- Inheritance:
- Definition and Examples
- Types of Inheritance
- UML Diagram
- Animal Inheritance Example
- Without Inheritance
- With Inheritance
- Method Overriding
- Constructors in Subclasses
- Keyword Super
- Method Overriding
- Keyword Super
- Access Modifier
- Protected
- Exercise for University Staff & Lecturer
3
Inheritance
Grand Father
Father
Child
Inheritance
- Inheritance is a way to reuse classes by expanding them
into more specific types of classes. Person
-Name
- Inheritance allows a child class (subclass) to inherit the -Age
attributes and the methods of a parent class (superclass).
+walk()
So a child class can do anything that the parent class +eat()
can do!
- A child class
- can have its own attributes and methods.
- A child class can customize methods that it inherits
from its parent class (method overriding)
7
UML Class Diagram for Shape
8
Types of Inheritance & Syntax
Without Inheritance
IU Digital Library Example
Animal Inheritance Example
12
With Inheritance
Let’s live code in Java!
14
Animal Class
[Info] Did you notice the superclass Animal looks like the
normal class with attributes and method.
15
Dog Class
[Question] Can you guess what does the keyword super
do in the constructor of Dog Class?
16
Spider Class
[Question] Can you guess what does the keyword super
do in the constructor of Spider Class?
17
Main Class for Testing
18
Keyword super() in a constructor in child class
- A child class, all properties, instance variables, and methods are inherited,
except for constructors, so you need to define constructors for child class.
- The keyword super in a constructor in a child class can be used to call a
constructor in the parent class. It’s a way to delegate the responsibility to the
parent class.
19
Method Overriding
- If subclass (child class) has the same method as
declared in the superclass (parent class), it is
known as method overriding in Java.
- Method overriding is used to provide a different
implementation of a method which is already
provided by its superclass.
- Rules for method overriding:
- Method must have the same name as in the
parent class.
- Method must have same parameter as in the
parent class.
- Method overriding must happens in IS-A
relationship (inheritance).
20
Method Overriding Example
21
Method Overriding in Spider Class
Spider.java
[Question] @Override is
optional, but why does we
want it when we override
any class?
Zoo.java
22
Keyword super() in a method in child class
- Also, the keyword super can be used in a method of child class to call a method of a
parent class even if that method is overridden.
Animal.java Spider.java
Zoo.java
Output:
23
Protected Members
- Remember, apart from public and private, we have “protected” to be an
access modifier
- “Protected” access offers an intermediate level of access between
public and private.
- A superclass’s protected members can be accessed by members of that
superclass, by members of its subclasses and by members of other
classes in the same package.
- In short, the “protected” access modifier means that anything within the
class can use it, as well as anything in any subclass.
24
The ‘protected’ Access Modifier
In the past, we discussed the private and public access modifiers. To review,
remember that public meant anyone could get access to the member
(variable, method, property, etc.), while private means that you only have
access to it from inside of the class that it belongs to.
25
Issue Without Protected Access
Animal.java
Dog.java
26
Protected Access Solution
Animal.java Dog.java
Output:
27
Getter Methods for Private Attributes
Output:
- The short answer is, you should make everything as restricted as possible
(private over protected, and protected over public) while still allowing
things to get done.
- When it comes to deciding between public and private, it all comes down to
how widely used you expect the class to be. If you think a class is reusable
across many projects, then you might as well make it public from the
beginning.
29
Class Object
- Secretly, any class/objects are inherited from the default Object class that is
the base class of everything.
- If you go up the inheritance hierarchy, everything always gets back to the
object class eventually.
- You can say that Class Object is the mother of all objects in Java.
30
Default Methods of the Class Object
34
Exercise for University Staff Inheritance
Sample Output
35
Let’s live code in Java!
Let’s write the Class UniversityStaff & the Class Lecturer together!
36
Recap
- Inheritance:
- Definition and Examples
- Types of Inheritance
- UML Diagram
- Animal Inheritance Example
- Without Inheritance
- With Inheritance
- Method Overriding
- Constructors in Subclasses
- Keyword Super
- Method Overriding
- Keyword Super
- Access Modifier
- Protected
- Exercise for University Staff & Lecturer
37
Thank you for your listening!
38