Posted in

INHERITANCE CODING QUESTIONS IN JAVA

1. Write a simple Java program to demonstrate inheritance create a Vehicle class with basic properties like speed and color and create a Car class that inherits from Vehicle and adds a new property, like model.Show how inheritance allows the Car class to reuse properties and methods of the Vehicle class?

    class Vehicle {
    String color;
    int speed;
    Vehicle(String color, int speed) {
        this.color = color;
        this.speed = speed;
    }
    void displayInfo() {
        System.out.println(“Color: ” + color + “, Speed: ” + speed);
    }
}
class Car extends Vehicle {
    String model;
    Car(String color, int speed, String model) {
        super(color, speed);  // Calling the superclass constructor
        this.model = model;
    }
    void displayInfo() {
        super.displayInfo();
        System.out.println(“Model: ” + model);
    }
    public static void main(String[] args) {
        Car car = new Car(“Red”, 120, “Tesla Model S”);
        car.displayInfo();  // Output all properties of the car
    }
}

Output :

Color: Red, Speed: 120
Model: Tesla Model S

2. Write a Java program to demonstrate method overriding create a Shape class with a method area() and create two subclasses: Circle and Rectangle, each overriding the area() method to calculate and return the area specific to the shape?

class Shape {
    void area() {
        System.out.println(“Area of Shape”);
    }
}
class Circle extends Shape {
    void area() {
        System.out.println(“Area of Circle”);
    }
}
class Rectangle extends Shape {
    void area() {
        System.out.println(“Area of Rectangle”);
    }
}
public class Main {
    public static void main(String[] args) {
        Shape s1 = new Circle();
        Shape s2 = new Rectangle();
        s1.area(); 
        s2.area(); 
    }
}

Output :

 Area of Circle
 Area of Rectangle

3. Write a Java program to demonstrate the use of super() keyword create a base class Animal with a constructor that takes the animal’s name and create a derived class Dog that calls the super() method to pass the name to the Animal constructor?

    class Animal {
    String name;
    Animal(String name) {
        this.name = name;
        System.out.println(“Animal constructor: ” + name);
    }
}
class Dog extends Animal {
    Dog(String name) {
        super(name);  // Calling Animal’s constructor
        System.out.println(“Dog constructor: ” + name);
    }
    public static void main(String[] args) {
        Dog dog = new Dog(“Buddy”);
    }
}

Output :

Animal constructor: Buddy
Dog constructor: Buddy

4. Write a Java program where a subclass inherits from a superclass and has its own methods create a class Person with properties like name and age and create a subclass Employee that inherits Person and adds a property salary?

    class Person {
    String name;
    int age;
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    void display() {
        System.out.println(“Name: ” + name + “, Age: ” + age);
    }
}
class Employee extends Person {
    double salary;
    Employee(String name, int age, double salary) {
        super(name, age);  // Calling Person’s constructor
        this.salary = salary;
    }
    void display() {
        super.display();  // Calling Person’s display method
        System.out.println(“Salary: ” + salary);
    }
    public static void main(String[] args) {
        Employee emp = new Employee(“John”, 30, 50000);
        emp.display();
    }
}

Output :

Name: John, Age: 30
Salary: 50000.0

5. Write a program to create a Java program that demonstrates constructor chaining in inheritance and  create a Base class with a constructor and create a Derived class that calls the constructor of the Base class using super()?

    class Parent {
    void greet() {
        System.out.println(“Hello from Parent class”);
    }
}
class Child extends Parent {
    void greet() {
        super.greet();  // Calling Parent’s greet method
        System.out.println(“Hello from Child class”);
    }
    public static void main(String[] args) {
        Child child = new Child();
        child.greet();
    }
}

Output :

Hello from Parent class
Hello from Child class

