×

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

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 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 Identifiers

In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many...

3 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Sorting Program in Java

Sorting Program in Java: The sorting program in Java is used to sort arrays either in ascending or descending order.  There are two predefined methods available in Java that are...

6 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.