0% found this document useful (0 votes)
0 views11 pages

Java Inheritance Presentation

The document explains Java inheritance, highlighting its ability to create hierarchical class structures where subclasses inherit fields and methods from superclasses. It covers types of inheritance, the use of the super keyword, method overriding, dynamic method dispatch, and abstract classes. Additionally, it discusses the final keyword's role in preventing method overriding and subclassing.

Uploaded by

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

Java Inheritance Presentation

The document explains Java inheritance, highlighting its ability to create hierarchical class structures where subclasses inherit fields and methods from superclasses. It covers types of inheritance, the use of the super keyword, method overriding, dynamic method dispatch, and abstract classes. Additionally, it discusses the final keyword's role in preventing method overriding and subclassing.

Uploaded by

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

Java Inheritance

Inheritance Basics

• Inheritance enables hierarchical class


structures.
• Subclass inherits fields and methods from
superclass.
• Syntax: class Triangle extends TwoDShape
{ ... }
• Promotes code reuse and logical modeling.
Types of Inheritance in Java

• ✔ Single Inheritance
• ✔ Multilevel Inheritance:
class C extends B extends A
• ✔ Hierarchical Inheritance
• ✘ Multiple Inheritance not supported via
classes (use interfaces)
Example – TwoDShape & Triangle

• class TwoDShape { double width, height; void


showDim() { ... } }
• class Triangle extends TwoDShape {
String style;
double area() { return width * height / 2; }
}
The super Keyword

• Used to call superclass constructor:


super(width, height);
• Used to access hidden superclass members:
super.showDim();
• Resolves naming conflicts and reuses parent
logic.
Method Overriding

• Subclass redefines method with same


signature:
• class A { void show() { ... } }
• class B extends A { void show() { ... } }
• Use super.show() to call overridden version.
Dynamic Method Dispatch

• Superclass reference refers to subclass object:


• A a; B b = new B(); a = b; a.show();
• Calls subclass method at runtime.
• Enables runtime polymorphism.
Abstract Classes & Methods

• Abstract class: can't be instantiated directly.


• Contains abstract methods with no body.
• abstract class Shape { abstract double area(); }
• Forces subclasses to define abstract methods.
Example – Abstract area()
abstract class TwoDShape {
double width, height;
abstract double area();
}
class Triangle extends TwoDShape {
double area()
{
return width * height / 2;
}
}
Final Keyword

• final method: cannot be overridden.


• final class: cannot be subclassed.
• final variable: becomes a constant.
• Usage:
final class A { ... }
final void meth() { ... }
Summary

• ✔ Inheritance models 'is-a' relationships


• ✔ super builds on inherited logic
• ✔ Overriding enables polymorphism
• ✔ Abstract class enforces implementation
contracts
• ✔ final protects methods, classes, and
constants

You might also like