inherit po

11
1 9/10/2003 Inheritance 1 Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance • Inheritance The property that instances of a sub-class can access both data and behavior associated with a superclass In programming languages, inheritance means that the behavior and data associated with subclasses are always an extension of the properties associated with the parent class A child is a more specialized form of the parent 9/10/2003 Inheritance 2 Transitivity Inheritance is always transitive A class can inherit features from superclasses many levels away If class dog is a subclass of class mammal, and class mammal is a subclass of animal, then dog will inherit attributes from both mammal and animal Multiple inheritance occurs when a subclass has more than one superclass Java does not support multiple inheritance, but other OO languages do (C++, Eiffel, …)

Upload: anonymous-3q6hik

Post on 12-May-2017

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inherit po

1

9/10/2003 Inheritance 1

Inheritance

• Classes can be organized in a hierarchical structure based on the concept of inheritance

• Inheritance– The property that instances of a sub-class can access

both data and behavior associated with a superclass

• In programming languages, inheritance means that the behavior and data associated with subclasses are always an extension of the properties associated with the parent class– A child is a more specialized form of the parent

9/10/2003 Inheritance 2

Transitivity

• Inheritance is always transitive– A class can inherit features from superclasses many

levels away– If class dog is a subclass of class mammal, and class mammal is a subclass of animal, then dog will inherit attributes from both mammal and animal

• Multiple inheritance occurs when a subclass has more than one superclass

• Java does not support multiple inheritance, but other OO languages do (C++, Eiffel, …)

Page 2: Inherit po

2

9/10/2003 Inheritance 3

Substitutability

• Since– Instances of a subclass contain all the state and

behavior associated with the superclass

• This means– An instance of a subclass can mimic the behavior of the

superclass and should be indistinguishable from an instance of the superclass

• So– It is possible to substitute instances of the subclass for

the superclass in any situation with no observable effect

9/10/2003 Inheritance 4

Forms of Inheritance

Form of Inheritance DescriptionSpecialization The subclass is a specialized form of the

superclass but satisfies the specifications of theparent class in all relevant aspects.

Specification The superclass defines behavior that isimplemented in the subclass but not in thesuperclass.

Generalization The subclass modifies or overrides some of themethods of the superclass.

Extension The subclass adds new functionality to the parentclass, but does not change any inherited behavior.

Limitation The subclass restricts the use of some of thebehavior inherited from the superclass.

Combination The subclass inherits features from more than onesuperclass (i.e. multiple inheritance)

Page 3: Inherit po

3

9/10/2003 Inheritance 5

Syntax

• A subclass inherits from a superclass using the extends keyword

• Inheritance is applicable to top-level classes, nested top-level classes, member classes, local classes and anonymous classes

class subClassName extends superClassName {variable and method declarations

}

9/10/2003 Inheritance 6

Inheritance

• A class can inherit from any class that is not final.• Objects of the subclass contain all the instance

variables and methods declared by the superclass.• The accessibility rules are still enforced which

means a subclass cannot access the private parts of the superclass.

• Subclassing can be repeated as many times as desired. A class can have only one superclass, but may have many subclasses.

Page 4: Inherit po

4

9/10/2003 Inheritance 7

Scope Rules

• Inheritance increases the number of scopes that need to be searched (both static and instance declarations are searched).– Check the local scope and any local scopes.– Check the class scope.– Check each superclass scope in turn up to the top of the

inheritance chain.

• If variables with the same identifier are declared in several scopes, the first one found is used.

9/10/2003 Inheritance 8

Method Overloading

• Methods can be overloaded, meaning that two or methods in the same class can have the same name provided they have different parameter lists.

• The return type for all overloaded methods must be the same.

• Operator overloading is not supported in java.

Page 5: Inherit po

5

9/10/2003 Inheritance 9

Method Overriding

• A subclass can override an inherited method by providing a new method declaration that has the same name, the same number and types of parameters and the same result type as the one inherited.

• Method overriding relies on dynamic binding, so the type of the object determines which method gets called.

9/10/2003 Inheritance 10

