Skip to main content

Abstraction

Abstraction in Java​

1. What is Abstraction?​

Abstraction is the process of hiding irrelevant details and exposing only the essential features of an object. It can be achieved using abstract classes or interfaces.

2. Key Points About Abstraction​

  • The abstract keyword is used to declare an abstract class and abstract methods.
  • An abstract method is a method that does not have a body; it must be implemented by subclasses.
  • If a class contains at least one abstract method, it must be declared as an abstract class.

image

3. Example of Abstraction in Java​

// Abstract class Teacher
abstract class Teacher {
// Abstract methods (no body)
abstract void teach();
abstract void project();
}

// JavaTeacher class extending Teacher
class JavaTeacher extends Teacher {
@Override
void teach() {
System.out.println("Teaching Java programming");
}

@Override
void project() {
System.out.println("Guiding Java project");
}
}

// PythonTeacher class implementing Teacher
class PythonTeacher extends Teacher {
@Override
void teach() {
System.out.println("Teaching Python programming");
}

@Override
void project() {
System.out.println("Guiding Python project");
}
}

// Main class - TeacherApp
public class TeacherApp {
// Static method to demonstrate teacher behavior
static void teacherBehavior(Teacher teacher) {
teacher.teach();
teacher.project();
}

public static void main(String[] args) {
// Creating objects of JavaTeacher and PythonTeacher
Teacher javaTeacher = new JavaTeacher();
Teacher pythonTeacher = new PythonTeacher();

// Calling static method with different teacher types
teacherBehavior(javaTeacher);
teacherBehavior(pythonTeacher);
}
}

4. Explanation of the Code​

  • Abstract Class Teacher: Defines abstract methods teach() and project(), which must be implemented by subclasses.
  • Concrete Classes JavaTeacher and PythonTeacher: Extend Teacher and provide implementations for teach() and project().
  • Static Method teacherBehavior(): Accepts a Teacher object and calls its methods, demonstrating polymorphism.
  • Main Method Execution:
    1. Creates instances of JavaTeacher and PythonTeacher.
    2. Calls teacherBehavior() to execute their methods.

5. Key Takeaways​

  • Abstraction helps in designing flexible and reusable code.
  • Abstract classes allow defining common functionality while forcing subclasses to implement specific behaviors.
  • Static methods can be used to handle different object types polymorphically.
  • Abstraction reduces code complexity by exposing only relevant details.

Pure and Impure Abstract Classes in Java​

1. What is an Abstract Class?​

An abstract class is a class that cannot be instantiated and may contain abstract methods (methods without a body) as well as concrete methods (methods with a body).

2. Types of Abstract Classes​

(A) Pure Abstract Class​

A pure abstract class is an abstract class where all methods are abstract (i.e., have no body).

// Pure Abstract Class
abstract class Program1 {
abstract void display1();
abstract void display2();
}
(B) Impure Abstract Class (Type 1)​

An impure abstract class is an abstract class that contains at least one abstract method and at least one concrete method.

// Impure Abstract Class with one abstract and one concrete method
abstract class Program2 {
abstract void display1(); // Abstract method

void display2() { // Concrete method
System.out.println("Inside concrete method display2()");
}
}
(C) Impure Abstract Class (Type 2)​

Another type of impure abstract class where both methods are concrete but the class is still abstract.

// Impure Abstract Class with all concrete methods
abstract class Program3 {
void display1() {
System.out.println("Inside concrete method display1()");
}

void display2() {
System.out.println("Inside concrete method display2()");
}
}

3. Why Can't Abstract Classes Be Instantiated?​

  • An abstract class is incomplete because it may have abstract methods that do not have an implementation.
  • If an object is created for an abstract class, it would not be able to call abstract methods as they have no body.
  • Instead, abstract classes must be inherited by a subclass, which provides implementations for abstract methods.

4. Example Demonstrating Instantiation Restriction​

abstract class Example {
abstract void show(); // Abstract method
}

public class Main {
public static void main(String[] args) {
// Example obj = new Example(); // Error! Abstract classes cannot be instantiated
}
}

5. Key Takeaways​

  • A pure abstract class has only abstract methods.
  • An impure abstract class has a mix of abstract and concrete methods.
  • Abstract classes cannot be instantiated but can be used as a base class for inheritance.
  • Subclasses must implement abstract methods before they can be instantiated.
  • Concrete methods in an abstract class provide shared functionality without requiring every subclass to implement them.

