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