Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Difference Between Abstract Class and Interface in Java

What Is Abstract Class in Java

  • A class is said to be abstract if the word "abstract" appears in the class declaration. Abstract classes in Java are intended to be subclassed by other classes and cannot be instantiated by themselves. The function of an abstract class is to specify a common set of methods and attributes that its subclasses must follow or override. By using abstract classes, concrete class blueprints can be created. The inheriting class must, however, implement the abstract method.

  • Abstract classes can have both regular methods with implementations and abstract methods that are declared but have no implementation in the abstract class itself. Subclasses of an abstract class are required to provide concrete implementations for these abstract methods. Typically, a hierarchy of related classes with shared behaviours or attributes is created using abstract classes.


Table Of Content

  • Definition
  • Comparison Chart
  • Key Differences


Key Benefits of Using Abstract Classes

  • For the subclasses, abstract classes provide default functionality.
  • abstract classes designs a template for upcoming targeted classes
  • Abstract classes assists you in defining a common interface for its subclasses.
  • The abstract class enables code reuse.




Abstract Class example
 // Abstract Class  example  
abstract class nano{
	abstract void Speed();
	  public void Weight() {
		   System.out.println("250 Kg");
	  }
}
class Vehicles extends nano{    
	@Override
	 void Speed() {
		System.out.println("90 kmph");	
	 }  
}
class Main{
	public static void main(String[] args) {
		Vehicles vehicles=new Vehicles();
		vehicles.Speed();
		vehicles.Weight();
	}
}




Output:

90 kmph 
250 Kg 


Abstract Method in Java

An abstract method is one that is declared with abstract keyword but does not have implementation.


Syntax

 abstract void Exam();  // does not have implementation  



What is Interface in java

    In Java, an interface is a reference type that details a collection of abstract methods or a contract that a class must adhere to. Without specifying how those methods should be implemented, it lists the methods that a class must offer. In other words, an interface specifies what a class that implements it should do, but not how.

    Using interfaces to achieve abstraction, you can distinguish between how a behaviour is defined and how it is actually implemented. You can write more flexible and maintainable code by clearly separating the implementation from the interface (what should be done and how it should be done).




Key Benefits of Using Interface

  • Abstraction is accomplished using interfaces.
  • Designed to support run-time dynamic method resolution
  • You can achieve loose coupling with the use of an interface.
  • Interface enables you to distinguish between how a method is defined and its inheritance structure.


Java Interface example
 // Java method overridden example  
interface Displayable {
	void display();
}
class Test implements Displayable {
	public void display(){
	System.out.println("Hello");
	}
}
class Main{
	public static void main(String[] args){
		Test test = new Test();
		test.display();
	}
}



Output:

Hello 

Comparison Chart

Difference Between Interface and Abstract class are mention below


S.N Aspect Interface Abstract class
1. Methods Interface can have only abstract methods. An abstract class can have abstract and non-abstract methods. Java 8 allows for the addition of default and static methods. It can also have private concrete methods as of Java 9.
2. Variables By default, variables declared in a Java interface are final,public and static. Abstract class can have only Non-final variables.
3. Access The interface's definitions are all assumed to be public by default. A Java abstract class may have class members such as private, protected, and so on.
4. Implementation Interface use "implements" keyword for implementation The keyword "extends" can be used to extend an abstract class.
5. Inheritance Interface supports multiple inheritance Multiple inheritance is not supported by the abstract class.
6. Code Since an interface is abstract, it does not provide any code implementation An abstract class can provide Default code with optional overrides



Interface: Embracing Pure Abstraction

  • Methods: Interfaces consist solely of abstract methods, enforcing a contract.
  • Variables: Interface variables are final, public, and static by default.
  • Access: All components are inherently public.
  • Implementation: Implemented using the "implements" keyword.
  • Inheritance: Supports multiple inheritance.
  • Coding Possibilities: No concrete code provided, meant for implementation.


Abstract Class: Abstraction 

  • Methods: Combines abstract and concrete methods.
  • Variables: Non-final variables can be utilized.
  • Access: Provides flexibility in access modifiers.
  • Implementation: Extended using the "extends" keyword.
  • Inheritance: Allows single inheritance.
  • Coding Possibilities: Provides default concrete methods.



The Most Significant Differences Between Abstract class and Interface

  • A class can implement multiple interfaces in Interface, but it can only inherit one Abstract Class.
  • No Access modifiers are within an Interface. Every element within an Interface is inherently regarded as having a public modifier. On the other hand, an Abstract Class retains the possibility of possessing an access modifier.
  • Data fields are not permitted in the Interface, but they are permitted in the abstract class.
  • Overloading achieves early binding by resolving which overloaded function will be run during build time. Overriding achieves late binding by resolving which overridden function will be invoked during runtime.
Java Encapsulation Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Abstraction
Java Interface
Abstract class vs Interface
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.