Partial Abstraction in Java​

1. What is Partial Abstraction?​

  • If a child class inherits an abstract class that contains abstract methods, the child class must override all abstract methods.
  • If the child class does not provide implementations for all abstract methods, it must be declared abstract as well.
  • A partially abstract class is an abstract class that implements some but not all of its abstract methods.

2. Example of Partial Abstraction​

// Abstract class Vehicle
abstract class Vehicle {
abstract void speed(); // Abstract method
abstract void seater(); // Abstract method
}

// Abstract class Car extending Vehicle (Partial Abstraction)
abstract class Car extends Vehicle {
@Override
void speed() { // Implementing only speed method
System.out.println("Car has a speed limit of 200 km/h");
}
// 'seater()' is not implemented, so Car remains abstract
}

// Concrete class Bike extending Vehicle (Full Implementation)
class Bike extends Vehicle {
@Override
void speed() {
System.out.println("Bike has a speed limit of 150 km/h");
}

@Override
void seater() {
System.out.println("Bike is a 2-seater");
}
}

// Main class
public class VehicleApp {
public static void main(String[] args) {
// Vehicle vehicle = new Vehicle(); // Error! Abstract classes cannot be instantiated
// Car car = new Car(); // Error! Abstract classes cannot be instantiated

Bike bike = new Bike(); // Creating an instance of Bike
bike.speed(); // Calls speed method
bike.seater(); // Calls seater method
}
}

3. Explanation of the Code​

  • Abstract Class Vehicle: Contains two abstract methods speed() and seater().
  • Partial Abstract Class Car: Implements only the speed() method but not seater(), so it remains abstract.
  • Concrete Class Bike: Implements both speed() and seater(), making it a complete class.
  • Instantiation Rules:
    • Abstract classes cannot be instantiated (new Vehicle() and new Car() would cause errors).
    • Concrete classes must implement all abstract methods before they can be instantiated.

4. Key Takeaways​

  • Partial abstraction allows abstract classes to provide some implementations while leaving others for subclasses.
  • If a child class does not implement all abstract methods, it must also be declared abstract.
  • Concrete subclasses must provide implementations for all inherited abstract methods.
  • Abstract classes help in designing a flexible and reusable object-oriented structure.

Restrictions on Abstract Methods in Java​

1. Why Can't Abstract Methods Be final or static?​

(A) Abstract Methods Cannot Be final​
  • An abstract method must be overridden by a subclass to provide its implementation.
  • A final method cannot be overridden, which contradicts the purpose of an abstract method.
  • Opposing Statements:
    • Abstract methods must be overridden.
    • Final methods cannot be overridden.
  • This conflict makes it impossible to declare an abstract method as final.
(B) Abstract Methods Cannot Be static​
  • A static method belongs to a class, not an instance.
  • Abstract methods are meant to be implemented in subclasses, but a static method cannot be overridden.
  • This means declaring an abstract method as static contradicts its purpose.

2. Example Demonstrating Errors​

// Abstract class Vehicle
abstract class Vehicle {
// Valid final method (Concrete method in abstract class)
final void speed() {
System.out.println("Speed limit is 100 km/h");
}

// Invalid: Abstract methods cannot be final
// abstract final void seater(); // ERROR

// Invalid: Abstract methods cannot be static
// abstract static void typeEngine(); // ERROR
}

3. Explanation of Errors​

  • final method speed() is valid because it has a body and cannot be overridden.
  • abstract final void seater(); causes an error because abstract methods must be overridden, but final methods cannot be.
  • abstract static void typeEngine(); causes an error because static methods belong to a class and cannot be overridden.

4. Key Takeaways​

  • Abstract methods cannot be final because they must be overridden.
  • Abstract methods cannot be static because static methods belong to the class and cannot be overridden.
  • Abstract classes can contain final or static concrete methods, but not abstract ones.
  • Using abstract methods correctly ensures proper inheritance and polymorphism in Java.

Understanding Static and Non-Static Members in an Abstract Class​

1. Components of an Abstract Class​

An abstract class in Java can contain:

  • Static variables: Shared among all instances of the class.
  • Static methods: Can be called without creating an object.
  • Static blocks: Executed once when the class is loaded.
  • Non-static (instance) variables: Belong to an instance of the class.
  • Non-static blocks: Execute when an object is created, before the constructor.
  • Constructors: Used to initialize instance variables.
  • Abstract methods: Must be implemented by subclasses.

