×

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

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.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Pattern Programs in Java

Pattern Programs in Java In Java, pattern programs are the most important from the perspective of interviews. The pattern programs improve thinking and coding skills. It also helps us to develop a...

27 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

How to Create a Generic List in Java?

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take type (Integer, String, etc., and user-defined types) as a parameter....

4 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.