Ads

Saturday, January 2, 2021

Create an abstract class "Animals" with two abstract methods "cats" and "dogs". Now create a class "Cats" with a method "cats" which prints "Cats meow" and a class "Dogs" with a method "dogs" which prints "Dogs bark", both inheriting the class "Animals". Now create an object for each of the subclasses and call their respective methods.



Source Code..


abstract class Animals {

    public abstract void cats();

    public abstract void dogs();

}

 

class Cats extends Animals {

    

    @override

    public void cats() {

        System.out.println("Cats meow");

    }

        @override

    public void dogs() {

        // Not implemented as Cats do not bark

    }

}

 

class Dogs extends Animals {

    public void cats() {

        // Not implemented as Dogs do not meow

    }

    public void dogs() {

        System.out.println("Dogs bark");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Cats catObj = new Cats();

        Dogs dogObj = new Dogs();

        catObj.cats();

        dogObj.dogs();

    }

}


In this code, we have defined an abstract class Animals with two abstract methods cats and dogs. We have also defined two concrete classes Cats and Dogs that extend the Animals class and provide the implementation details for the abstract methods.

The Cats class overrides the cats method and prints "Cats meow". Since cats do not bark, the dogs method in the Cats class is not implemented.

The Dogs class overrides the dogs method and prints "Dogs bark". Since dogs do not meow, the cats method in the Dogs class is not implemented.

Finally, the main program creates objects of the Cats and Dogs classes and calls their respective methods to print "Cats meow" and "Dogs bark" to the console using the System.out.println() statement


here are some additional points to keep in mind about this code:

In Java, when a class extends an abstract class that has abstract methods, it must either provide the implementation for those abstract methods or be declared abstract itself.

In the Cats class, we have overridden the cats method inherited from the Animals class, but we have not implemented the dogs method because cats do not bark. Similarly, in the Dogs class, we have overridden the dogs method but have not implemented the cats method because dogs do not meow.

When we create objects of the Cats and Dogs classes, we are only able to call the methods defined in the Animals class (i.e., cats and dogs). This is because the methods defined in the concrete subclasses (Cats and Dogs) are specific to those subclasses and not visible to the outside world.

We have used the System.out.println() method to print the output to the console. This method takes a string as an argument and prints it to the console followed by a newline character.







3 comments:

  1. in java this type of inheritance was not supported directly but using the abstraction and interface we are use it...but in this code you will write the no args method...why ?? what was use of this two lines and it necessary to write ?

    ReplyDelete
  2. These websites are really needed, you can learn a lot. Munchkin Kitten For Sale

    ReplyDelete