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

Topic 2 Simple Inheritance

Inheritance in java simple

Uploaded by

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

Topic 2 Simple Inheritance

Inheritance in java simple

Uploaded by

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

Single Inheritance in Java

Inheritance is a fundamental concept in object-oriented programming. It allows you to create new classes based on existing
classes, inheriting their properties and behaviors. This promotes code reuse, hierarchical classification, and polymorphism,
making your code more efficient, organized, and flexible.

by dennis ebenezer
Introduction to Inheritance
1 What is Inheritance? 2 Why Use Inheritance?
Inheritance is a powerful mechanism in Java that Inheritance offers several advantages: it promotes
enables you to create new classes (subclasses) based code reusability by allowing you to reuse existing
on existing classes (superclasses). It establishes an code in new classes, creates a hierarchical
"is-a" relationship between classes, where the classification structure for organizing your classes,
subclass inherits properties and methods from the and enables polymorphism, allowing objects of
superclass. different types to be treated as if they were of the
same type.
Single Inheritance in Java
Definition Syntax

In Java, single inheritance allows a subclass to inherit from The syntax for single inheritance in Java is simple: you use
only one superclass. This ensures a clear and the keyword "extends" followed by the superclass name
straightforward inheritance hierarchy. A subclass can inherit within the subclass declaration. For example, "class Dog
all the public and protected members of the superclass, extends Animal { ... }" defines a "Dog" class that inherits
including fields and methods. from the "Animal" class.
Members Inheritance
Access Modifier Field Inheritance Method Inheritance Constructor Inheritance

public Inherited Inherited Not Inherited

protected Inherited Inherited Not Inherited

default Inherited (same package) Inherited (same package) Not Inherited

private Not Inherited Not Inherited Not Inherited


Method Overriding
Concept
Method overriding allows a subclass to provide a specific implementation for a method inherited from its superclass.
This is useful when the subclass requires a different behavior for the inherited method.

Use @Override
The @Override annotation is used to explicitly indicate that a method is overriding a method from the superclass.
This improves code readability and helps the compiler detect potential errors.

Example
For example, a "Dog" class might override the "speak()" method inherited from the "Animal" class to print "Woof!"
instead of the generic "Animal sounds." This allows the "Dog" class to have its own unique behavior.
Super Keyword
Purpose Syntax
The "super" keyword is used to refer to members of the The "super" keyword can be used to access fields,
superclass from within a subclass. This is necessary methods, and constructors of the superclass. For
when you need to access a superclass member that is example, "super.field" refers to the field named "field"
hidden by a member with the same name in the subclass in the superclass, "super.method()" calls the method
or when you want to call a superclass constructor. named "method" in the superclass, and "super()" calls
the superclass constructor.
Inheritance and Constructors

Constructor Chaining Example


When a subclass constructor is invoked, Java implicitly calls For example, if the "Dog" class has a constructor that takes
the no-argument constructor of its superclass. This is known the breed as an argument, it might call the superclass
as constructor chaining. If the superclass doesn't have a no- constructor with the "name" argument to initialize the
argument constructor, you need to explicitly call a specific "name" field of the "Animal" class.
superclass constructor using the "super()" keyword.
Inheritance and Polymorphism
1 Runtime Polymorphism
Runtime polymorphism is a fundamental concept in object-oriented programming that allows objects of different
types to be treated as if they were of the same type. This is achieved through method overriding and method binding.

2 Method Overriding
When a subclass overrides a method inherited from its superclass, it provides a specific implementation of that
method. This allows objects of different types to respond differently to the same method call.

3 Method Binding
Method binding is the process of associating a method call with the correct method implementation at runtime.
During runtime, the actual type of the object is determined, and the appropriate method implementation is called.

You might also like