09 - OOP Recap, Abstract classes, interfaces & polymorphism

Alina

Goals

  • Discuss Preject questions
  • OOP Recap. Remember what we already learnt
  • Understand what is Abstract class in java and why do we need it
  • Implement an Abstract Class
  • Understand what is Interface in java and why do we need it
  • Implement an Interface
  • Understand what is Polymorphism, try to use it

Slides

Abstract class

Abstract Classes are classes in Java, that declare one or more abstract methods.

Consider the following class hierarchy consisting of a Shape class which is inherited by three classes Rectangle, Circle, and Triangle. The Shape class is created to save on common attributes and methods shared by the three classes Rectangle, Circle, and Triangle. calculateArea() is one such method shared by all three child classes and present in Shape class.

Abstract Classes & methods

Create a new abstract class Shape

public abstract class Shape {

    public abstract double calculateArea();
}

Let's create Rectangular class


😎 Toggle Solution
public class Rectangular extends Shape {

    int width;
    int length;
    
    Rectangular(int width, int length) {
        this.width = width;
        this.length = length;
    }
}

As you can see we need to implement method calculateArea() for the Rectangular class


😎 Toggle Solution
public class Rectangular extends Shape {

    double width;
    double length;
    
    Rectangular(double width, double length) {
        this.width = width;
        this.length = length;
    }
    
    public double calculateArea() {
        return width * length;
    }
} 

Let's create Circle class


😎 Toggle Solution
public class Circle extends Shape {

    double radius;

    Circle(double radius) {
        this.radius = radius;
    }
    
    public double calculateArea() {
        return radius * radius * Math.PI;
    }
} 

Interface

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Let's create two interfaces Cleaner & Funny

public interface Cleaner {
    void clean();
}

public interface Funny {
    void makeJoke();
}

Let's make YellowRobot Funny

From the previous lesson: https://github.com/marcelioleal/javailesson8/tree/master/src


😎 Toggle Solution
public class YellowRobot extends Robot implements Funny {
    
    // ...
    
    public void makeJoke() {
        System.out.println("Some joke");
    }
}


😎 Toggle Solution
public class PurpleRobot extends BlueRobot implements Cleaner {
    
    // ...
    
    public void clean() {
        System.out.println("Cleaning....");
    }
}

Polymorphism

It is a OOPs concept where one name can have many forms. For example, you have a smartphone for communication. The communication mode you choose could be anything. It can be a call, a text message, a picture message, mail, etc. So, the goal is common that is communication, but their approach is different. This is called Polymorphism.

Let's create an array of different Robots and let them introduce themselves

public class Main {

    public static vois main() {
        // create many different Robots
        // ...
        
        Robot[] robots = new Robot[]{blueRobot, yellowRobot, purpleRobot, justRobot};
        for (Robot robot: robots) {
            robot.introduce();
        }
        
        Funny[] funnyRobots = new Funny[]{yelloRobot, purpleRobot};
        for (Funny funny: funnyRobots) {
            funny.makeJoke();
        }
    }
}

Homework

Exercise 1

  • Create a subclass of the class Shape Triangle
  • Implement calculateArea()

Exercise 2

  • Create an interface Movable with a method moveTo(int x, int y)
  • Make BlueRobot movable
  • Move your blue (& purple) robots to different directions

Exercise 3 (Optional)

  • Create a class Human that implements interface Movable
  • Create an object of the class Human
  • Add this object to you Movable array

Exercise 4 (Optional)

  • Create a class Square that extends Rectangular
  • (Hint) a Square is a Rectangular with the same width and length
  • Create a Constructor with one parameter: side

Exercise 5 (Optional)

  • Create many different shapes (Circle, Rectangular, Square, Triangle)
  • add all of them to the array of Shapes
  • for every shape in the array calculate Area

Exercise 6 (Optional)

  • Make abstract class Shape Movable
  • Implement method move() in the Shape class (abstract class can have methods with behaviour)
  • Now you can move shapes and robots together

Preject !!!

Last Updated: 9/18/2019, 8:27:18 PM