2. Example of an Abstract Class with Static and Non-Static Members​

// Abstract class Vehicle
abstract class Vehicle {
static int x; // Static variable
int y; // Non-static variable

// Static block (Executes when class loads)
static {
System.out.println("Inside static block");
x = 10;
}

// Static method (Can be called without an object)
static void display1() {
System.out.println("Inside static method display1()");
System.out.println("Value of x: " + x);
}

// Non-static block (Executes before constructor when an object is created)
{
System.out.println("Inside non-static block");
y = 20;
}

// Constructor
Vehicle() {
System.out.println("Inside Vehicle constructor");
}

// Non-static method
void display2() {
System.out.println("Inside non-static method display2()");
System.out.println("Value of x: " + x);
System.out.println("Value of y: " + y);
}

// Abstract method (Must be implemented by subclasses)
abstract void display3();
}

3. Explanation of the Code​

  • Static Variable x: Initialized in the static block and shared across all objects.
  • Static Block: Executes once when the class is loaded.
  • Static Method display1(): Prints the value of x and can be called without an object.
  • Non-static Variable y: Initialized in the non-static block.
  • Non-static Block: Runs when an object is created, before the constructor.
  • Constructor: Runs when an object is instantiated.
  • Non-static Method display2(): Prints values of x and y.
  • Abstract Method display3(): Must be implemented by a subclass.

4. Key Takeaways​

  • Static members belong to the class, whereas non-static members belong to instances.
  • Static blocks execute once when the class is loaded.
  • Non-static blocks execute before the constructor when an object is created.
  • Abstract methods must be implemented in subclasses.
  • Static methods cannot access non-static variables directly.
  • Static methods can be called without an instance, but non-static methods require an object.

This structure ensures better understanding and organization of Java classes containing static and non-static members.

Abstraction Using Interfaces in Java​

1. What is an Interface?​

  • An interface consists only of abstract methods (prior to Java 8).
  • It is another way of achieving abstraction in Java.
  • A class that implements an interface must provide implementations for all abstract methods or be declared abstract.
  • implements keyword is used by a class to inherit an interface.

2. Example of Abstraction Using an Interface​

// Interface defining student activities
interface StudentActivities {
void study(); // Abstract method
void practice(); // Abstract method
}

// Class implementing StudentActivities interface for Physics student
class PhysicsStudent implements StudentActivities {
@Override
public void study() {
System.out.println("Physics student is studying Physics concepts.");
}

@Override
public void practice() {
System.out.println("Physics student is solving numerical problems.");
}
}

// Class implementing StudentActivities interface for Math student
class MathStudent implements StudentActivities {
@Override
public void study() {
System.out.println("Math student is studying algebra and calculus.");
}

@Override
public void practice() {
System.out.println("Math student is solving equations and proofs.");
}
}

// Main class with static method to invoke student activities
public class StudentApp {
static void invokeStudentActivity(StudentActivities student) {
student.study();
student.practice();
}

public static void main(String[] args) {
PhysicsStudent physicsStudent = new PhysicsStudent();
MathStudent mathStudent = new MathStudent();

System.out.println("Physics Student Activities:");
invokeStudentActivity(physicsStudent);

System.out.println("\nMath Student Activities:");
invokeStudentActivity(mathStudent);
}
}

3. Explanation of the Code​

  • Interface StudentActivities:
    • Declares two abstract methods: study() and practice().
  • Class PhysicsStudent:
    • Implements the StudentActivities interface.
    • Provides concrete implementations for study() and practice().
  • Class MathStudent:
    • Implements the StudentActivities interface.
    • Provides concrete implementations for study() and practice().
  • Static Method invokeStudentActivity(StudentActivities student):
    • Calls study() and practice() on any object that implements StudentActivities.
  • Main Method:
    • Creates instances of PhysicsStudent and MathStudent.
    • Calls invokeStudentActivity() for both students.

4. Key Takeaways​

  • Interfaces in Java help achieve abstraction by enforcing method implementation.
  • A class implementing an interface must provide method bodies for all abstract methods.
  • Interfaces promote loose coupling and enable multiple inheritance.
  • Static methods in a separate class can be used to invoke interface methods dynamically.

Why Were Interfaces Introduced in Java?​

