0% found this document useful (0 votes)
4 views8 pages

javadoc4

Uploaded by

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

javadoc4

Uploaded by

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

Experimental class on May 8th

Today's experimental class mainly focuses on the concept of inheritance


and learning some related keywords, including extends, super. Finally, we will
use various examples to help everyone better understand inheritance.

 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).

2. Advantages and Use Cases of Inheritance


// Sample code snippet for superclass and subclass
class Animal {
String name;
void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println(name + " is barking.");
}
}

3. The extends Keyword


// Sample code snippet demonstrating the use of extends
class Dog extends Animal {
// Dog class inherits properties and methods from Animal class
}
extends are inherited keywords

4. The super Keyword


// Sample code snippet demonstrating the use of super
class Dog extends Animal {
void display() {
super.makeSound(); // Call superclass method
}
}
5. Method overriding
Introduction to Method Overriding:
Method overriding is a concept in object-oriented programming where a
subclass provides a specific implementation of a method that is already
defined in its superclass. When a method is overridden in a subclass, it
replaces the implementation provided by the superclass, allowing the
subclass to tailor the behavior of the method to its specific needs.
How Subclasses Override Methods from Superclass:
Subclasses override methods from their superclass by providing a new
implementation of the method with the same signature (i.e., same name,
same return type, and same parameter list) in the subclass. When an object of
the subclass calls the overridden method, the JVM invokes the overridden
version defined in the subclass rather than the one defined in the superclass.
example illustrating method overriding:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Dog barks");
}
}

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
}

// Concrete animal subclass Dog


class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}

// Concrete animal subclass Cat


class Cat extends Animal {
void makeSound() {
System.out.println("Cat meows");
}
}

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.

2. Comparison: Abstract Class vs. Regular Class


Instantiation: Abstract classes cannot be instantiated directly, while
regular classes can be instantiated using the new keyword.
Constructor: Abstract classes can have constructors but cannot be
directly called for instantiation, while constructors of regular classes can be
directly called.
Methods: Abstract classes can contain abstract methods and non-
abstract methods, while regular classes can only contain non-abstract
methods.
Inheritance: Abstract classes can be inherited by other classes, while
regular classes can also be inherited.

3. Try incorporating abstract classes into your code


 Practical Example of Inheritance
Real-world scenario demonstrating inheritance in action
Requirement:
Let's consider a hierarchy of geometric shapes: Shape as the superclass,
with subclasses Circle and Rectangle. Each shape has a method
calculateArea() to compute its area.
Implement:
Next, let's explain the code behind it
1. Explanation and Considerations:
In this example, Shape is the superclass, and Circle and Rectangle are

subclasses that inherit from Shape.

Each subclass overrides the calculateArea() method to provide its specific

implementation for computing the area.

When an object of Circle or Rectangle calls calculateArea(), the overridden

method in the respective subclass is invoked, providing the correct area

calculation for that shape.

This example demonstrates method overriding in the context of

inheritance, where each subclass provides its own behavior while maintaining

a consistent interface with the superclass.

It also illustrates the principle of polymorphism, where objects of different

subclasses can be treated uniformly through a common superclass interface

(Shape), allowing for flexible and reusable code.

2. In this main function:


We create a Circle object named circle with a radius of 5.0 and a

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

objects to calculate their respective areas.

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.

You might also like