Answer Key OOPS
Answer Key OOPS
Part-A
A class is a blueprint or template for creating objects. It defines the attributes (also known as
fields or properties) and behaviors (methods) that the objects created from it will have.
Simple
Object-Oriented
Platform-Independent
Secure
Abstraction is the concept of hiding complex implementation details and showing only the
essential features of an object to the outside world
Encapsulation is the concept of bundling the data (variables) and methods (functions) that
operate on the data into a single unit or class, while restricting access to some of the object's
components
4. Define constructor
Javadoc is a documentation tool in Java that generates HTML documentation for your code
by parsing special comments in your source files
Public
Private
Protected
9. Explain the use of extend keyword with suitable example?
The extends keyword in Java is used to create a subclass by inheriting properties and
behaviors from an existing superclass.
Inheritance is a mechanism in OOP where a new class (called the subclass or derived class)
is based on an existing class (called the superclass or base class).
Polymorphism means "many forms" and allows objects of different classes to be treated as
objects of a common superclass.
Part-B
12(a) Express a Java Program to create class named Shape that contains two integers and an empty
method named print Area (). Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape. Each one of the classes contains only the method
print Area () that prints the area of the given shape.
class Shape
{
// Two integers to store dimensions
int dimension1;
int dimension2;
// Rectangle class
class Rectangle extends Shape
{
// Constructor to initialize dimensions
Rectangle(int length, int width)
{
this.dimension1 = length;
this.dimension2 = width;
}
// Overriding printArea() method
@Override
void printArea() {
int area = dimension1 * dimension2; // Area = length * width
System.out.println("Area of Rectangle: " + area);
}
}
// Triangle class
class Triangle extends Shape
{
// Constructor to initialize dimensions
Triangle(int base, int height)
{
this.dimension1 = base;
this.dimension2 = height;
}
// Overriding printArea() method
@Override
void printArea()
{
double area = 0.5 * dimension1 * dimension2; // Area = 0.5 * base * height
System.out.println("Area of Triangle: " + area);
}
}
// Circle class
class Circle extends Shape
{
// Constructor to initialize radius
Circle(int radius)
{
this.dimension1 = radius; // Use dimension1 for radius
}
// Overriding printArea() method
@Override
void printArea()
{
double area = Math.PI * dimension1 * dimension1; // Area = π * radius^2
System.out.println("Area of Circle: " + area);
}
}
// Main class to demonstrate the functionality
public class Main
{
public static void main(String[] args)
{
// Creating objects of each shape
Rectangle rectangle = new Rectangle(5, 10);
Triangle triangle = new Triangle(4, 6);
Circle circle = new Circle(3);
// Calling printArea() for each shape
rectangle.printArea(); // Output: Area of Rectangle: 50
triangle.printArea(); // Output: Area of Triangle: 12.0
circle.printArea(); // Output: Area of Circle: 28.274333882308138
}}
(OR)
12(b) Explain Inheritance and its types with example?
Inheritance is a fundamental concept in object-oriented programming (OOP) that
allows one class (the subclass or derived class) to inherit the properties and behaviors
(methods) of another class (the superclass or base class).
Types of Inheritance
There are several types of inheritance in Java:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritances (through interfaces)
Hybrid Inheritance (through interfaces)
1. Single Inheritance
In single inheritance, a class inherits from one superclass only.
2. Multilevel Inheritance
In multilevel inheritance, a class inherits from a superclass, and another class inherits from
that subclass
3. Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from the same superclass
4. Multiple Inheritances (through Interfaces)
Java does not support multiple inheritances with classes to avoid ambiguity, but it allows
multiple inheritances through interfaces.
5. Hybrid Inheritance (through Interfaces)
Hybrid inheritance is a combination of two or more types of inheritance. In Java, it is
typically implemented using interfaces