Q1: Write a Java program to demonstrate Single Inheritance
class Animal {
void eat() {
[Link]("Animals eat food");
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
public class SingleInheritance {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // from parent
[Link](); // from child
Q2: Write a Java program to demonstrate Multilevel Inheritance
class Animal {
void eat() {
[Link]("Animals eat");
}
class Mammal extends Animal {
void walk() {
[Link]("Mammals walk");
class Dog extends Mammal {
void bark() {
[Link]("Dog barks");
public class MultilevelInheritance {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
[Link]();
Q3: Explain with examples how Constructor Overloading differs from Method Overloading
• Constructor Overloading: Multiple constructors in the same class with different parameter lists.
• Method Overloading: Multiple methods in the same class with the same name but different
parameters.
class Example {
int a, b;
// Constructor Overloading
Example() {
a = 0; b = 0;
Example(int x, int y) {
a = x; b = y;
// Method Overloading
void show() {
[Link]("a=" + a + ", b=" + b);
void show(int x) {
[Link]("Value: " + x);
public class OverloadingDemo {
public static void main(String[] args) {
Example e1 = new Example();
Example e2 = new Example(5, 10);
[Link]();
[Link]();
[Link](20); // method overloading
Q4: Create a Rectangle class with Constructor Overloading
class Rectangle {
int length, breadth;
// Default constructor
Rectangle() {
length = 5;
breadth = 3;
// Parameterized constructor
Rectangle(int l, int b) {
length = l;
breadth = b;
int area() {
return length * breadth;
public class RectangleDemo {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(); // default
Rectangle r2 = new Rectangle(10, 4); // parameterized
[Link]("Area (default): " + [Link]());
[Link]("Area (parameterized): " + [Link]());