Java Extends keyword
Extends
- Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.
- To understand about of the keyword, we need to learn the concept of inheritance of the java programming language.
So, let us understand the concept of inheritance briefly to have a clear picture of extend keyword.
Java Inheritance
One object can acquire all a parent object's properties and actions through the technique of inheritance in Java. It is a core part of OOPs (System for Object-Oriented Programming).
Inheritance in Java is intended to allow you to construct new classes.
which are extensions of existing classes. From an existing class we can implement the parent class or super class methods and its properties. For existing class, we can also provide additional fields and its methods.
Why use inheritance in Java:
- To enable runtime polymorphism when overriding methods
- Can be accomplished to reuse code.
Defining Terms for Inheritance
- Class: The collection of items with similar characteristics and properties is called as class. It serves as a model or blueprint from which things can be made.
- Subclass/child class: A class that inherits or developed from another class is referred to be as a child class\subclass. The class, which is derived, the class which is extended is the sub class/child class which is other names for it.
- Super class / parent class: The class from which a subclass/child class retrieves its properties is said to as the super class or parent class. It is also referred to be as a super class/parent class or a base class.
- Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. The defined previous class methods and fields are same which can be implemented in inheritance.
Syntax of inheritance in java programming language:
class Subclass-name extends Superclass-name
{
//statements, fields, and methods
}
A new class which is extended from an existing or created class is shown by the extends keyword. The word "extends" means to make something more functional.
Sample Example of inheritance in java:
Employee is the parentclass, and Programmer is a subclass. Programmer IS-A Employee is how the two classes are related to one another. It implies that an employee is a type of programmer.
class Emp {
float sal=40000;
}
class Pmr extends Emp {
int offer =20000;
public static void main (String s []) {
Pmr t=new Pmr ();
System.out.println("Pmr salary is:"+t. sal);
System.out.println("Bonus of Pmr is:"+t. offer);
}
}
Output:
Pmr salary is:40000.0
Bonus of programmer is:20000
Code reuse is a field that the Programmer object in the example above can access from both its own class and the Employee class.
We have three types or levels if inheritances:
- Single level inheritance.
- Multilevel inheritance.
- Hiererchial inheritance.
1. Example program for single level inheritance using extend:
A single inheritance in java programming comes when one class is called from another class. In the example presented below, there is just one inheritance because the Kitchen class derives from the House class.
class House {
void kitchen () {System.out.println("Kitchen...");}
}
class Kitchen extends House {
void utensils() {System.out.println("Washing utensils...");}
}
class TestInheritance {
public static void main (String args []) {
Kitchenk=new Kitchen ();
k. Utensils ();
d.Kitchen ();
}}
Output:
Kitchen...
Washing utensils...
2. Example Program for Multilevel Inheritance using extend keyword:
Multilevel inheritance refers to a succession or further implementation of inheritances form or concept.
class Anml{
void et () {System.out.println("eating the prey...");}
}
class Dg extends Anml {
void brk () {System.out.println("barking loudly...");}
}
class BabyDog extends Dg {
void ep () {System.out.println("weeping high...");}
}
class Inh_Test_2{
public static void main (String s []) {
BabyDg b=new BabyDg ();
d.ep();
d.brk ();
d.et ();
}}
Output:
eating the prey...
barking loudly...
weeping high...
3. Example program of Hierarchical Inheritance:
Hierarchical inheritance is the process in which two or more classes inherit properties from a single class. There is hierarchical inheritance in the example shown below since the Dog and Cat classes inherit from the Animal class.
class Anml {
void et() {System.out.println("food eating...");}
}
class Dg extends Anml{
void brk () {System.out.println("barking loudly...");}
}
class Cat extends Anml {
void mow() {System.out.println("meowing sound...");}
}
class Test_Inh_3{
public static void main (String s []) {
Cat c=new Cat ();
c.mow ();
c.et ();
//c.brk ();
}}
Output:
Meowing sound...
Food eating...