×

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:

  1. Single level inheritance.
  2. Multilevel inheritance.
  3. 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...

Related Topics

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.

Java Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.