javadoc4
javadoc4
Concept of Inheritance
1. Introduction to Inheritance
Inheritance is a fundamental concept in object-oriented programming
(OOP) languages like Java. It allows a class to inherit properties and behaviors
(methods) from another class, known as the superclass or parent class. The
class that inherits from the superclass is called the subclass or child class.
Inheritance facilitates code reusability and promotes a hierarchical
relationship between classes. It enables the creation of more specialized
classes (subclasses) that inherit common characteristics from more general
classes (superclasses).
6. Inheritance of Constructors
How Constructors are Inherited in Inheritance
Constructors : In Java, a constructor is a special type of method that is
automatically called when an object of a class is created. Its primary purpose
is to initialize the newly created object.
// Sample code snippet demonstrating inheritance of constructors
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // Call superclass constructor
System.out.println("Dog constructor");
}
}
7. Summary of Inheritance
Can you simply make the code run for the above content? Give it a
try, and we will provide the final result below??
Abstract Class
Abstract Class is a special class in Java that cannot be instantiated but can
be inherited
// Abstract class Animal
abstract class Animal {
abstract void makeSound(); // Abstract method
}
1. Concept Explanation:
The abstract class Animal defines an abstract method makeSound(),
which does not have a specific implementation. Since different animals make
different sounds, this method needs to be implemented in concrete
subclasses.
Concrete animal subclasses (such as Dog and Cat) inherit from the Animal
class and implement the abstract method makeSound(), providing specific
sounds for each type of animal.
The abstract class Animal can be used as a type for other classes but
cannot be instantiated directly.
inheritance, where each subclass provides its own behavior while maintaining
Rectangle object named rectangle with a width of 4.0 and a height of 6.0.
We call the calculateArea() method for both the circle and rectangle
We print out the calculated areas for the circle and rectangle.
summary
In this chapter, we explored the concept of inheritance in Java, which
allows a subclass to inherit attributes and behaviors from its superclass.
Here's a summary of the key points covered:
Introduction to Inheritance:
Inheritance is a mechanism in Java that allows a class to inherit properties
and methods from another class.
The class that inherits from another class is called a subclass, while the
class being inherited from is called a superclass.
Relationship between Superclass and Subclass:
A subclass extends a superclass using the extends keyword.
Subclasses inherit all non-private fields and methods from their
superclass.
Subclasses can override methods inherited from the superclass to provide
their own implementation using the @Override annotation.
Practical Example of Inheritance:
We demonstrated inheritance with a practical example involving shapes,
where Shape was the superclass and Circle and Rectangle were subclasses.
Each subclass provided its own implementation of the calculateArea()
method inherited from the Shape superclass.
Method Overriding:
Method overriding occurs when a subclass provides a specific
implementation of a method that is already defined in its superclass.
The @Override annotation is used to indicate that a method is intended to
override a method from the superclass.
Rules and Considerations for Method Overriding:
The method signature (name and parameters) in the subclass must match
the method signature in the superclass.
The return type of the overriding method can be a subtype of the return
type in the superclass.
Overall, inheritance is a powerful feature in Java that promotes code reuse
and facilitates the creation of hierarchies of related classes. It allows for the
organization of code in a hierarchical manner, promoting modularity and
extensibility. Through method overriding, subclasses can customize the
behavior of inherited methods, enabling polymorphic behavior and enhancing
the flexibility of Java programs.