• No results found

Lesson A20 notes: Inheritance, Polymorphism, and Abstract Classes

In document Java Notes (Page 76-79)

Introduction

o a class represents a set of objects that share same structure and behaviors

o class determines structure by specifying variables that are contained in each instance of class o it determines behavior by providing methods that express behavior of objects

o central new idea in object-oriented programming is to allow classes to express similarities among objects that share some, but not all, of their structure and behavior

o main goals of OOP are team development, software reusability, and easier program maintenance

o OOP concepts to serve these goals are abstraction, encapsulation, inheritance, and polymorphism

Inheritance

o key element in OOP is ability to derive new classes from existing classes by adding new methods and redefining existing methods…new class can inherit many of its attributes and behaviors from the class…process of deriving new classes from existing classes is called inheritance

o general class that forms basis for inheritance is called the superclass

o specialized class that inherits from superclass is called the subclass (or derived class).

o in Java, all classes belong to one big hierarchy derived from most basic class, called Object…this class provides a few features common to all objects…it is useful for implementing structures that can deal with any type of object

o if we start a class from “scratch,” the class automatically extends Object

public class SeaCreature {

...

}

is equivalent to:

public class SeaCreature extends Object{

...

}

o when new classes are derived from SeaCreature, a class hierarchy is created. For example:

public class Fish extends SeaCreature

{ ...

}

public class Mermaid extends SeaCreature {

...

}

o this results in hierarchy shown below

Abstract Classes

o Java allows us to formally define an abstract class…in an abstract class, some or all methods are declared abstract and left without code.

o an abstract method has only a heading: a declaration that gives method‟s name, return type, and arguments…an abstract method has no code

o for example, all of methods in definition class shown below are abstract…SeaCreature tells us what methods a sea creature must have, but not how they work.

// A type of creature in the sea public abstract class SeaCreature {

// Called to move the creature in its environment public abstract void swim();

// Returns the name of the creature public abstract String getName();

}

o in an abstract class, some methods and constructors may be fully defined and have code supplied for them while other methods are abstract

o subclasses of an abstract class have more and more methods defined…eventually, down inheritance line, code is supplied for all methods

o a class where all methods are fully defined is called a concrete class…a program can only create objects of concrete classes (an object is called an instance of its class)…an abstract class cannot be instantiated

o different concrete classes in same hierarchy may define same method in different ways:

public class Fish extends SeaCreature{

...

// returns the name of the creature

public String getName() {return "Wanda the Fish";}

}

public class Mermaid extends SeaCreature{

...

// returns the name of the creature

public String getName() {return "Ariel the Mermaid";}

Figure 20-1. SeaCreature and two derived classes

}

o a class may be declared abstract for other reasons as well…for example, some of instance variables in an abstract class may belong to abstract classes

Polymorphism

o can refer to objects of specific types through more generic reference o can mix objects of different subtypes in the same collection:

SeaCreature s = new Fish(...);

...

s.swim();

o instance of Fish class is a Fish, but also a SeaCreature

o there are situations that require reference to an object using its more generic supertype rather than its most specific type

o for example, when different subtypes of objects in same collection (array, list, etc.) are mixed:

ArrayList <SeaCreature> creature = new ArrayList <SeaCreature>;

Creature.add( new Fish(...));

Creature.add( new Mermaid(...));

...

creature.get(currentCreature).swim();

o Fish and Mermaid classes provide two different implementations of the swim method, so correct method is located by the Java virtual machine

o overriding method

o when subclass redefines implementation of a method from its superclass o actual method to call is determined at run time

o called dynamic binding or late binding

o polymorphism: principle that actual type of object determines method to be called (Greek for

“many shapes”)…same computation works for objects of many forms and adapts itself to nature of object

o overloading a method

o often confused with overriding because names are so similar o means to keep name of method, but change parameter list

o compiler can choose method that is actually called because the signatures are different

Interfaces

o Java has a class-like form called an interface that can be used to encapsulate only abstract methods and constants

o can be thought of as a blueprint or design specification

o a class that uses interface is a class that implements the interface

o similar to an abstract class: it lists methods, giving their names, return types, and argument lists, but does not give any code

o difference is that abstract class may have some constructors and methods implemented, while an interface does not give any code for its methods, leaving their implementation to a class that implements interface

o interface and implements are Java reserved words…here is an example:

public interface Comparable {

public int compareTo(Object other);

}

o looks like class definition, except implementation of compareTo() method is omitted o a class that implements Comparable interface must provide an implementation for this

method…class can also include other methods and variables…for example,

class Location implements Comparable {

public int compareTo(Object other) {

. . . // do something -- presumably, compare objects }

. . . // other methods and variables }

o a class can implement any number of interfaces…a class can both extend another class and implement one or more interfaces:

class Fish extends SeaCreature implements Locatable, Eatable {

...

}

o can declare a variable whose type is given by interface…if Fish and Mermaid are classes that implement Locatable, you could say:

Locatable nemo;

nemo = new Fish(); // nemo now refers to an object of type Fish nemo.location(); // Calls location () method from class Fish nemo = new Mermaid(); // Now, nemo refers to an object of type Mermaid nemo.location(); // Calls location() method from class Mermaid

o a variable of type Locatable can refer to any object of any class that implements Locatable interface…statement nemo.location() is legal because nemo is of type Locatable and any Locatable object has a location() method

In document Java Notes (Page 76-79)

Related documents