A7: Abstract Classes & Method Overloading

CPSC 210

Learning Goals

  • To use abstract classes to
    • record the similarity between other types
    • avoid code duplication
    • store common implementations to be inherited by subclasses
  • To describe and use method overloading in the design of an interface or class

Assume all mammals walk, eat, and make noises

public interface Mammal {
  void walk();
  void makeNoise();
  void eat();
}

Assume all dogs walk and eat the same way

public class Dog implements Mammal {
  @Override
  public void walk() {
    System.out.println("waaalk...");
  }
  @Override
  public void eat() {
    System.out.println("mmmmpf...");
  }
  
  
  
  
  
}
@Override
public void makeNoise() {
  System.out.println("NOT SURE"); // ???
}

But different dogs make completely different noises

Why Abstract Classes?

public class Dog implements Mammal {
  // ...
  @Override
  public void makeNoise() {
    System.out.println("NOT SURE"); // ???
  }
}
public class Poodle extends Dog {

}

"Not Sure"?

  • How do I...
    • make any subclass implement its own makeNoise method?
    • implement all methods except makeNoise in the Dog class and make all subclasses implement makeNoise?

Abstract Classes

  • Defines a new type of data
  • Can optionally not provide implementations for methods
  • Cannot be instantiated

Abstract Class

Abstract Classes (2)

  • Must provide implementations for all abstract methods
  • ...OR...
  • Must itself become an abstract class

Actual Class

Abstract Classes (3)

public class Dog implements Mammal {
  // ...
  @Override
  public void makeNoise() {
    System.out.println("NOT SURE");
  }
}
public abstract class Dog implements Mammal {
  // ...
  @Override
  public abstract void makeNoise();
}
public class Poodle extends Dog {
  @Override
  public void makeNoise() {
    System.out.println("Wiiif wiiif");
  }
}

OR (next level!)

public abstract class Dog implements Mammal {






  
}
public class Poodle extends Dog {





}
public abstract void bark();
@Override
public void makeNoise() {
  bark();
}
@Override
public void bark() {
  System.out.println("Wiiif wiiif");
}

Type Substitution

  • Everything we have learned about type substitution also applies to type hierarchies with abstract classes
    • You can use any subtype for a super type
  • But: cannot create instances of abstract classes
Dog dog = new Dog();
Dog poodle = new Poodle();
Dog borderCollie = new BorderCollie();
Dog germanShepherd = new GermanShepherd();

dog.bark(); // Woof Woof (generic)
poodle.bark(); // Wiiif Wiiif
borderCollie.bark(); // Wuuuf Wuuuf
germanShepherd.bark(); // Waaaf Waaaf

Method Overloading

public abstract class Dog {
  // ...
  abstract void bark();
  abstract void bark(int times);
}
  • Two methods with same name but different parameter lists
  • We can reuse a name to describe similar operations that take different parameters as input
  • Methods can also call each ​other
public class Poodle extends Dog {
  @Override
  public void bark() {
    System.out.println("Wiiif wiiif");
  }
  @Override
  public void bark(int times) {
    for (int i = 0; i < times; i++) {
      bark();
    }
  }
}

Lecture Ticket Review

public abstract class Barista {
  public void serveCoffee(){
    greet();
    giveCoffee();
    sayGoodbye();
  }
  protected _________ void giveCoffee()   
  protected _________ void greet()
  protected _________ void sayGoodbye()
}

public class HappyBarista {
  // greet and say goodbye in a cheery way
}

public class GrumpyBarista {
  // greet and say goodbye in a grumpy way
}

What methods should be abstract?

What methods should be implemented in HappyBarista and GrumpyBarista?

Lecture Lab

Get Oval and Rectangle Working at the Same Time

Lecture Lab (A)

Lecture Lab (B1)

Lecture Lab (B2)

Lecture Lab (C)

A7: Abstract Classes & Method Overloading

The End - Thank You!

super
protected
method override
supertype
subtype
declared/apparent type
actual/instantiated type
method dispatch
method overload

CPSC210 - A7 Abstract Classes & Method Overloading

By Felix Grund

CPSC210 - A7 Abstract Classes & Method Overloading

  • 1,348