Object-Oriented Programming IA - Solved
Here are the answers to the questions, based on VTU resources and including
relevant Java code.
1. What is inheritance? Explain the types of inheritance.
Inheritance is a fundamental concept in Object-Oriented Programming (OOP)
that allows one class (the subclass or child class) to acquire the properties (fields)
and behaviors (methods) of another class (the superclass or parent class). The
primary purpose of inheritance is to enable code reusability and to establish a
clear “is-a” relationship between classes. For example, a Dog is an Animal.
In Java, inheritance is implemented using the extends keyword.
Types of Inheritance in Java Java supports the following types of inheri-
tance through classes:
a) Single Inheritance In single inheritance, a subclass inherits from only one
superclass. This is the simplest form of inheritance.
Java Code Example:
// Superclass
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
// Subclass
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
[Link](); // Inherited from Animal
[Link](); // Method from Dog class
}
}
b) Multilevel Inheritance In multilevel inheritance, a class inherits from a
superclass, which in turn inherits from another superclass, forming a chain of
1
inheritance.
Java Code Example:
// Superclass
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
// Subclass inheriting from Animal
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
// Subclass inheriting from Dog
class Puppy extends Dog {
void weep() {
[Link]("The puppy weeps.");
}
}
public class Main {
public static void main(String[] args) {
Puppy myPuppy = new Puppy();
[Link](); // Inherited from Animal
[Link](); // Inherited from Dog
[Link](); // Method from Puppy class
}
}
c) Hierarchical Inheritance In hierarchical inheritance, multiple subclasses
inherit from a single superclass.
Java Code Example:
// Superclass
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
// Subclass 1
class Dog extends Animal {
2
void bark() {
[Link]("The dog barks.");
}
}
// Subclass 2
class Cat extends Animal {
void meow() {
[Link]("The cat meows.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
[Link]();
[Link]();
Cat myCat = new Cat();
[Link]();
[Link]();
}
}
Note on Other Inheritance Types: Java does not support Multiple Inheri-
tance (a class inheriting from more than one superclass) directly through classes
to avoid the “Diamond Problem.” This is achieved using interfaces instead.
2. Achieve Polymorphism using method overriding.
Polymorphism means “many forms,” and in OOP, it is the ability of an object
to take on many forms. Runtime Polymorphism is a process in which a call
to an overridden method is resolved at runtime rather than compile-time. This
is achieved through Method Overriding.
Method Overriding occurs when a subclass provides a specific implementa-
tion for a method that is already defined in its superclass. The method in the
subclass must have the same name, same parameters, and same return type (or
a subtype) as the method in the superclass.
When an overridden method is called through a superclass reference, Java de-
termines which version of the method to execute based on the type of the object
being referred to at runtime.
Java Code Example:
// Superclass
3
class Shape {
// Method to be overridden
void draw() {
[Link]("Drawing a generic shape.");
}
}
// Subclass 1
class Circle extends Shape {
// Overriding the draw method for Circle
@Override
void draw() {
[Link]("Drawing a circle.");
}
}
// Subclass 2
class Square extends Shape {
// Overriding the draw method for Square
@Override
void draw() {
[Link]("Drawing a square.");
}
}
public class Main {
public static void main(String[] args) {
// A Shape reference can refer to a Shape, Circle, or Square object.
Shape myShape;
// Assign a Circle object to the Shape reference
myShape = new Circle();
// At runtime, the JVM calls the draw() method of the Circle class.
[Link](); // Output: Drawing a circle.
// Assign a Square object to the Shape reference
myShape = new Square();
// At runtime, the JVM calls the draw() method of the Square class.
[Link](); // Output: Drawing a square.
}
}
In this example, the [Link]() method call is polymorphic. The exact
method that gets called is determined at runtime based on the object that
myShape is currently referencing. This is the essence of achieving runtime poly-
morphism through method overriding.