Introduction (CB chap. 1 & 2)

Download Report

Transcript Introduction (CB chap. 1 & 2)

Inheritance and Class Hierarchies
Ellen Walker
CPSC 201 Data Structures
Hiram College
• Encapsulation of code into classes makes
reuse easier
• But many times we want to reuse “most of” a
• Inheritance allows information to be
organized so that new classes can be derived
from existing ones, instead of starting from
An Inheritance Hierarchy
• Each object inherits data and methods from parent
• Goal: put information as close to root as possible
Inheritance Example
• Computer
– Hard drive
– Processor
• Laptop
– Battery
– (inherits Hard Drive, Processor from Computer)
• Server
– Additional processors
– (inherits Hard Drive, Processor from Computer)
Inheritance in Java
• All Java classes are arranged in a hierarchy
– Extensive (e.g. Throwable sub-hierarchy)
– Your objects will fit somewhere in this hierarchy
• Object is the superclass of all Java classes
• Subclasses refine or extend their
– A laptop is a computer
– A student is a person
– A TA is a student
Is-a Versus Has-a Relationships
• IS-A
– Denotes subclass relationship
– E.g. Laptop IS-A computer
– In Java, “extends”
– Denotes component relationship
– E.g. Laptop HAS-A battery
– In Java, one class is a data member of another
Superclass and Subclass in UML
• Triangle-arrow
denotes a subclass
• The superclass is also
called “base class”
Three Kinds of Object Visibility
• Public - accessible to any other class
– Use for methods at the “interface”
• Private - accessible to no other class
– Use for data members, as well as methods to be called only
by the methods of this class
• Protected - accessible to subclasses (and classes in
the same package)
– Use when subclasses will need to access item, but external
classes should not
– Dangerous if package contains unrelated classes (or no
package defined)
Constructing a Subclass
• When inherited objects are private, the
subclass cannot access them to initialize!
– This is normal and correct
• Use super() to explicitly construct the
superclass first (with appropriate parameters)
• If not, super() (with no parameters) is
implicitly called
– If the superclass doesn’t have a default (noparameter) constructor, this causes a compiler
Overriding Objects
• Parameter or local object overrides a data object in
the class with the same name; use this. to specify
the “hidden” object
public void setName( String name ) { = name; }
• Subclass’s method overrides parent’s method; use
super. to specify the “hidden” method
public String toString () {
super.toString() + “ “ + battery; }
• Overridden methods must have the same return type
Method Overloading
• Method overloading: having multiple methods
with the same name but different signatures
(number and type of parameters) in a class
• Constructors are often overloaded
• Example:
– MyClass(int inputA, int inputB)
– MyClass(int inputA, int inputB, double inputC)
• Polymorphism means many forms or many
• In OOP, polymorphism refers to a single
(superclass) object that can belong to one of
many (subclass) classes
• Example: list of shapes
– Shape[] myShapes = new Shape[5];
– myShapes[0] = new Square(5);
– myShapes[1] = new Circle(2);
Which Method to Run?
• Consider:
for (int j=0;j<2;j++){
System.out.println(Shapes[j].area()) }
• Compiler doesn’t know which area() method
to run!
• Polymorphism allows the JVM to determine
which method to invoke at run time
– Even though it’s different for different iterations of
the loop!
Abstract Classes
• An interface declares abstract methods
– Contain name and parameters but no implementation
• An abstract class is between an interface and a
(concrete) class
– Can contain abstract methods (specified with keyword
– Can also contain concrete methods and data objects
• Used for specifying polymorphic objects (e.g. Shape)
Classes, Abstract Classes and
Abstract Classes and Interfaces
• Like an interface, an abstract class can’t be
• An abstract class can have constructors to initialize
its data fields when a new subclass is created
– Subclass uses super(…) to call the constructor
• May implement an interface but it doesn’t have to
define all of the methods declared in the interface
– Implementation is left to its subclasses