1. Reasons for Introducing Interfaces​

  • Standardization: Interfaces provide method declarations only, ensuring that implementing classes follow a standard structure. Interfaces specify what to do, not how to do it.
  • Multiple Inheritance: Java does not support multiple inheritance with classes, but multiple inheritance can be achieved using interfaces.
  • No Default Parent: Unlike classes, interfaces do not extend any parent class by default, making them more flexible.
  • Object Instantiation Restriction: Objects of an interface cannot be created, but interface references can be used to hold objects of implementing classes.

2. Example of Interface Usage in Java​

// Define an interface with method declarations
interface Calculator {
void add(int a, int b);
void subtract(int a, int b);
}

// Implementing the interface in a class
class MyCalculator implements Calculator {
@Override
public void add(int a, int b) {
System.out.println("Sum: " + (a + b));
}

@Override
public void subtract(int a, int b) {
System.out.println("Difference: " + (a - b));
}
}

// Main class
public class CalculatorApp {
public static void main(String[] args) {
// Interface reference holding an object of implementing class
Calculator calc = new MyCalculator();

// Calling methods using interface reference
calc.add(10, 5);
calc.subtract(10, 5);

// Trying to create an object of interface (Not Allowed)
// Calculator obj = new Calculator(); // Error: Cannot instantiate an interface
}
}

3. Explanation of the Code​

  • Interface Calculator:
    • Declares add() and subtract() methods.
    • Methods are implicitly public and abstract.
  • Class MyCalculator:
    • Implements Calculator interface.
    • Provides concrete implementations for add() and subtract().
  • Main Class CalculatorApp:
    • Creates an interface reference (Calculator calc).
    • Assigns an object of MyCalculator to the interface reference.
    • Calls methods using the interface reference.
    • Demonstrates that an object of an interface cannot be created.

4. Key Takeaways​

  • Interfaces ensure standardization by defining method declarations.
  • Multiple inheritance is possible because a class can implement multiple interfaces.
  • An interface cannot have an object, but an interface reference can hold an object of an implementing class.
  • All methods in an interface are public and abstract by default.

Interface Methods and Variables in Java​

1. Key Properties of Interfaces​

  • All methods in an interface are implicitly public and abstract.
  • All variables in an interface are implicitly public, static, and final.
  • Since interface variables are final, they must be initialized at the time of declaration.

2. Example Program: Implementing an Interface​

// Define an interface with method declarations
interface Calculator {
int A = 10; // Implicitly public, static, and final
public static final int B = 20; // Explicitly declared public, static, final
// int C; // Error: Variable 'C' must be initialized

void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
}

// Implementing the interface in a class
class MyCalculator implements Calculator {
@Override
public void add(int a, int b) {
System.out.println("Sum: " + (a + b));
}

@Override
public void subtract(int a, int b) {
System.out.println("Difference: " + (a - b));
}

@Override
public void multiply(int a, int b) {
System.out.println("Product: " + (a * b));
}

// Attempting to modify a final variable (Will Cause an Error)
// { A = 99; } // Error: final variable cannot be reassigned
}

// Main class
public class CalculatorApp {
public static void main(String[] args) {
// Accessing interface variables
System.out.println("Value of A: " + Calculator.A);
System.out.println("Value of B: " + Calculator.B);

// Creating an object of MyCalculator
Calculator calc = new MyCalculator();

// Calling methods using interface reference
calc.add(10, 5);
calc.subtract(10, 5);
calc.multiply(10, 5);
}
}

3. Explanation of the Code​

  • Interface Calculator:
    • Declares three methods: add(), subtract(), and multiply().
    • Defines variables A and B as public static final.
    • C is commented out because it would cause an error since interface variables must be initialized.
  • Class MyCalculator:
    • Implements the Calculator interface.
    • Provides concrete implementations for all three methods.
    • Attempts to modify A, which causes an error because interface variables are final.
  • Main Class CalculatorApp:
    • Accesses interface variables directly using Calculator.A and Calculator.B.
    • Creates an object of MyCalculator using an interface reference.
    • Calls add(), subtract(), and multiply() methods.

4. Key Takeaways​

  • Interface methods are always public and abstract (even if not explicitly mentioned).
  • Interface variables are always public, static, and final.
  • Final variables must be initialized at the time of declaration.
  • Final variables cannot be reassigned once initialized.
  • Interfaces provide a contract that implementing classes must follow.

Partial Implementation of an Interface in Java​

