Page 182 - CTS - CSA TP - Volume 2
P. 182
COMPUTER SOFTWARE APPLICATION - CITS
3. Main Class (AnimalExample):
• Creates instances of concrete classes (Dog and Cat).
• Calls both abstract and concrete methods.
4. Output:
In this example, Animal is an abstract class representing common characteristics of animals. It declares two
abstract methods (makeSound() and eat()) that must be implemented by its concrete subclasses. The sleep()
method is a concrete method with a default implementation.
• Concrete Class Dog:
• Implements the makeSound() and eat() methods specific to a dog.
• Inherits the sleep() method from the Animal class.
• Concrete Class Cat:
• Implements the makeSound() and eat() methods specific to a cat.
• Inherits the sleep() method from the Animal class.
• Main Class (AnimalExample):
• Creates instances of Dog and Cat.
• Demonstrates calling methods: makeSound(), eat(), and sleep().
This example showcases the concept of abstraction where the common behavior is defined in the abstract
class (Animal), and specific details are implemented in the concrete subclasses (Dog and Cat). The main class
demonstrates polymorphism, as instances of different subclasses can be treated as instances of the common
abstract class (Animal).
167
CITS : IT & ITES - Computer Software Application - Exercise 110 CITS : IT & ITES - Computer Software Application - Exercise 110