Abstract Classes

• An abstract class is a place holder for declaring shared methods and variables for use by subclasses.

• An abstract class cannot have instance objects and so exists as a class that other classes can inherit from.

• A concrete class is a class that is not abstract.

Page 6: Inherit po

6

9/10/2003 Inheritance 11

Abstract Methods

• A method can be declared abstract so that it must be overridden by subclasses.

• An abstract class does not have a method body; The declaration ends with a semi-colon not a compound statement.

• A class declaring one or more abstract methods must be declared as an abstract class.

• Private and static methods cannot be abstract.

9/10/2003 Inheritance 12

Stack

abstract class Stack {protected int count = 0;

public abstract void push( Object o );public abstract void pop();public abstract Object top();public abstract boolean isFull();

public boolean isEmpty() {return count==0;

}}

Page 7: Inherit po

7

9/10/2003 Inheritance 13

ArrayStackpublic class ArrayStack extends Stack {private Object data[];private tos = -1;

public ArrayStack() { data = new Object[ 100 ]; }

public void push( Object o ) {if ( !isFull() ) {tos++; data[ tos ] = o; count++;

}}

public void pop() {if ( !isEmpty() ) { tos--; count--; }

}

public Object top() { return data.lastElement(); }public boolean isFull() { return tos == ( data.length – 1);

}}

9/10/2003 Inheritance 14

Final Methods

• A final instance method cannot be overridden (but can still be overloaded).

• A final static method cannot be re-declared in a sublcass.

• Final methods prevent a method that has the same name and parameter types from being declared in a subclass.

• This takes into account both static and instance variables.

Page 8: Inherit po

8

9/10/2003 Inheritance 15

Constructors and Inheritance

• The guarantee of proper initialization must be maintained in the presence of inheritance.

• Java forces the constructors for each superclass to be called and provides syntax for explicitly controlling which constructors are called.

• The keyword super can be used to explicitly call a superclass constructor.– super ( argumentList ) ;

• super must be the first statement in a constructor.

9/10/2003 Inheritance 16

Methods Inherited From Class Object

• Class Object declares the following methods that can be overwritten:– public boolean equals( Object obj );– public String toString();– public final native int hashCode() ;– protective native Object clone();– protected void finalize();– public final Class getClass()

Page 9: Inherit po

9

9/10/2003 Inheritance 17

Interfaces

• An interface declaration allows the specification of a reference type without providing an implementation.

• A type can conform to another type if it specifies at least the same set of methods as the other type (and possibly more).

• The two types do not have to be related by inheritance which gives more freedom as to which types may conform to other types.

9/10/2003 Inheritance 18

Syntax

• An interface is declared as shown below:

• The optional modifier allows an interface to be declared public.

• Any variables declared are implicitly constants and are also static.

interfaceModifier interface identifier {interfaceMethodDeclarations;interfaceVariableDeclarations;

}

Page 10: Inherit po

10

9/10/2003 Inheritance 19

Implements

• The implements keyword allows a class to implement (or conform to) one or more interfaces.

• A class can implement any number of interfaces (and also extend a class at the same time).

• Any variables defined in the interface become static variables of the class.

• A method declared in a public interface must be public in an implementing class.

9/10/2003 Inheritance 20

Inheritance and Association

• Inheritance actually serves two distinct purposes.– A mechanism for sharing implementation by making

one class an extension of another.– A mechanism for sharing a public interface such that an

object of one class can safely be substituted for an object of another class.

• Is inheritance the only way to share code?– An alternative is to put shared methods and variables

into another class and gain access via an instance variable referring to an object of that class.

Page 11: Inherit po

11

9/10/2003 Inheritance 21

Inheritance and Association

• How do you decide which is approach to use in a given situation?– You have to think carefully about why the classes need

to be related• If a class simply needs to use the implementation of another

class, but does not need the same public interface then association should be used

• If the decision is made to use inheritance, then both the implementation and the public interface of the proposedsuperclass as needed

– If a class Y is-a or is-a-kind-of class X, then inheritance is probably the right way to go