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

Lecture 04 - Inheritance

Uploaded by

anonimolankaan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 04 - Inheritance

Uploaded by

anonimolankaan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Lecture 04: Inheritance

SE116 - Introduction to Programming II


Previously on SE116
● Checkpoint#1: Suppose that you have a factory and you need to maintain
the list of all workers in your factory. If you represent your factory as a list to
contain workers, can you use an ArrayList object to contain Employee
references? If you implement the relationship “Employee HAS-A Date” where
a date variable refers to the hiring date of an employee in the factory, can you
search an employee in the list according to a hiring date? Can you sort the
list? Would you prefer sorting an ArrayList using the ready-to-use static
methods of Collections class or using your new implemented sorting
algorithms? Why?
● Checkpoint#2: Can you use an array object to contain Employee
references? Would you prefer using an array or an ArrayList? Why?
Previously on SE116
● What are the main features of an Object Oriented Programming Language?
○ An object oriented programming language should provide the following features (at least):
■ encapsulation,
■ inheritance,
■ polymorphism.
Object Oriented Programming: Inheritance
● We have talked about “encapsulation” before.
● We have also talked about how we can create objects from other objects.
○ composition: “HAS-A” relationship
● Today, we will be talking about “inheritance”.
○ inheritance: “IS-A” relationship
● We can think of “inheritance” as in generalization and specialization.
Animal Cat

More general More specific


Object Oriented Programming: Inheritance
● The common elements are in general classes: consider the “Animal” class.
● More specific elements are in specialized classes: consider the “Cat” class.
● Later on, if we want to create a “Dog” class, we can also base it on “Animal”
and provide it with basic animal properties and actions.
Object Oriented Programming: Inheritance
● To be honest, as humans we can get caught up in the process and create
deep layers of inheritance.
● Try to keep it shallow - an inheritance tree that has more than three layers is
hard to maintain.
Object Oriented Programming: Inheritance
● Provides software usability
● Can save time during program development by basing new classes on existing
proven and debugged high-quality software.
● The keyword “extends”
○ A new class is created by acquiring an existing class’s members and
possibly embellishing them with new and/or modified capabilities.
○ Derived (or Child or Sub class) “IS A” Base (or Parent or Super class)
■ Derived class inherits all members (except constructors) from base
class
■ Derived class can have extra members (data and/or method)
● This is why inheritance is sometimes referred to as specialization.
Object Oriented Programming: Inheritance
● A subclass can be a superclass of future subclasses.
● The direct superclass is the superclass from which the subclass explicitly
inherits.
● An indirect superclass is any class above the direct superclass in the
class hierarchy.
● The Java class hierarchy begins with class Object (in package java.lang)
● Every class in Java directly or indirectly extends (or “inherits from”) Object.
● Java supports only single inheritance, in which each class is derived from
exactly one direct superclass.
Object Oriented Programming: Inheritance
● Consider the class hierarchy of each of the following examples:

○ (from Fig. 9.1 of the textbook)


Object Oriented Programming: Inheritance
● Consider the following CommunityMember hierarchy
○ (from Fig. 9.2 of the textbook)
Object Oriented Programming: Inheritance
● Consider the following Shape hierarchy
○ (from Fig. 9.3 of the textbook)
Object Oriented Programming: Inheritance
Implementation of an inheritance example: Animal hierarchy
Object Oriented Programming: Inheritance vs Composition
● Inheritance provides a mechanism to pass properties and actions to “sub-
classes.”
● As we design the program (or software) we come across the decision to either
to use inheritance or composition.
○ Inheritance: “IS-A” relationship
○ Composition: “HAS-A” relationship
● This decision has to be made carefully.
○ Does the new class really have to be a subclass of the parent class?
Object Oriented Programming: Inheritance vs Composition

● Consider two classes Point and Circle to be implemented


independently.
● Alternatively, consider these classes to be implemented using either
HAS-A relationship or IS-A relationship.
○ Which of the following relationships is more realistic?
■ Circle HAS-A Point
■ Circle IS-A Point
Object Oriented Programming: Inheritance vs Composition
public class Point { public class Circle {
private int x; // no relationship between Point and Circle
private int y; private int x;
private int y;
public Point(int a, int b) { private int radius;
x = a;
y = b; public Circle(int a, int b, int r) {
} x = a;
// setters and getters to be defined for x and y … y = b;
} radius = r;
}
// setters and getters to be defined for x, y and radius …
}

How can you access the x variable of a


