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

Exploring Java Inheritance

Uploaded by

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

Exploring Java Inheritance

Uploaded by

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

Teacher: Omar Khuraisat

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.

Implementation Why Constructors Are


"Super" Keyword
for Subclasses Not Inherited
Providing step-by-step Constructors not inherited in Scenarios in which the
explanations and illustrating with Java, and provide examples to "super" keyword is used in
examples. illustrate this concept. Java programming
Creating Superclasses and Subclasses

In Java, it is possible to inherit attributes and


methods from one class to another. We group
the "inheritance concept" into two categories:

● Subclass (child) - the class that inherits


from another class
● Superclass (parent) - the class being
inherited from
Creating Superclasses and Subclasses

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

To implement constructors for subclasses in Java, simply use the


super() keyword in the subclass constructor to call the
constructor of the superclass. You can pass parameters to super()
if needed. This ensures that the superclass constructor is
executed before the subclass constructor. Then, initialize any
subclass-specific attributes as necessary within the subclass
constructor.
Example:
class Animal {
String species;

// Superclass constructor
Animal(String species) {
this.species = species;
}
}

class Dog extends Animal {


String breed;

// Subclass constructor
Dog(String species, String breed) {
super(species); // Call superclass constructor
this.breed = breed; // Initialize subclass-specific attribute
}
}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog("Canine", "Labrador");
System.out.println("My dog is a " + myDog.species + " of breed " + myDog.breed);
}
}
Why constructors are not Inherited

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

Please keep this slide as attribution

You might also like