Interfaces and Abstract Classes

 

🔷 Interfaces in Java

An interface in Java is a reference type similar to a class, but it is used to specify a contract that a class must follow. It contains abstract methods (without implementation) and constants. From Java 8 onwards, interfaces can also have default and static methods.


✅ Key Points

  • Declared using the interface keyword.

  • All methods in an interface are public and abstract by default (until Java 7).

  • All fields are public, static, and final (i.e., constants).

  • A class implements an interface using the implements keyword.

  • A class can implement multiple interfaces (Java supports multiple interface inheritance).

  • Interfaces cannot have constructors.


🧩 Syntax

interface MyInterface {
void display(); // implicitly public and abstract }

✅ Implementing an Interface


interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("Bark"); } } public class Test { public static void main(String[] args) { Dog d = new Dog(); d.makeSound(); // Output: Bark } }

✳️ Java 8+ Enhancements

Interfaces can now have:

  • default methods: with a body

  • static methods: utility methods


interface Printable { void print(); default void show() { System.out.println("Default show method"); } static void greet() { System.out.println("Hello from interface!"); } }

🔷 Abstract Classes in Java

An abstract class in Java is a class that cannot be instantiated, and it is used to define common behavior for a group of related subclasses. It may contain both:

  • Abstract methods (without a body),

  • Concrete methods (with a body),

  • Fields (variables), and

  • Constructors.


✅ Definition


abstract class ClassName { // abstract method (no body) abstract void methodName(); // concrete method void normalMethod() { System.out.println("This is a concrete method."); } }

🔑 Key Points

  • Use the abstract keyword to declare an abstract class or method.

  • An abstract class cannot be instantiated directly.

  • If a class contains at least one abstract method, it must be declared abstract.

  • Subclasses must override all abstract methods unless they are also declared abstract.


✅ Example


abstract class Animal { abstract void makeSound(); // abstract method void eat() { // concrete method System.out.println("This animal eats food."); } } class Dog extends Animal { void makeSound() { System.out.println("Dog barks"); } } public class TestAbstract { public static void main(String[] args) { Dog d = new Dog(); d.makeSound(); // Output: Dog barks d.eat(); // Output: This animal eats food. } }

📌 When to Use Abstract Classes?

  • When you want to share code among several related classes.

  • When you want to define a template for future subclasses.

  • When you want to partially implement a class and leave the rest for subclasses.


🆚 Abstract Class vs Interface (Quick Comparison)

Feature        Abstract Class                    Interface
Can have fields            ✅ Yes                ❌ No (only constants)
Constructors            ✅ Yes                ❌ No
Concrete methods                ✅ Yes                ✅ Yes (from Java 8)
Inheritance        Single inheritance                Multiple implementation
Instantiation        ❌ Not allowed                ❌ Not allowed


📊 3. Interface vs Abstract Class: Comparison Table

FeatureInterfaceAbstract Class
Keyword     interface     abstract
Method types             Only abstract (till Java 7); from Java      8, default and static        Abstract and concrete                         methods
Access modifier for methods        All methods are public by default            Can be private, protected,                etc.
Fields        Only public static final                       (constants)        Can have instance variables
Constructors        No        Yes
Multiple inheritance        Yes (a class can implement many)        No (single inheritance)
Inheritance keyword    implements     extends
Instantiable        No            No
Use case        When you need a contract (what to do)When you need partial implementation (how to do)

Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Inheritance and Polymorphism