Circle object?
→ aCircleObject.getx()
Object Oriented Programming: Inheritance vs Composition
public class Point { public class Circle {
private int x; private Point center; // Circle HAS-A Point
private int y; private int radius;

public Point(int a, int b) { public Circle(Point myCenter, int r) {


x = a; center = myCenter;
y = b; radius = r;
} }

// setters and getters to be defined for x and y … // setters and getters to be defined for center and radius …
} }

How can you access the x variable of the


center member of a Circle object?

aCircleObject.getCenter().getx()
Object Oriented Programming: Inheritance vs Composition
public class Point { public class Circle extends Point {
private int x; // Circle IS-A Point
private int y; private int radius;

public Point(int a, int b) { public Circle(int a, int b, int r) {


x = a; super(a, b);
y = b; radius = r;
} }
// setters and getters to be defined for x and y … // setters and getters to be defined for radius …
} }

How can you access the x variable of a


Circle object?
→ aCircleObject.getx()
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
● Access modifiers specify the accessibility of the members of a class
○ public : directly accessible everywhere
○ private : directly accessible within class scope only
○ protected : directly accessible within class, all subclasses, and
package
○ no modifier (default) : directly accessible within class and
package
■ referred as ‘package private’
Object Oriented Programming: Inheritance
● An example of using a package :
○ Let’s consider an example package (named my_pack_folder) in a
simple project.
○ Let the package my_pack_folder store the following sample Java
source files:
■ TheParent.java
■ TheChild.java
■ TheUnrelated.java
Object Oriented Programming: Inheritance
package my_pack_folder;

public class TheParent /* extends Object */ {

int firstData; // visible in this class and in the other parts of the package my_pack_folder

protected int secondData; // visible in this class, in the subclasses of this class and in the package my_pack_folder

public int thirdData; // visible everywhere in Java world

private int fourthData; // visible in this class only

// filename: TheParent.java
Object Oriented Programming: Inheritance
package my_pack_folder;

public class TheChild extends TheParent {

public void aMethodInTheChild() {

firstData = 11; // is it accessible here? YES

this.secondData = 12; // is it accessible? YES

thirdData = 13; // is it accessible? YES

fourthData = 14; // is it accessible? NO

// filename: TheChild.java
Object Oriented Programming: Inheritance
package my_pack_folder;

public class TheUnrelated /* extends Object */ {

public void exampleMethod() {

TheParent theReference = new TheParent();

theReference.firstData = 1; // is it accessible? YES

theReference.secondData = 2; // is it accessible? YES

theReference.thirdData = 3; // is it accessible? YES

theReference.fourthData = 4; // is it accessible? NO

TheChild anotherReference = new TheChild();

anotherReference.firstData = 10; anotherReference.secondData = 20; anotherReference.thirdData = 30; // YES

anotherReference.fourthData = 40; // NO

firstData = 5; // There is no such identifier in this scope! It needs to be defined!

} // filename: TheUnrelated.java
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object Oriented Programming: Nested Classes

The Java programming language allows you to define a class within


another class. Such a class is called a nested class.
Nested classes in Java are divided into two categories: non-static and
static. Non-static nested classes are called inner classes. Nested
classes that are declared static are called static nested classes.
Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Object Oriented Programming: Nested Classes

Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Object Oriented Programming: Nested Classes
● Why do we use nested classes?
○ Compelling reasons for using nested classes include the following:
■ It is a way of logically grouping classes that are only used in one place: If a class is useful to
only one other class, then it is logical to embed it in that class and keep the two together.
Nesting such "helper classes" makes their package more streamlined.
■ It increases encapsulation: Consider two top-level classes, A and B, where B needs access to
members of A that would otherwise be declared private. By hiding class B within class A, A's
members can be declared private and B can access them. In addition, B itself can be hidden
from the outside world.
■ It can lead to more readable and maintainable code: Nesting small classes within top-level
classes places the code closer to where it is used.

Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Object Oriented Programming: Inheritance

Let’s now dissect the Employee hierarchy example of the textbook.

Homework: Yet another sample inheritance application, Vehicle hierarchy


(Check please the “Lecture Materials” on Bb.)
References
● The main reference is our textbook: Java How to Program, 10/e (Early Objects), Global Edition,
Paul Deitel and Harvey Deitel, Pearson, ISBN13: 9781292018195
● Additional references are the slides by Kaya Oğuz, Senem Kumova Metin, İlker Korkmaz

You might also like