0% found this document useful (0 votes)
15 views

Lecture 6 - Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 6 - Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Vietnam National University of HCMC

International University
School of Computer Science and Engineering

Inheritance

(IT069IU)

Le Duy Tan, Ph.D.


📧 [email protected]

🌐 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)

- Inheritance represents IS-A relationship between parent Programmer Dancer Singer


and child objects. -Name -Name -Name
-Age -Age -Age
-CompanyName -groupName -bandName
- Inheritance promotes the idea of code reuse to reduces
code repetition as classes can share similar common logic, +walk() +walk() +walk()
+eat() +eat() +eat()
structure, attributes and methods. +code() +dance() +sing()
+playGuitar()
Examples of Superclass, Subclasses
- Superclass tend to be “more general” and Subclass is more “more specific.”

Parent Class Child Class


(Superclass) (Subclasses)
Vehicles Car, Truck, Boat, Bicycle

Shape Circle, Triangle, Rectangle, Cube

UniversityStaff Lecturer, TeachingAssistant

Animal Dog, Cat, Spider, Duck


UML Class Diagram for CommunityMembers

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

[Question] Can you find out which


method of which class is overridden?

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.

With inheritance we add another option: protected. If a member of the class


uses the protected accessibility level, then anything inside of the class can
use it, as well as any subclass. It’s a little broader than private, but still more
restrictive than public.

25
Issue Without Protected Access
Animal.java

[Info] This will give an error as the instance


variable “name” is private in the parent class
because remember that private instance
variables can be access within the parent class
but not in the child class.

Dog.java

26
Protected Access Solution
Animal.java Dog.java

Zoo.java With protected access modifier, the subclass


Dog can access the instance variable
“name” that it inherited from Animal Class.

Output:

27
Getter Methods for Private Attributes

Output:

- This is a better way to keep your instance


variables to be private and still have ways
to access or modify their values using
getter and setter methods. 28
Which access modifier to use?
- For beginners, keep it simple:
- Class: public
- Attributes/Instance Variables: private
- Methods (including getters and setters): public
- Class Variables (static keyword): public
- Class Methods (static keyword): public
- Constants (final keyword):
- Class Constant (with static keyword): public
- Normal Constant (without static keyword): private

- 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.

You can imagine every classes or objects would be an child to the


default class Object of Java.

30
Default Methods of the Class Object

- Since all objects are inherited from the class Object


then all objects can use those methods.
- All classes in Java inherit directly or indirectly from
class Object (package java.lang), so its 11 methods
(some are overloaded) are inherited by all other
classes.
31
The default implementation of toString() method of Object Class

- When you print out any object, it will automatically


call toString() method of the class Object.
Output: - By default, toString() method will print out the Class
Name with the location of the object in the memory.
- This is not very useful to us! So let’s override it!
32
Override toString() method

Now, our new overridden


toString() method is
customized to print out the
string that is interesting and
useful to our defined classes
Output:
by call print() method.
33
Exercise for University Staff Inheritance
Sample Output

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!

“Motivation is what gets you started. Habit is


what keeps you going!”
Jim Ryun

38

You might also like