240 likes | 529 Views
Inheritance. in the Java programming language J. W. Rider. Object-oriented features. Encapsulation Inheritance Polymorphism. Inheritance. Inheritance means that we never have to start over from scratch when defining a new class that similar to an existing class. Class/Interface name usage.
E N D
Inheritance in the Java programming language J. W. Rider
Object-oriented features • Encapsulation • Inheritance • Polymorphism
Inheritance • Inheritance means that we never have to start over from scratch when defining a new class that similar to an existing class.
Class/Interface name usage • Import statement • Extends clause • Implements clause • Constructor header/reference • Static member reference • Method return type • Variable type • Typecasting • Class testing with instanceof Defines “ancestry” of class Tests “ancestry” of object
Ancestry • Extends • One class may extend another class • One interface may extend another interface • Implements • A class may implement zero, one or many interfaces.
Super/sub classes • Ancestry establishes a super-class/subclass relationship between two classes/interfaces. Super class Subclass “extends” “parent” “child”
Extends vs Implements Implementation1 Super class Implementation2 ImplementationN … “extends” “implements” Subclass
Object class is origin of all extensions Object I2 C1 C2 I1 I3 Extends I2 C3 Extends C2
Single Inheritance / Multiple Ancestry Object MyGrandParent MySuperParent MyClass
Encapsulation revisited • A “class” is an encapsulation of its members. • An “object” is an encapsulation of ALL of the non-static fields defined in its instantiated class and its ancestry.
Class qualifiers • Public • Abstract • Final
The abstract qualifier • Abstract methods contain no body. • The method header terminates with a semicolon rather than an open brace. • A method with an empty body (vice “no body”) would have the header terminated with {}. • Abstract classes may not be instantiated. • Frequently, but NOT required, abstract classes contain abstract methods. Concrete methods may be implemented. Constructors may be included. Abstract methods are not required. • A class that contains an abstract method MUST be declared abstract itself. • In order to create an object based upon the abstract class, the class needs to be extended by another class.
The final qualifier • A final class may never be extended. • A final method may never be overridden. • A final variable may never be assigned a value.
The super keyword • On the first executable line of a constructor, super is used as the name of the constructor of the super class. • When dereferenced, super permits overridden methods in the super class to be called.
Overloading/overriding methods • Overloading • Same method name, different formal parameters • Compiler determines which method gets called. • Overriding • Same method name, same formal parameters • Only a subclass may override the method of an ancestor. • Object determines which method gets called.
The protected qualifier • As a member qualifier, protected is accessible by a subclass, but not by other classes out of the package of the super class. • Protected scope is often used for methods that are expected to be overridden by subclasses.
Polymorphism • From the Greek for “accommodating many shapes”, • Polymorphism supports programmers in writing code that accommodates objects from different classes.
Polymorphic features • Subclass assignment compatibility • Instanceof operator • “Virtual” methods • Dynamic or late binding
Subclass assignment compatibility • A reference value may be assigned to a variable whose type is derived from an ancestor class or interface. MySuperClass x = new MySubClass(); • The compiler uses only the type of the variable to determine what messages may be sent to the object.
The instanceof operator • Object-referenceinstanceofClassname • Returns a Boolean result of true if the referenced object has Classname as one of its ancestors.
“Virtual” methods • The compiler does not choose which possibly-overridden method might be called when a method is not-static, not-private, and not-final. • Instead, the object is sent a message, and the object decides what method should be invoked.
Design consideration Suppose we desire two constructible classes (A and B) to share a considerable amount of common code. • If instances of A are assignable to variables of type B, have class A extend class B and make any changes within A. • If instances of A and B are not assignable to variables of the other type, create a mutual abstract class and extend both A and B from the abstract class.
Summary • Inheritance is just one of many ways to relate two classes with each other. • Inheritance can keep us from writing duplicate code for classes. • Inheritance allows us to write polymorphic code that uses a common algorithm for solving similar problems.