×

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

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Pyramid Program in Java

Pyramid Program in Java In the previous section, we have discussed about the number pattern programs in Java. The logic for the number pattern and pyramid pattern is the same except...

2 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.