RAWAL INSTITUTE OF MANAGEMENT
Subject: Java Programming
Assignment No. 3
1) Explain the Uses and Benefits of Inheritance in OOPS.
Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows
one class (child or subclass) to acquire properties and behaviors (methods and variables)
from another class (parent or superclass). This helps promote reusability and organization
of code. Uses of Inheritance:
1. Code Reusability: Once a class has been written, it can be reused in other classes
through inheritance.
2. Method Overriding: Child classes can redefine parent class methods for specific
behavior.
3. Extensibility: You can extend existing classes without modifying them.
4. Maintainability: Centralized updates in a base class automatically apply to derived
classes.
5. Logical Hierarchy: Helps organize related classes in a tree-like structure.
Benefits:
- Reduces code redundancy
- Promotes better organization
- Simplifies debugging and testing
- Encourages modular and flexible software design
2) Explain Types and Methods of Inheritance in Java.
In Java, inheritance allows a new class to inherit members from an existing class using the
extends keyword. Types of Inheritance:
1. Single Inheritance: A subclass inherits from one superclass.
Example: class B extends A.
2. Multilevel Inheritance: A class inherits from another derived class.
Example: class C extends B extends A.
3. Hierarchical Inheritance: Multiple classes inherit from one parent class.
Example: class B extends A, class C extends A.
4. Multiple Inheritance (via Interfaces): Java doesn’t support multiple inheritance with
classes to avoid ambiguity, but allows it with interfaces.
Example: class D implements Interface1, Interface2.
Methods of Inheritance:
- Using extends keyword for class inheritance.
- Using implements keyword for interface inheritance.
- Using super() to access parent class constructors and methods.
3) Explain Role of Constructors in Inheritance.
Constructors are special methods used to initialize objects. In inheritance, constructors
ensure that both parent and child objects are properly initialized. Key Points:
1. Constructors are not inherited by subclasses.
2. The constructor of the superclass is called before the subclass constructor.
3. The super() keyword is used to explicitly call the parent class constructor.
4. If super() is not written, Java automatically calls the default constructor of the parent
class.
5. This ensures that all parent attributes are initialized before child attributes.
Example:
class Parent {
Parent() { System.out.println("Parent constructor"); }
}
class Child extends Parent {
Child() { System.out.println("Child constructor"); }
}
Output:
Parent constructor
Child constructor
4) Explain Purpose, Definition, Implementation, Reference Variables of an
Interface.
Purpose of an Interface:
An interface in Java provides a way to achieve abstraction and multiple inheritance. It
defines a contract that classes must follow. It is mainly used to define a set of
unimplemented methods that subclasses must define.
Definition:
An interface is defined using the interface keyword. It can contain constants, abstract
methods, default methods, and static methods.
Example:
interface Animal {
void eat();
void sleep();
}
Implementation:
A class uses the implements keyword to provide functionality for all abstract methods
defined in an interface.
Example:
class Dog implements Animal {
public void eat() { System.out.println("Dog eats"); }
public void sleep() { System.out.println("Dog sleeps"); }
}
Reference Variables of an Interface:
- Interface reference variables can store references to any object that implements the
interface.
- It supports runtime polymorphism.
Example:
Animal a = new Dog();
a.eat();
a.sleep();