inheritance in the java programming language j. w. rider
TRANSCRIPT
![Page 1: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/1.jpg)
Inheritancein the Java programming language
J. W. Rider
![Page 2: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/2.jpg)
Object-oriented features
EncapsulationInheritancePolymorphism
![Page 3: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/3.jpg)
Inheritance
Inheritance means that we never have to start over from scratch when defining a new class that similar to an existing class.
![Page 4: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/4.jpg)
Class/Interface name usage
Import statementExtends clauseImplements clauseConstructor header/referenceStatic member referenceMethod return typeVariable typeTypecastingClass testing with instanceof
Defines “ancestry” of class
Tests “ancestry” of object
![Page 5: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/5.jpg)
Ancestry
Extends– One class may extend another class– One interface may extend another interface
Implements– A class may implement zero, one or many
interfaces.
![Page 6: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/6.jpg)
Super/sub classes
Ancestry establishes a super-class/subclass relationship between two classes/interfaces.
Super class Subclass“extends”
“parent” “child”
![Page 7: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/7.jpg)
Extends vs Implements
Super class
Subclass
Implementation1
Implementation2
ImplementationN“extends”
“implements”
…
![Page 8: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/8.jpg)
Object class is origin of all extensions
Object
C1
C3Extends
C2
I1
I2
C2
I3Extends
I2
![Page 9: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/9.jpg)
Single Inheritance / Multiple Ancestry
Object
MyClass
MySuperParent
MyGrandParent
![Page 10: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/10.jpg)
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.
![Page 11: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/11.jpg)
Class qualifiers
PublicAbstractFinal
![Page 12: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/12.jpg)
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.
![Page 13: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/13.jpg)
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.
![Page 14: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/14.jpg)
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.
![Page 15: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/15.jpg)
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.
![Page 16: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/16.jpg)
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.
![Page 17: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/17.jpg)
Polymorphism
From the Greek for “accommodating many shapes”,Polymorphism supports programmers in writing code that accommodates objects from different classes.
![Page 18: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/18.jpg)
Polymorphic features
Subclass assignment compatibility– Instanceof operator
“Virtual” methods – Dynamic or late binding
![Page 19: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/19.jpg)
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.
![Page 20: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/20.jpg)
The instanceof operator
Object-reference instanceof Classname
Returns a Boolean result of true if the referenced object has Classname as one of its ancestors.
![Page 21: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/21.jpg)
“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.
![Page 22: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/22.jpg)
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.
![Page 23: Inheritance in the Java programming language J. W. Rider](https://reader035.vdocument.in/reader035/viewer/2022062304/56649eea5503460f94bfbbb7/html5/thumbnails/23.jpg)
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.