Exploring Java Inheritance
Exploring Java Inheritance
AP COMPUTER SCIENCE,
EXPLORING JAVA INHERITANCE
Made by: Abdulelah Hamam, Abdullah
Alsinan, Hassan Alawami, Yousef
Alsanad
TABLE OF CONTENTS
Superclass/Subclass Inheritance in
Relationship from a Java "is-a" Relationship
subclass to the superclass Benefits of using inheritance in "is-a" relationship in object-
with clear examples. Java programming. oriented programming with
relevant examples.
To inherit from a
class, use the
extends keyword.
In the example on
the right, the Car
class (subclass)
inherits the
attributes and
methods from the
Vehicle class
(superclass):
Inheritance in Java
In Java, inheritance is like a family tree for classes. Just like children
can inherit traits from their parents, classes can inherit properties
and methods from other classes.
Why is is useful
1. Less Repetition: You don't have to write the same code over and over again. You
can define common features in a parent class and let the child classes inherit
them.
2. Easy to Change: If you need to update something that multiple classes use, you
only have to change it once in the parent class, and all the child classes get the
update automatically.
3. Organization: It's like putting similar items together on a shelf. It keeps your code
organized and makes it easier to manage.
4. Consistency: If all your classes share a parent class, they're likely to have a
similar structure. This makes your code more predictable and easier to
understand.
"Inheritance is a way to reuse code of existing
objects, or to establish a subtype from an
existing object, or both, depending upon
programming language support."
- James Gosling
Understanding the "is-a"
Relationship
In Java's object-oriented programming, the "is-a" relationship
signifies inheritance, where a subclass inherits characteristics from
a superclass. For instance, in a shape hierarchy, a Circle and
Rectangle subclass "is-a" Shape". This relationship allows treating
both circles and rectangles as shapes, enabling the invocation of
methods defined in the Shape` superclass, thus illustrating the core
principle of the "is-a" relationship in Java.
Constructor Implementation for
Subclasses
// Superclass constructor
Animal(String species) {
this.species = species;
}
}
// Subclass constructor
Dog(String species, String breed) {
super(species); // Call superclass constructor
this.breed = breed; // Initialize subclass-specific attribute
}
}
Members Classes
A subclass inherits all the Because the constructor
members (fields, methods, name is based on the class
and nested classes) from its name. When you inherit
superclass. Constructors are methods, the method
not members, so they are not signature must be the same.
inherited by subclasses, but Constructors of a child class
the constructor of the can't keep the same method
superclass can be invoked signature of the parent class.
from the subclass.
"Super" Keyword
1. Accessing Superclass Members: It lets you use methods and
variables from the class you're extending.
2. Invoking Superclass Constructor: It helps you run the constructor
of the class you're extending, in case you need to add something
to it.
3. Invoking Superclass Method: If you've got a method with the same
name in both your class and the class you're extending, super can
help you call the one in the superclass.
4. Referring to Superclass: If you just want to talk about the
superclass in general, you can use super.
THANKS!
CREDITS: This presentation template was
created by Slidesgo, and includes icons by
Flaticon, and infographics & images by Freepik