1. Key Concept​

  • When a class implements an interface, it must provide implementations for all abstract methods of the interface.
  • If a class does not provide implementations for all abstract methods, it must be declared as an abstract class.
  • Abstract classes cannot be instantiated directly.

2. Example Program: Partial Implementation of an Interface​

// Define an interface with method declarations
interface Calculator {
void add(int a, int b);
void subtract(int a, int b);
}

// Partial implementation of the interface
abstract class PartialCalculator implements Calculator {
@Override
public void add(int a, int b) {
System.out.println("Sum: " + (a + b));
}

// No implementation for subtract(), so the class remains abstract
}

// Full implementation of the interface
class MyCalculator extends PartialCalculator {
@Override
public void subtract(int a, int b) {
System.out.println("Difference: " + (a - b));
}
}

// Main class
public class CalculatorApp {
public static void main(String[] args) {
// PartialCalculator calc = new PartialCalculator(); // Error: Cannot instantiate an abstract class
Calculator calc = new MyCalculator(); // Using full implementation
calc.add(10, 5);
calc.subtract(10, 5);
}
}

3. Explanation of the Code​

  • Interface Calculator:
    • Declares add() and subtract() methods.
  • Class PartialCalculator:
    • Implements add() method.
    • Does not provide an implementation for subtract(), so it is declared as abstract.
    • Cannot be instantiated directly.
  • Class MyCalculator:
    • Extends PartialCalculator.
    • Provides implementation for subtract().
  • Main Class CalculatorApp:
    • Cannot create an object of PartialCalculator (since it is abstract).
    • Uses MyCalculator to access both methods.

4. Key Takeaways​

  • A class must implement all abstract methods from an interface.
  • If a class provides only a partial implementation, it must be declared abstract.
  • Abstract classes cannot be instantiated.
  • A subclass must implement any remaining abstract methods to become fully concrete.

Achieving Multiple Inheritance Using Interfaces in Java​

1. Key Concept​

  • Java does not support multiple inheritance with classes, but it can be achieved using interfaces.
  • A class can implement multiple interfaces and provide implementations for their methods.
  • This approach allows Java to achieve multiple inheritance without ambiguity.

2. Example Program: Achieving Multiple Inheritance with Interfaces​

// Define first interface
interface Calculator1 {
void add(int a, int b);
}

// Define second interface
interface Calculator2 {
void subtract(int a, int b);
}

// Define third interface
interface Calculator3 {
void multiply(int a, int b);
}

// Implementing multiple interfaces in a single class
class MyCalculator implements Calculator1, Calculator2, Calculator3 {
@Override
public void add(int a, int b) {
System.out.println("Sum: " + (a + b));
}

@Override
public void subtract(int a, int b) {
System.out.println("Difference: " + (a - b));
}

@Override
public void multiply(int a, int b) {
System.out.println("Product: " + (a * b));
}
}

// Main class
public class CalculatorApp {
public static void main(String[] args) {
// Creating an object of MyCalculator
MyCalculator calc = new MyCalculator();

// Calling methods
calc.add(10, 5);
calc.subtract(10, 5);
calc.multiply(10, 5);
}
}

3. Explanation of the Code​

  • Interfaces Calculator1, Calculator2, and Calculator3:
    • Each interface declares one method (add(), subtract(), and multiply(), respectively).
  • Class MyCalculator:
    • Implements all three interfaces.
    • Provides concrete implementations for all methods.
  • Main Class CalculatorApp:
    • Creates an object of MyCalculator.
    • Calls all implemented methods.

4. Key Takeaways​

  • Multiple inheritance is achieved using interfaces.
  • A class can implement multiple interfaces and provide concrete implementations.
  • Each interface defines a specific contract that the implementing class must fulfill.
  • Using interfaces prevents the common issues of multiple inheritance in traditional OOP languages.

Interface Inheritance in Java​

1. Key Concept​

  • An interface cannot implement another interface, but it can extend another interface.
  • When an interface extends another interface, the child interface inherits all methods of the parent interface.
  • If a class implements the child interface, it must provide implementations for all methods of both the child and parent interfaces.

2. Example Program: Interface Extending Another Interface​

// Parent interface
interface Calculator1 {
void add(int a, int b);
}

// Child interface extending the parent interface
interface Calculator2 extends Calculator1 {
void subtract(int a, int b);
}