6. Write a Java program that demonstrates the concept of upcasting and downcasting and create a base class Animal and subclasses Cat and Dog.Demonstrate how upcasting and downcasting work when assigning an object of the derived class to a variable of the base class type?

    class Animal {
    void sound() {
        System.out.println(“Animal makes a sound”);
    }
}
class Dog extends Animal {
    @Override
    void sound() {
        System.out.println(“Dog barks”);
    }
}
class Cat extends Animal {
    @Override
    void sound() {
        System.out.println(“Cat meows”);
    }
}
public class Main {
    public static void main(String[] args) {
        // Upcasting: Dog object assigned to Animal reference
        Animal animal1 = new Dog();
        Animal animal2 = new Cat();
        animal1.sound(); 
        animal2.sound();
        // Downcasting: Animal reference back to Dog
        Dog dog = (Dog) animal1;
        dog.sound(); 
    }
}

Output :

Dog barks
Cat meows
Dog barks

7. Write a Java program that demonstrates the use of abstract classes and interfaces in inheritance and create an abstract class Shape with an abstract method draw().create an interface Colorable with a method color() implement a class Circle that extends Shape and implements Colorable, providing implementations for both draw() and color() methods?

abstract class Shape {
    abstract void draw();
}
interface Colorable {
    void color();
}
class Circle extends Shape implements Colorable {
    @Override
    void draw() {
        System.out.println(“Drawing a Circle”);
    }
    @Override
    public void color() {
        System.out.println(“Coloring the Circle”);
    }
}
public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.draw();  // Calling abstract method
        circle.color(); // Calling interface method
    }
}

Output :

Drawing a Circle
Coloring the Circle 

8. Write a Java program to implement a “has-a” and “is-a” relationship using inheritance and create a class Engine and a class Car that “has a” Engine and create a class Vehicle that “is a” Car (inheritance), and demonstrate both relationships with the appropriate object creation and method calls?

class Engine {
    void start() {
        System.out.println(“Engine started.”);
    }
}
class Car {
    Engine engine;  // “has-a” relationship with Engine
    Car() {
        engine = new Engine();  // Creating an Engine object inside Car
    }
    void drive() {
        System.out.println(“Car is driving.”);
        engine.start();  // Calling Engine’s method
    }
}
class Vehicle extends Car {
    void display() {
        System.out.println(“This is a Vehicle.”);
    }
}
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.drive(); 
        Vehicle myVehicle = new Vehicle();
        myVehicle.drive();  // Vehicle “is a” Car, so it can call Car’s methods
        myVehicle.display(); // Calling Vehicle specific method
    }
}

Output :

    Car is driving.
    Engine started.
    Car is driving.
    Engine started.
    This is a Vehicle.

9. Write a Java program that demonstrates the use of super to call a method from a parent class that is overridden in a subclass and create a base class Animal with a method sound().create a Dog class that overrides sound(). In the Dog class, use super.sound() to call the base class method?

class Animal {
    void sound() {
        System.out.println(“Animal makes a sound.”);
    }
}
class Dog extends Animal {
    @Override
    void sound() {
        System.out.println(“Dog barks.”);
        super.sound(); 
    }
}
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sound(); 
    }
}

Output :

 Dog barks.
 Animal makes a sound.

10. Write a Java program to demonstrate the concept of polymorphism with inheritance and create a class Employee with a method work().create subclasses Manager and Developer, both overriding work() to provide specific implementations.Demonstrate how polymorphism allows you to call the appropriate work() method at runtime?

class Employee {
    void work() {
        System.out.println(“Employee is working.”);
    }
}
class Manager extends Employee {
    @Override
    void work() {
        System.out.println(“Manager is managing the team.”);
    }
}
class Developer extends Employee {
    @Override
    void work() {
        System.out.println(“Developer is writing code.”);
    }
}
public class Main {
    public static void main(String[] args) {
        Employee emp1 = new Manager(); 
        Employee emp2 = new Developer(); 
        emp1.work();
        emp2.work(); 
    }
}

Output :

Manager is managing the team.
Developer is writing code.

Leave a Reply

Your email address will not be published. Required fields are marked *