×

Java Extends vs Implements

Java:

We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral, interpreted, high performance, dynamic as well as distributed.

Java programming language has been given by or introduced by James Gosling in the year 1995 with the help of sun micro systems. The architecture of java mainly consists of Java Virtual Machine ( JVM ), Java Runtime Environment ( JRE ), Java Development Kit ( JDK ) each with their respective features.
In java programming language we have many kinds of data types such as primitive data types which are categorized as boolean, character, integral, floating point etc and also non primitive data types. We also have different keywords, operators, variables etc.

Some such kind of key words or reserved words are extends and implements now let us know about extends and implements in detail.

Extends:

Extends is one of the keyword that is used in java programming language. It is mainly used for the purpose of inheritance. It means that a class can get the properties of the inherited class using the “ extends “  keyword. We can also say it as inheriting the properties of parent class.

We know that the java programming language will not support the concept of multiple inheritance. Hence we use extends keyword only to inherit a single class from the another class. This in turn provides us with less ambiguity.

In any program of inheritance if we use the “ extends “ keyword then there is no need of creating object for both parent class and child class we can only create the object for child class and we can access the parent class. We can say that it is an advantage of using the “ extends “ key word.

Syntax for using extends keyword:

Inorder to define a particular key word in java we need to know the syntax for defining a certain keyword and the syntax to define extends keyword is as shown below :

class Child class name  extends  Parent class name
                                ( or ) 
class Derived class name  extends  Base class name
                                ( or )
class Sub class name  extends  Super class name

  Now let us know about the extends keyword in detail by using an example with the following code as shown below :

Example:

import java.io.*;
import java.util.*;
class Parent
{
void show ( )
{
System.out.println( “ Parent – hi ” );
} // show method
} // parent class
class Child extends Parent
{
void show ( ) 
{
super.show ( );
System.out.println( “ Child – hi “ );
} // show method
} // child class
class Demo
{
public static void main( String args [ ] )
{
Child obj = new Child ( );
obj.show ( );
} // main method
} // demo class

Output:

Parent – hi 
Child – hi 

Implements:

Extends is one of the keyword that is used in java programming language. It is mainly used for the purpose of interface concept. An interface is an implementation of all the features required by the specific constants like an object. Interfaces are performed by using the “ implements “  keyword.

We know that the java programming language will not support the concept of multiple inheritance. Therefore we use the concept of interfaces inorder to perform the concept of multiple inheritance. Hence we use implements keyword to perform the multiple inheritance. This in turn provides us with less ambiguity.

We know that interface cannot create the object they can only create the reference to an object. Interfaces has full security. It only consists of the abstract methods. It is mainly performed by the implementation class.

In the interfaces concept the variables are public, final as well as static by default and also the methods in the interfaces are public and abstract by default. By using the “ implements “ keyword we can access many classes using a single interface.

Syntax for using extends keyword:

Inorder to define a particular key word in java we need to know the syntax for defining a certain keyword and the syntax to define implements keyword is as shown below :

class class_name  implements  interface_name 

Now let us know about the implements keyword in detail by using an example with the following code as shown below :

Example:

import java.io.*;
import java.util.*;
interface Employee
{
void salary ( );
void leave ( );
} // employee interface
class Worker implements Employee
{
public void salary ( )
{
System.out.println ( “ Salary paid to worker “ );
} // salary method
public void leave ( )
{
System.out.println (“ Leave granted to  worker “ );  
} // leave method
} // work class
class Manager implements Employee
{
public void salary ( )
{
System.out.println ( “ Salary paid to manager “ );
} // salary method
public void leave ( )
{
System.out.println (“ Leave granted to manager “ );  
} // leave method
} // manager class

Output:

Salary paid to worker
Leave granted to  worker
Salary paid to manager
Leave granted to  manager

Related Topics

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Java Code Coverage Tools

Code coverage testing is a crucial metric that gauges how thoroughly the program's source code has been tested. The market is flooded with Code Coverage Tools, making it difficult to...

7 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.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.