// Class implementing the child interface
class MyCalculator implements Calculator2 {
@Override
public void add(int a, int b) {
System.out.println("Sum: " + (a + b));
}

@Override
public void subtract(int a, int b) {
System.out.println("Difference: " + (a - b));
}
}

// Main class
public class CalculatorApp {
public static void main(String[] args) {
// Creating an object of MyCalculator
MyCalculator calc = new MyCalculator();

// Calling methods
calc.add(10, 5);
calc.subtract(10, 5);
}
}

3. Explanation of the Code​

  • Interface Calculator1:
    • Declares add() method.
  • Interface Calculator2:
    • Extends Calculator1, inheriting the add() method.
    • Declares an additional subtract() method.
  • Class MyCalculator:
    • Implements Calculator2, which means it must provide implementations for both add() and subtract().
  • Main Class CalculatorApp:
    • Creates an object of MyCalculator and calls both methods.

4. Key Takeaways​

  • An interface can extend another interface to inherit its methods.
  • A class implementing a child interface must implement all methods of both child and parent interfaces.
  • This mechanism supports code reusability and standardization in Java.

Interface Inheritance in Java​

1. Key Concept​

  • An interface cannot implement another interface, but it can extend another interface.
  • When an interface extends another interface, the child interface inherits all methods of the parent interface.
  • If a class implements the child interface, it must provide implementations for all methods of both the child and parent interfaces.
  • A class can extend another class and implement an interface at the same time.

2. Example Program: Class Extending Another Class and Implementing an Interface​

// Parent class
class BasicCalculator {
void multiply(int a, int b) {
System.out.println("Product: " + (a * b));
}
}

// Interface
interface Calculator {
void add(int a, int b);
}

// Class extending another class and implementing an interface
class AdvancedCalculator extends BasicCalculator implements Calculator {
public void add(int a, int b) {
System.out.println("Sum: " + (a + b));
}
}

// Main class
public class CalculatorApp {
public static void main(String[] args) {
AdvancedCalculator calc = new AdvancedCalculator();
calc.add(10, 5);
calc.multiply(10, 5);
}
}

3. Key Takeaways​

  • A class can extend another class while also implementing an interface.
  • This allows the class to inherit concrete methods from the parent class and provide implementations for abstract methods from the interface.
  • This mechanism supports both inheritance and interface-based abstraction, making the code more flexible and reusable.

Marker (Tag) Interface in Java​

1. Key Concept​

  • A marker interface (also called a tag interface) is an empty interface that does not contain any method declarations.
  • Marker interfaces are used to signal metadata or specific behavior to the JVM or other classes.
  • The Serializable interface is a built-in marker interface in Java.

2. Example of a Marker Interface​

// Marker Interface
interface Marker {}

// Class implementing Marker Interface
class SachinTendulkar implements Marker {
void showPlayer() {
System.out.println("Sachin Tendulkar is a legendary cricketer.");
}
}

// Main class
public class MarkerExample {
public static void main(String[] args) {
SachinTendulkar player = new SachinTendulkar();
if (player instanceof Marker) {
System.out.println("This class is marked with Marker interface.");
}
player.showPlayer();
}
}

3. Key Takeaways​

  • Marker interfaces do not contain any methods.
  • They are used to indicate that a class belongs to a specific category.
  • The instanceof operator is commonly used to check if an object belongs to a marker interface.
  • Examples of built-in marker interfaces in Java: Serializable, Cloneable, Remote.

2. Example Program: Interface with Various Methods​

interface ExampleInterface {
// Abstract method (public & abstract by default)
void displayOne();

// Default concrete method
default void displayTwo() {
System.out.println("Inside default method displayTwo()");
}

// Static concrete method
static void displayThree() {
System.out.println("Inside static method displayThree()");
}

// Private concrete method (can only be used inside the interface)
private void displayFour() {
System.out.println("Inside private method displayFour()");
}
}

// Implementing the interface
class MyClass implements ExampleInterface {
public void displayOne() {
System.out.println("Implementing abstract method displayOne()");
}
}

// Main class
public class InterfaceExample {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.displayOne(); // Calling abstract method
obj.displayTwo(); // Calling default method
ExampleInterface.displayThree(); // Calling static method
}
}

3. Key Takeaways​

  • Abstract methods must be implemented by the implementing class.
  • Default methods provide a concrete implementation within the interface.
  • Static methods can be called using the interface name.
  • Private methods are only for internal use within the interface.