0% found this document useful (0 votes)
5 views4 pages

Java Abstract Classes vs. Interfaces

Uploaded by

Ayush
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java Abstract Classes vs. Interfaces

Uploaded by

Ayush
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Abstract Classes vs.

Interfaces in Java

August 16, 2025

1 Introduction
Abstract classes and interfaces in Java are mechanisms for achieving abstraction, but
they serve distinct purposes. This document compares their features, explains their use
cases, and provides code examples to illustrate their application.

2 Tabulated Comparison

Table 1: Abstract Class vs. Interface


Aspect Abstract Class Interface
A class declared with the A contract defining method
Definition abstract keyword that cannot be signatures that classes
instantiated. implement.
Methods are abstract by default;
Can have abstract (no
since Java 8, can have default
Implementation implementation) and concrete
and static methods with
(with implementation) methods.
implementation.
Can have instance and static Only constants (public static
Variables
variables of any type. final).
Multiple inheritance via
Inheritance Single inheritance via extends.
implements.
Can have constructors to
Constructors No constructors.
initialize instance variables.
Members can be public, All members are implicitly
Access Modifiers
protected, private, etc. public.
Base for related classes with Define capabilities for unrelated
Use Case
shared code or state. classes.
Default methods allow adding
Adding new abstract methods
Extensibility functionality without breaking
breaks subclasses.
implementers.

1
3 Explanation
3.1 Abstract Classes
Abstract classes are used when you want to define a template for a group of related classes
that share common behavior or state. They are ideal for a hierarchy where subclasses
inherit shared code or data. Key features include:

• Can include both abstract methods (must be implemented by subclasses) and con-
crete methods (shared implementation).

• Can have instance and static variables, allowing state management.

• Support constructors to initialize instance variables.

• Limited to single inheritance, as a class can only extend one abstract class.

Use an abstract class when you have a “is-a” relationship (e.g., a Car is a Vehicle) and
need shared logic.

3.2 Interfaces
Interfaces define a contract of methods that classes can implement, typically to specify
capabilities. They are used for unrelated classes that share behaviors. Key features
include:

• Historically, all methods were abstract; since Java 8, default and static methods
can have implementation.

• Variables are implicitly public static final (constants).

• Support multiple inheritance, allowing a class to implement multiple interfaces.

• No constructors, as interfaces do not manage instance state.

Use an interface for a “can-do” relationship (e.g., a Car can be Drivable).

3.3 When to Use


• Abstract Class: Use for a family of related classes with shared code, such as a
Vehicle class extended by Car and Bike.

• Interface: Use to define behaviors across unrelated classes, such as a Drivable in-
terface implemented by Car and Boat.

4 Code Examples
4.1 Abstract Class Example
This example shows an abstract Vehicle class with a static variable, instance variable,
and shared logic.

2
1 abstract c l a s s Vehicle {
2 s t a t i c i n t t o t a l V e h i c l e s = 0 ; // S t a t i c v a r i a b l e
3 S t r i n g brand ; // I n s t a n c e v a r i a b l e
4

5 V e h i c l e ( S t r i n g brand ) {
6 t h i s . brand = brand ;
7 t o t a l V e h i c l e s ++;
8 }
9

10 // C o n c r e t e method
11 void startEngine ( ) {
12 System . out . p r i n t l n ( brand + ” e n g i n e s t a r t e d . ” ) ;
13 }
14

15 // A b s t r a c t method
16 a b s t r a c t v o i d move ( ) ;
17 }
18

19 c l a s s Car e x t e n d s V e h i c l e {
20 Car ( S t r i n g brand ) {
21 s u p e r ( brand ) ;
22 }
23

24 @Override
25 v o i d move ( ) {
26 System . out . p r i n t l n ( ” Car i s d r i v i n g . ” ) ;
27 }
28

29 // I n s t a n c e method a c c e s s i n g s t a t i c v a r i a b l e
30 void di s pl a yT o ta l Ve h ic l es ( ) {
31 System . out . p r i n t l n ( ” T o t a l v e h i c l e s : ” + t o t a l V e h i c l e s ) ;
32 }
33 }
34

35 p u b l i c c l a s s Main {
36 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
37 Car c a r = new Car ( ” Toyota ” ) ;
38 c a r . s t a r t E n g i n e ( ) ; // Output : Toyota e n g i n e s t a r t e d .
39 c a r . move ( ) ; // Output : Car i s d r i v i n g .
40 c a r . d i s p l a y T o t a l V e h i c l e s ( ) ; // Output : T o t a l v e h i c l e s : 1
41 }
42 }

4.2 Interface Example


This example shows a Drivable interface with a default method and a static method,
implemented by the Car class.
1 i n t e r f a c e Drivable {
2 // A b s t r a c t method

3
3 void drive ( ) ;
4

5 // D e f a u l t method
6 d e f a u l t v o i d honk ( ) {
7 System . out . p r i n t l n ( ”Honk ! Honk ! ” ) ;
8 }
9

10 // S t a t i c method
11 s t a t i c void d e s c r i b e ( ) {
12 System . out . p r i n t l n ( ” This i s a d r i v a b l e v e h i c l e . ” ) ;
13 }
14 }
15

16 c l a s s Car implements D r i v a b l e {
17 s t a t i c i n t t o t a l C a r s = 0 ; // S t a t i c v a r i a b l e
18

19 Car ( ) {
20 t o t a l C a r s ++;
21 }
22

23 @Override
24 public void drive ( ) {
25 System . out . p r i n t l n ( ” Car i s d r i v i n g . ” ) ;
26 }
27

28 // I n s t a n c e method a c c e s s i n g s t a t i c v a r i a b l e
29 void displayTotalCars ( ) {
30 System . out . p r i n t l n ( ” T o t a l c a r s : ” + t o t a l C a r s ) ;
31 }
32 }
33

34 p u b l i c c l a s s Main {
35 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
36 Car c a r = new Car ( ) ;
37 c a r . d r i v e ( ) ; // Output : Car i s d r i v i n g .
38 c a r . honk ( ) ; // Output : Honk ! Honk !
39 D r i v a b l e . d e s c r i b e ( ) ; // Output : This i s a d r i v a b l e v e h i c l e .
40 c a r . d i s p l a y T o t a l C a r s ( ) ; // Output : T o t a l c a r s : 1
41 }
42 }

You might also like