Page 369 - Computer_Science_F5
P. 369
handle multiple inheritance,
Computer Science 3. How do interfaces provide Abstraction in Java
and why is it achieved
differently than in other
Abstract classes
programming languages?
Abstract classes are classes that cannot be
instantiated and may contain abstract methods.
FOR ONLINE READING ONLY
a way to achieve multiple
inheritance in Java? Explain They serve as blueprints for other classes and
can define common behavior for subclasses.
the role of interfaces in In other words, abstract class is a restricted
defining contracts for classes class that cannot be instantiated directly, but
and how classes implement can only be accessed by inheriting it from
interfaces to fulfill those another class.
contracts.
4. Discuss the diamond problem Abstract method
in the context of multiple An abstract method is a method defined in
inheritance and explain how an abstract class. It serves as a placeholder
Java’s interface approach for methods that must be implemented by
avoids this issue. Provide subclasses. It can only be used in an abstract
an example to illustrate how class, and it does not have a body. The body is
interfaces help resolve the provided by the subclass. Abstract classes are
diamond problem. declared using the “abstract” keyword. They
5. Describe the concept of can have both abstract and concrete methods.
abstract classes and how Abstract classes cannot be instantiated but can
they relate to inheritance and be subclassed. Subclasses of abstract classes
interfaces in Java. Explain must implement all abstract methods.
when it’s appropriate to
use abstract classes versus Program Example 5.26:
interfaces in your Java
programs. Java program to show abstract class
6. Discuss the importance of // Abstract class
polymorphism in Object- abstract class Animal {
Oriented programming // Abstract method (does not have a body)
and how inheritance and public abstract void animalSound();
interfaces contribute to // Regular method
achieving polymorphic public void sleep() {
behavior in Java. Provide System.out.println(“Zzz”);
examples to demonstrate }
}
polymorphism in action // Subclass (inherit from Animal)
using inheritance and class Pig extends Animal {
interfaces. public void animalSound() {
360
for Advanced Secondary Schools
Computer Science Form 5.indd 360 23/07/2024 12:34

