Table of Contents

Lesson 5

Abstract classes, interfaces and polimorfism.

Objectives

Documentation

Polimorfismul
Clase abstracte si interfete
Thinking in Java : Chapter 7 - Polimorphism
Diagrama de clase
Implementarea diagramelor UML in Java

Exercises

Exercise 1

Follow the instructions and implement the program according to specification . Create an appropriate test class for testing the program. For testing purpose you will create an array of shapes and then call and display getArea() and getPermiter() for each object in the array.

In this exercise, Shape shall be defined as an abstract class, which contains:

Exercise 2

Follow the instructions and implement the program according to specification . Create an appropriate test class for testing the program.

public interface Image {
   void display();
}
 
public class RealImage implements Image {
 
   private String fileName;
 
   public RealImage(String fileName){
      this.fileName = fileName;
      loadFromDisk(fileName);
   }
 
   @Override
   public void display() {
      System.out.println("Displaying " + fileName);
   }
 
   private void loadFromDisk(String fileName){
      System.out.println("Loading " + fileName);
   }
}
 
public class ProxyImage implements Image{
 
   private RealImage realImage;
   private String fileName;
 
   public ProxyImage(String fileName){
      this.fileName = fileName;
   }
 
   @Override
   public void display() {
      if(realImage == null){
         realImage = new RealImage(fileName);
      }
      realImage.display();
   }
}

For the image application in the code above:

Exercise 3

Implement application based on the UML class diagram bellow. Create a test class for testing the program.

Follow the instructions and implement the program according to specification .

Exercise 4

Update the Controller class from exercise 3 to be implemented as a Singleton.