14 July 2023

Difference between Abstract class and Interface In Java - learngreen.net


Difference between Abstract class and Interface In Java


In Java, each abstract class and interface are used to define common behaviors and supply a contract for classes to implement. However, there are countless key variations between abstract classes and interfaces:


1. Definitions:-

- Abstract Class:- An abstract type is a class that cannot be instantiated. It serves as a blueprint for derived classes and can comprise both abstract and non-abstract methods. It may also encompass instance variables, constructors, and approach implementations.

- Interface:- An interface is a collection of summary methods that outline a contract for classes to implement. It can't include any instance variables or method implementations, only method signatures. It is implemented by way of lessons to provide particular behavior.


2. Instantiation:-

- Abstract Class:- An abstract type can't be instantiated directly with the usage of the 'new' keyword. Instead, it must be extended by way of a subclass, and the subclass can be instantiated.

- Interface:- An interface can't be instantiated at all. It can only be implemented via classes, which ability a class has to provide implementations for all the strategies declared in the interface.


3. Multiple Inheritance:

- Abstract Class:- A class can extend only one abstract class due to the fact Java does now not aid more than one inheritance for classes.

- Interface:- A class can implement more than one interface. This lets in for achieving multiple inheritance of behavior through implementing a couple of interfaces.


4. Method Implementation:

- Abstract Class:- An abstract type can provide approach implementations (non-abstract methods) along with abstract methods. Subclasses can select to override or use the provided method implementations.

- Interface:- An interface can't provide method implementations. It only broadcasts method signatures that implementing instructions need to define.


5. Access Modifiers:

- Abstract Class:- An abstract category can have abstract methods with any access modifier (public, private, protected, or default) and non-abstract techniques with any access modifier.

- Interface:- All methods declared in an interface are implicitly public and abstract. It is no longer indispensable to specify these modifiers explicitly.


6. Usage:-

- Abstract Class:- Abstract classes are often used when there is a joint base implementation among the derived classes. They supply a way to share code and common functionality.

- Interface:- Interfaces are used when there is a want for multiple unrelated classes to share a common behavior. They define a contract that ensures classes adhere to a particular behavior except imposing any implementation details.


In general, if you favor furnishing a common base implementation or if you need to acquire a couple of inheritance of behavior, a summary class is a higher choice. On the other hand, an interface is more appropriate if you want to outline a contract for unrelated training to implement a specific behavior.





 //Abstract class Example
 abstract class City {
 protected String name;
    
 public City(String name) {
 this.name = name;
    }
 public abstract void area(); 
 public void special() {
 System.out.println
 (name + " Education");
    }
 }

 class CityOne extends City {
 public CityOne(String name) {
 super(name);
    }
    
 @Override
 public void area() {
 System.out.println
 (name + " is a large city");
    }
    
 @Override
 public void special() {
 System.out.println
 (name + " has a rich cultural heritage");
    }
 }
 public class Class {
 public static void main(String[] args) {
 CityOne obj = new CityOne("Pune");
 obj.area();
 // Output-> Pune is a large city          
 obj.special();
 // Output-> Pune has a rich cultural heritage        
    }
 }
 

 


 
 //Interface Example
 
 interface Project {
 oid start();
 void stop();
 }
 class Engineering implements Project {
 @Override
 public void start() {
 System.out.println
 ("Engineering Work started");
    }
 @Override
 public void stop() {
 System.out.println
 ("Engineering Work stopped");
    }
 }
 class Pharmacy implements Project {
 @Override
 public void start() {
 System.out.println
 ("Pharmacy work started");
    }
    
 @Override
 public void stop() {
 System.out.println
 ("Pharmacy work stopped");
    }
 }
 public class MainClass {
 public static void main(String[] args) {
 Project obj = new Engineering();
 obj.start();
 // Output-> Engineering Work started  
 obj.stop();
 // Output-> Engineering Work stopped   
 Project obj2 = new Pharmacy();
 obj2.start();
 // Output-> Pharmacy work started
 obj2.stop();
 // Output-> Pharmacy work stopped   
    }
 }
 

 



No comments:

Post a Comment