difference between abstract class and interface - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

difference between abstract class and interface

Difference between abstract class and interface

 

Interface

Interface is fully unimplemented class it is used for declaring a set of operation of an object.

like class interface is created by using the keyword interface.

Variables in interface by default public static final. 

Even if we create variable as non-static compiler converts non-static into static.

interface restricts the other access modifiers i.e., private, protected...!

we should initialize variable at the time of variable declaration.

interface supports only abstract methods no concreate methods. 

Interface methods are by default public abstract.

Since Java8 we can implement methods in interface by using default and static keyword.

The key word ‘implements’ is used in class to provide implementation for an interface.

 

interface Car 
{
               vehicleSpeed();
}
class Vehicle implements Car {   
               //override interface method
               @Override
               vehicleSpeed(){
            //write vehicle spped logic hear
}}

 

Interface tells what should implement but not how.

Java doesn’t support multiple inheritance in case of class. 

But if we use interface, we can achieve multiple inheritance because a class can implement multiple interfaces.

 

public class Vehicle implements Bike, Car {
 }
 interface Bike {
}
 interface Car {
}

interface can extend another interfaces but an interface can not implement another interface.

for an interface not possible to create an object. but we can create reference variables of an interface.

reference variable can be created for storing its sub class object references, to develop loosely coupled user applications to get runtime polymorphism.

Interface is mainly used to achieve loosely coupling.

We must design an object start with interface if its operations have different implementations.

 
Abstract class

Interface and abstract class both used for abstraction but in case of interface we can have only abstract methods.

But in abstract class we can have abstract methods as well as concrete methods.

Abstract class is created by using the abstract keyword.

Abstract class can have any public, private, protected etc... variables. but in interface we can have static final variables only.

If we have at least one abstract method that class should be declared as abstract class.

Abstract method is a method, if method is having declaration and not having definition is called abstract method.

Abstract class is partially implemented class is used for developing some of operations of an object which are common for all next level sub-classes.

The keyword ‘extend’ is used to extend an abstract class.

for an abstract class not possible to create an object.

but we can create reference of an abstract class. 

An abstract class we can extends another Java class and implement multiple Java interfaces.

Abstraction is hiding details of implementation. and it reduces the complexity of viewing the things.


No comments:

Post a Comment