Exploring the online world is my passion. have experience of #content writing #SEO #Digital Marketing #On-Page #Lead Generation #Content Analyst #Marketing Analyst...
I could never stop at one point, continuously wanted to acquire more and more skills. My work for any organization will be full of passion and hard work.
First we come to interface and abstract class, both are just only a thought that’s can use for achievingmultiple inheritance. When we know about some property of defined method, then we createan abstract class. Suppose that we create a class Bus and Car, then both havethe property of clutch,brake and gear. Then if we know some property of brake, so we create abstract class and defined a function brake and give some functionality of brake, means in abstract class we declare and definethe method. And this abstract class implemented by Bus and Car Classes. And if we want to changethe algorithm ofthe parent class (abstract class) method, then we change in our child class.
abstract class Bike { Bike() { System.out.println("bike is created"); } void changeGear() { System.out.println("gear changed"); } } class Honda extends Bike { void changeGear() { System.out.println("Gear change.."); } } class Hello { public static void main(String args[]) { Bike obj = new
Honda(); obj.changeGear(); } }
But if wedon't know aboutthe functionality ofmethod, then we createinterfaces and just define a method and extend in child class. Suppose if we not know about Clutch and Gearfunctionality, then we define a method inan interface and extend in class Bus and Car. And changethe algorithm ofthe parent class method (interface) in child class.
interface Bike { public void clutch(); public void gear(); } class Bus implements Bike { public void clutch () { System.out.println("implementation of clutch "); } public void gear () { System.out.println("implementation of gear "); } public static void main(String args[]) { Bike obj = new Bus (); obj. clutch (); } }
Liked By
Write Answer
When we use Interface and abstract class in Java?
Join MindStick Community
You have need login or register for voting of answers or question.
Hemant Patel
02-Mar-2017First we come to interface and abstract class, both are just only a thought that’s can use for achieving multiple inheritance. When we know about some property of defined method, then we create an abstract class. Suppose that we create a class Bus and Car, then both have the property of clutch, brake and gear. Then if we know some property of brake, so we create abstract class and defined a function brake and give some functionality of brake, means in abstract class we declare and define the method. And this abstract class implemented by Bus and Car Classes. And if we want to change the algorithm of the parent class (abstract class) method, then we change in our child class.
But if we don't know about the functionality of method, then we create interfaces and just define a method and extend in child class. Suppose if we not know about Clutch and Gear functionality, then we define a method in an interface and extend in class Bus and Car. And change the algorithm of the parent class method (interface) in child class.