Java – Abstraction, Interfaces, Abstract Classes
and Thread Creation
Question 1: Analyze and compare the roles of interfaces and
abstract classes in achieving abstraction and runtime
polymorphism in Java.
Introduction
Abstraction is one of the fundamental principles of Object-Oriented Programming in Java. It refers
to hiding implementation details while showing only the essential features of an object. In Java,
abstraction is mainly achieved using abstract classes and interfaces. Both of these mechanisms
help developers design flexible, reusable, and scalable software systems. They also support
runtime polymorphism, where the method that gets executed is determined during program
execution.
Abstract Classes in Java
An abstract class is a class declared with the abstract keyword and cannot be instantiated directly.
It can contain both abstract methods (methods without implementation) and concrete methods
(methods with implementation). Abstract classes are useful when several classes share a common
base structure or functionality. Subclasses extend the abstract class and provide implementation for
its abstract methods.
Interfaces in Java
An interface is a reference type in Java used to achieve full abstraction. It contains method
declarations that must be implemented by classes that implement the interface. Interfaces define a
contract that a class must follow. Modern Java versions also allow default and static methods inside
interfaces.
Role in Achieving Abstraction
Abstract classes provide partial abstraction because they allow both implemented and
unimplemented methods. They are useful when related classes share common properties and
behaviour. Interfaces, on the other hand, provide full abstraction by defining only behaviour without
implementation. They help enforce a standard structure that different classes must follow.
Role in Runtime Polymorphism
Runtime polymorphism in Java is achieved through method overriding. When a superclass
reference refers to a subclass object, the method that gets executed is determined at runtime. Both
abstract classes and interfaces support runtime polymorphism. For example, an Animal reference
can refer to a Dog object and call the sound() method implemented by Dog.
Differences Between Abstract Class and Interface
Abstract classes can contain instance variables, constructors, and both abstract and concrete
methods. In contrast, interfaces mainly contain abstract methods and constant variables. A class
can extend only one abstract class but can implement multiple interfaces, which allows multiple
inheritance in Java.
Conclusion
Both interfaces and abstract classes are essential tools for achieving abstraction and runtime
polymorphism in Java. Abstract classes are suitable when classes share a strong relationship and
common implementation, while interfaces are better when only behaviour specification is required
and multiple inheritance is needed.
Question 2: Analyze the differences between creating a thread by
extending the Thread class and implementing the Runnable
interface. Justify when each approach is preferred.
Introduction
Multithreading is a powerful feature in Java that allows a program to execute multiple tasks
simultaneously. Threads improve performance and responsiveness of applications. In Java,
threads can be created mainly in two ways: by extending the Thread class or by implementing the
Runnable interface.
Creating a Thread by Extending the Thread Class
In this method, a new class extends the Thread class and overrides the run() method. The run()
method contains the code that will be executed by the thread. After creating the object of the class,
the start() method is called to begin thread execution.
Advantages of Extending Thread
This method is simple and easy to understand. It provides direct access to Thread class methods
and is suitable for small programs or demonstrations.
Limitations of Extending Thread
The main limitation is that Java does not support multiple inheritance. If a class extends Thread, it
cannot extend another class. This reduces flexibility and reusability in larger applications.
Creating a Thread by Implementing Runnable Interface
In this approach, a class implements the Runnable interface and defines the run() method. An
object of this class is then passed to a Thread object, and the start() method is called to execute the
thread.
Advantages of Implementing Runnable
This method allows a class to extend another class while still implementing Runnable. It promotes
better object-oriented design because the task and thread execution are separated. It is widely
used in real-world applications and supports better code reuse.
Limitations of Runnable
The approach requires creating an additional Thread object and may appear slightly more complex
for beginners.
Conclusion
Both methods are used to create threads in Java. Extending the Thread class is simpler but less
flexible. Implementing the Runnable interface is more flexible, supports better design principles,
and is generally preferred in modern Java applications.