Join Regular Classroom : Visit ClassroomTech

Programming in JAVA – codewindow.in

Related Topics

JAVA Programming

Example of using inheritance in Java?

Yes, I can provide an example of using inheritance in Java.

Inheritance is a mechanism that allows a new class to be based on an existing class, inheriting all of its attributes and methods. The new class is called the subclass or derived class, while the existing class is called the superclass or base class.

Here is an example of using inheritance in Java:

public class Animal {
    private String name;
    private int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println("The animal is eating.");
    }

    public void sleep() {
        System.out.println("The animal is sleeping.");
    }
}

public class Dog extends Animal {
    public Dog(String name, int age) {
        super(name, age);
    }

    public void bark() {
        System.out.println("The dog is barking.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Buddy", 3);
        myDog.eat(); // Output: The animal is eating.
        myDog.sleep(); // Output: The animal is sleeping.
        myDog.bark(); // Output: The dog is barking.
    }
}

In this example, we have a superclass Animal with two instance variables name and age and two methods eat() and sleep(). We also have a subclass Dog that extends the Animal class and adds a new method bark().

The Dog class inherits the name, age, eat() and sleep() methods from the Animal class. The Dog class also adds its own method bark().

In the Main class, we create a new object of the Dog class called myDog. We can then call the eat(), sleep(), and bark() methods on the myDog object, which will execute the corresponding methods in the Animal and Dog classes.

This example demonstrates how inheritance allows us to reuse code from an existing class and add new functionality to it by defining a new subclass.

What is polymorphism in Java and how is it achieved?

Polymorphism is a concept in Java that allows objects of different classes to be treated as if they are of the same type. It means that a single variable or reference can hold different types of objects at different times.

Polymorphism in Java is achieved through two mechanisms: method overriding and method overloading.

  1. Method Overriding: Method overriding allows a subclass to provide its own implementation of a method that is already defined in its superclass. The method in the subclass has the same name, return type, and parameters as the method in the superclass.

Here is an example of method overriding:

public class Animal {
    public void makeSound() {
        System.out.println("The animal makes a sound.");
    }
}

public class Dog extends Animal {
    public void makeSound() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        Animal myDog = new Dog();
        
        myAnimal.makeSound(); // Output: The animal makes a sound.
        myDog.makeSound(); // Output: The dog barks.
    }
}

In this example, we have a superclass Animal with a method makeSound(). We also have a subclass Dog that overrides the makeSound() method and provides its own implementation.

In the Main class, we create an object of the Animal class called myAnimal and an object of the Dog class called myDog. We can then call the makeSound() method on both objects. When we call the method on the myAnimal object, the makeSound() method defined in the Animal class is executed. When we call the method on the myDog object, the makeSound() method defined in the Dog class is executed.

  1. Method Overloading: Method overloading allows a class to have multiple methods with the same name but different parameters. The methods can have different parameter types or different numbers of parameters.

Here is an example of method overloading:

public class Calculator {
    public int add(int x, int y) {
        return x + y;
    }

    public double add(double x, double y) {
        return x + y;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator myCalculator = new Calculator();
        
        int result1 = myCalculator.add(3, 4);
        double result2 = myCalculator.add(2.5, 4.2);
        
        System.out.println(result1); // Output: 7
        System.out.println(result2); // Output: 6.7
    }
}

In this example, we have a Calculator class with two add() methods that have the same name but different parameter types (int and double).

In the Main class, we create an object of the Calculator class called myCalculator. We can then call the add() method with different parameter types and get different results.

Questions on Chapter 7

Questions on Chapter 7

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories