Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.

21
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    1

Transcript of Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.

Object-Orientated Design and Programming Unit 9: Abstract

Classes and Interfaces Jin Sa

Objectives

• The objectives are to know when to use abstract classes and when to use interfaces;

• to know how to define and use abstract classes and interfaces.

Abstract and Concrete Classes

• Concrete class can have (direct) instances.

• Abstract class cannot have its own (direct) instances.

• Abstract classes exist purely to generalize common behaviour that would otherwise be duplicated across (sub)classes.

• Abstract classes (normally) have abstract methods

Example of Abstract and Concrete classes

Example of Abstract and Concrete classes

• The Employee class contains one abstract method called calculatePay().

• It is an abstract method with no implementation. • each of the subclasses provides its own

implementation of the calculatePay method. This means that each class provides an implementation of the abstract behaviour but each subclass does that in a slightly different way.

Abstract classes

• Class must be declared as abstract.

• Typically an abstract class contains one or more abstract method.

• Use abstract classes when you need to provide some functionality but also wish to defer some implementation to the subclasses.

• In UML it is shown in italics.

Inheritance Mechanisms

• Inheritance of Implementation. The operation signature and implementation method are passed without modification from superclass to subclass.

• Inheritance of Interface. The superclass offers an abstract operation signature. The subclass inherits the operation signature but must provide an implementation method if the class is to be concrete, i.e. have instances.

• Overriding Inheritance. The operation signature and method are inherited by the subclass but the method is modified to exhibit behaviour appropriate to that class.

Abstract classes in Java

• Consider an application that deals with different shapes– Circle, Triangle, Square– Common attributes and methods: colour,

findArea()– How to define findArea() for the Shape class?

• Java provides a mechanism called abstract method to allow us to represent such methods. Abstract methods do not have any definition.

public abstract class Shape {

private String colour="red";

Shape() {

System.out.println("Shape's constructor called");

}

… …//other methods

public String toString(){

return "Colour is:"+ colour;

}

public abstract double findArea();

}

Abstract classes and methods

• Abstract methods such as findArea() provide “place holders” for methods that can sensibly be mentioned at one level, but that are going to be implemented in a variety of ways in the subclasses.

• An abstract class is a class usually with at least one abstract method.

• Abstract classes cannot have instances.

Extending an abstract class

public class Square extends Shape{

private double length;

/** Creates a new instance of Square */

public Square(double l) {

length=l;

}

public double findArea(){

return length*length;

}

}

Student activity 9.1 (D.1)

• Let’s model the concept of a pet. A pet has a name; it has activities. For example, a fish swims; a cat catches mice and a dog guards home.

• Define a collection of classes that would model the concepts of pet, fish, cat and dog. Provide suitable methods to display the information about a pet, including its name and the kind of activity it can do.

• Define a testing class in which you create some instances of Fish, Cat and Dog and display the information of these pets.

Polymorphism via abstract classes

• Student activity 9.2

• Modify the above program. Create a collection of pets. Provide a polymorphic solution to display the information of each pet.

Interface

• An interface is an elegant way of defining the public services that a class will provide without being bogged down by implementation details.

• Think of an interface as a business contract between two parties. The class implementing the interface agrees to provide the services defined in that interface to other classes.

• Interface has no attribute

Interface in Java

• An interface is a class like construct that contains only constants and abstract methods.

• An interface defines the specification of a set of constants and methods.

• The interface acts as a guarantee or a contract: any class that implements the interface is guaranteed to provide implementations for these abstract methods.

• an interface is similar to an abstract class, but an abstract class can contain variables and concrete methods as well as constants and abstract methods.

Interface example

public interface LifeStyle{

void haveGoodFood();

void haveAccommodation();

}

Implementing interface

public class MScStudent2 extends Student implements LifeStyle{

private String supervisor;

MScStudent2(String nm){

super(nm);}

… … //other methods

public void haveGoodFood(){

System.out.println("Lots of bread, meat and veg");

}

public void haveAccommodation(){

System.out.println("have a single room in a shared house");

}

}

Polymorphism via interface

• Same as polymorphism via inheritance, Java also supports polymorphism via interfaces.

public interface Speaker {

void speak();

void announce(String s);

}

Speaker guest;

guest=new Dog();

guest.speak();

guest=new Politician();

guest.speak();

Student activity 9.4 (D.2)

• Implementing polymorphism via interface• Provide suitable definitions for the Dog class

and the Politician class. Both the Dog class and the Politician class should implement the Speaker interface. You may also want to define other methods that are suitable for each class. For example, you can define a method called lobbying() in the Politician class.

Abstract classes vs Interfaces

• In an interface, the data must be constants; an abstract class can have all types of data.

• Each method in an interface has only a signature without implementation; an abstract class can have concrete methods.

When do we use an interface and abstract class?

• A strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes.

• A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property. A weak is-a relationship can be modeled using interfaces.

•  Interfaces can partially support multiple inheritances.

• The concepts of abstract classes and interfaces play an important role in software development. Using abstract classes and interfaces, to identify the boundaries of different parts of a software system without having to provide details about their implementations.