You are on page 1of 5

SE322: Software Design and Architecture

Tutorial 8: Detailed Design I

Objective: Reviewing essential principles of object-oriented programming (OOP) that used in


detailed design such as:

 Abstract Classes
 Interfaces
 Polymorphism
 Dynamic Binding
 Concrete classes and methods

Course Learning Outcome:


1. CLO 1: Recognize the principles, strategies, and process of software design and
architecture.
2. CLO 2: Analyze an existing software architecture or design.
3. CLO 3: Differentiate how various architectural styles and design patterns enhance and
degrade a system’s functional-and non-functional properties.
4. CLO 4: Justify architecture and/or design given a collection of requirements.

In this tutorial, you will review how:

 Abstract classes provide a blueprint for other classes.


 Interfaces ensure that certain methods are implemented.
 Polymorphism allows objects of different classes to be treated as objects of a common
superclass.
 Dynamic Binding allows the JVM to decide at runtime which method to invoke.

Follow the steps below to complete the tutorial:

1. Read and understand the following definitions and examples


Abstract Classes

 Definition: Classes that cannot be instantiated and are meant to be subclassed.

abstract class Animal {


abstract void sound();
}

Usage: Acts as a base for other classes to derive from.


Interfaces

 Definition: A contract that guarantees certain methods are implemented.

interface Walkable {

void walk();

Usage: Can be used by any class to guarantee certain behaviors.

Polymorphism

 Definition: The ability for different classes to be treated as instances of the same class
through inheritance.

Animal myHorse = new Horse();

Animal myCat = new Cat();

Dynamic Binding

 Definition: The ability of the program to resolve which method to invoke at runtime
rather than compile-time.

Animal myPet = new Cat();

myPet.sound(); // The actual method called is determined at runtime.

Practical Examples: Animal, Horse, and Cat

Animal (Abstract class)

abstract class Animal {


abstract void sound();
void breathe() {
System.out.println("Breathing...");
}
}

Horse (Subclass of Animal and implements Walkable)

class Horse extends Animal implements Walkable {


@Override
void sound() {
System.out.println("Neigh!");
}
@Override
public void walk() {
System.out.println("Horse is walking...");
}
}

Cat (Subclass of Animal and implements Walkable)

class Cat extends Animal implements Walkable {


@Override
void sound() {
System.out.println("Meow!");
}

@Override
public void walk() {
System.out.println("Cat is walking...");
}
}

Using Polymorphism and Dynamic Binding

public class Test {


public static void main(String[] args) {
Animal a1 = new Horse();
Animal a2 = new Cat();

a1.sound(); // Outputs: Neigh!


a2.sound(); // Outputs: Meow!

// Using the interface


Walkable w1 = new Horse();
w1.walk(); // Outputs: Horse is walking...
}
}
Concrete Class:
 A concrete class is a standard class in object-oriented programming that can be
instantiated, meaning you can create objects of this class type.
 Unlike abstract classes, concrete classes don't have any abstract methods that are left
unimplemented. They provide implementations for all of their methods.
 You can create instances (objects) directly from a concrete class.

Concrete Method:
 A concrete method is a method that has a complete implementation. In contrast to
abstract methods (which only have a method signature without a body), concrete methods
provide a specific set of instructions that are executed when the method is called.
 Concrete methods can exist in both abstract classes and concrete classes.
 All methods in an interface are abstract (non-concrete, have no implementation)

Polymorphism and dynamic binding

Polymorphism is the broader concept that allows objects of different types to be treated as if they
are objects of the same type. Dynamic binding (for runtime polymorphism) ensures that the
correct method associated with the actual object type is called when there are overridden
methods.

2. Answer the following questions:

How to enforce implementing a method in all subclasses?

How are interfaces different from abstract classes?


When should you use abstract classes instead of concrete classes?

3. Exercises
1. Implementing New Animals:
 Create a new class Dog that extends Animal and implements Walkable.
 The sound method should output "Bark!".
 The walk method should output "Dog is walking...".

2. Bird Interface:
 Design an interface named Flyable with a method named fly.
 Implement this interface in a new class called Bird. The fly method should print
"Bird is flying...".
 Make sure the Bird class also extends the Animal class and implements its
methods.

3. Animal Array:
 Create a class TestAnimals that will have a main method to test what you have
done so far.
 Create an array of Animal and populate it with instances of Horse, Cat, and Dog.
 Loop through the array and call the sound method on each animal.
 Extend the loop to also call the walk method if the animal is an instance of
Walkable.

4. Dynamic Binding:
 Create a method that takes an Animal reference as its parameter.
 Inside this method, use dynamic binding to check if the animal can walk (i.e., if
it's an instance of Walkable) and, if so, call its walk method.
 Test this method with different animal objects.

5. Extending Interfaces:
 Create a new interface Runnable with a method run.
 Modify the Dog class to implement both Walkable and Runnable.
 Implement the run method to print "Dog is running...".
 Test the Dog class by creating an instance and calling all its methods.

You might also like