Implement v/s Extends in JAVA
Here, we will discuss about implement and extend with the help of code example.
What is Extends?
As the name says to extend. It means taking all the methods and definition from parent class(super class) and modify them as per your needs or no need to modify.
When a class extend another class it becomes subclass and the extended class becomes superclass.
for e.g. There are two classes A and B
When Class B extends the class A, B becomes subclass(child) and A becomes superclass(parent).
B can use/reuse all the features of class A.
Class A:
public class A {
public void show() {
System.out.println("Class A");
}
}
What is Extends?
As the name says to extend. It means taking all the methods and definition from parent class(super class) and modify them as per your needs or no need to modify.
When a class extend another class it becomes subclass and the extended class becomes superclass.
for e.g. There are two classes A and B
When Class B extends the class A, B becomes subclass(child) and A becomes superclass(parent).
B can use/reuse all the features of class A.
Class A:
public class A {
public void show() {
System.out.println("Class A");
}
}
Class B:
public class B extends A {
public void display() {
System.out.println("Class B display");
}
public void show() {
System.out.println("Class B show");
}
}
Class Main:
public class Main {
public static void main(String[] args) {
A b = new B();
b.show(); // B is extending A, it will call the Class B show method
A a = new A();
a.show(); // it will call the Class A show method
}
}
Output:
Class B show
Class A
Implement:-
Implement general means to build something that you are aware how to build like its characteristics, definition, how it should look etc.
For implementation we need to have an interface which will tell us about the implementation, what should be its features.
It does not have any body. It will contain only method which has to be implemented in the class which is implementing.
for e.g Vehicle is an interface. it can tell us different types of vehicle, its seating capacity, no of tyres, what kind of engine it is etc.
Interface vehicle:-
As you can see that it does not have any body. It is just the list of operations which has to be implemented in abstract class.
public interface Vehicle {
public int number_of_tyres();
public String engine();
public int seating_capacity();
}
Class Car:-
It is implementing the vehicle interface and defining its structure. While implementing you need to implement all the methods you cannot leave any.
public class Car implements Vehicle {
@Override
public int number_of_tyres() {
// TODO Auto-generated method stub
return 4;
}
@Override
public String engine() {
// TODO Auto-generated method stub
return "Petrol";
}
@Override
public int seating_capacity() {
// TODO Auto-generated method stub
return 5;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int no_of_tyres, capacity;
String type;
Car car = new Car();
no_of_tyres = car.number_of_tyres();
System.out.println("No of Tyres is " + no_of_tyres);
type = car.engine();
System.out.println("Engine type is " + type);
capacity = car.seating_capacity();
System.out.println("Seating Capacity is " + capacity);
}
}
Output:
Output:
No of Tyres is 4
Engine type is Petrol
Seating Capacity is 5
Engine type is Petrol
Seating Capacity is 5
Class Auto:
Another example for implementation
public class Auto implements Vehicle {
@Override
public int number_of_tyres() {
// TODO Auto-generated method stub
return 3;
}
@Override
public String engine() {
// TODO Auto-generated method stub
return "Diesel";
}
@Override
public int seating_capacity() {
// TODO Auto-generated method stub
return 4;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int no_of_tyres, capacity;
String type;
Auto auto = new Auto();
no_of_tyres = auto.number_of_tyres();
System.out.println("No of Tyres is " + no_of_tyres);
type = auto.engine();
System.out.println("Engine type is " + type);
capacity = auto.seating_capacity();
System.out.println("Seating Capacity is " + capacity);
}
}
Output:
Output:
No of Tyres is 3
Engine type is Diesel
Seating Capacity is 4
Engine type is Diesel
Seating Capacity is 4
Comments
Post a Comment