1 / 23

Inheritance

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.

kyle
Télécharger la présentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance in the Java programming language J. W. Rider

  2. Object-oriented features • Encapsulation • Inheritance • Polymorphism

  3. Inheritance • Inheritance means that we never have to start over from scratch when defining a new class that similar to an existing class.

  4. 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

  5. Ancestry • Extends • One class may extend another class • One interface may extend another interface • Implements • A class may implement zero, one or many interfaces.

  6. Super/sub classes • Ancestry establishes a super-class/subclass relationship between two classes/interfaces. Super class Subclass “extends” “parent” “child”

  7. Extends vs Implements Implementation1 Super class Implementation2 ImplementationN … “extends” “implements” Subclass

  8. Object class is origin of all extensions Object I2 C1 C2 I1 I3 Extends I2 C3 Extends C2

  9. Single Inheritance / Multiple Ancestry Object MyGrandParent MySuperParent MyClass

  10. 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.

  11. Class qualifiers • Public • Abstract • Final

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. Polymorphism • From the Greek for “accommodating many shapes”, • Polymorphism supports programmers in writing code that accommodates objects from different classes.

  18. Polymorphic features • Subclass assignment compatibility • Instanceof operator • “Virtual” methods • Dynamic or late binding

  19. 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.

  20. The instanceof operator • Object-referenceinstanceofClassname • Returns a Boolean result of true if the referenced object has Classname as one of its ancestors.

  21. “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.

  22. 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.

  23. 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.

More Related