Chapter 5
Inheritance
Inheritance
and
and
polymorphism
polymorphism
Inheritance
Inheritance
Inheritance is a fundamental
concept in object-oriented
programming (OOP) that
allows developers to create
new classes by inheriting
attributes and behaviors from
existing classes.
Inheritance is based on the idea of
creating a hierarchy of classes, with
more general classes at the top, and
more specific classes at the bottom.
The more general classes are called
superclass or parent classes, and
the more specific classes are called
subclasses or child classes.
The superclass
The superclass and
and
subclass relationship
subclass relationship
Inheritance is based on the concept of
a superclass and subclass
relationship. A superclass is a class
that is used as a basis for creating
other classes, while a subclass is a
class that is created from a
superclass, inheriting its attributes and
behaviors.
person.java
person.java Student.java
Student.java
public class Person
{ public class Student extends Person{
protected String name; public Student(){
protected String address;
System.out.println("Inside Student:Constructor");
public Person(){
//some code here
System.out.println(“Inside Person:Constructor”); }
name = ""; public static void main( String[] args ){
address = ""; Student anna = new Student();
}
}
public Person( String name, String address ){
this.name = name; }
this.address = address;
}
public String getName(){ return name; }
public String getAddress(){ return address; }
public void setName( String name ){
Extends
Extends
this.name = name;
}
Output
public void setAddress( String add ){
this.address = add;
} Inside Person:Constructor
} Inside Student:Constructor
The super
The super keyword
keyword
This keyword is used to refer
to the superclass of a
subclass. It can be used in two
main ways:
1. To call the constructor
of the superclass
public class Student extends Person{ public class Student extends Person{
public Student(){ public Student(){
super(); super("SomeName", "SomeAddress");
} }
public static void main( String[] args ){ public static void main( String[] args ){
Student anna = new Student(); Student anna = new Student();
} }
} }
2. to call a method or access a
field of the superclass.
public class Student extends Person{
public class Student extends Person{
public Student(){
public Student(){
super.name = "SomeName";
super.setName("SomeName");
super.address = "SomeAddress";
System.out.println(super.getName());
//some code here
}
}
public static void main( String[] args ){
public static void main( String[] args ){
Student anna = new Student();
Student anna = new Student();
}
}
}
}
OVERRIDE
OVERRIDE allows a subclass to provide a
different implementation of a
method that is already
defined in its superclass.
When a method in a subclass
has the same name, return
type, and parameters as a
method in its superclass, it is
said to override that method.
Final
Final
the "final" keyword is used to
create a constant variable, a
final method, or a final class.
Constant variable: If you declare a variable as
final, its value cannot be changed once it has
been assigned. This is useful when you want to
define a variable that should never change,
such as a mathematical constant.
public class Math {
public static final double PI = 3.141592653589793;
}
Final method: If you declare a method as final, it
cannot be overridden by a subclass. This is
useful when you want to ensure that a method's
behavior is not changed by subclasses.
public class Animal {
public final void makeSound() {
System.out.println("Animal is making a sound");
}
}
public class Cat extends Animal {
/*This will cause a compile-time error,
since makeSound() in Animal is final*/
public void makeSound() {
System.out.println("Meow");
}
}
Final class: If you declare a class as final, it
cannot be subclassed. This is useful when you
want to prevent other developers from
extending your class.
public final class Math {
// ...
}
public class Animal extends Math {
/*This will cause a compile-time error,
since Math is final and cannot be extended*/
}
Vehicles
Vehicles
Resource Page
Resource Page