1 / 14

Inheritance

Inheritance. Fundamentals. class A. class B extends A. Inheritance. A derived class extends a base class. It inherits all of its methods (behaviors) and attributes (data) and it may have additional behaviors and attributes of its own. Derived class. Base class.

Download Presentation

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 Fundamentals

  2. class A class B extends A Inheritance A derived class extends a base class. It inherits all of its methods (behaviors) and attributes (data) and it may have additional behaviors and attributes of its own. Derived class Base class Base class attributes attributes inherited from base Additional attributes Base class methods methods inherited from base Additional methods

  3. class name class Animal private String name, sound; private double xpos, ypos; attributes (data) public Animal(String n, String s) {..} public void speak( ) {..} public void moveTo(double x,double y){..} public double getX( ) {..} public double getY( ) {..} Getters and setters for name, sound, xpos, ypos methods (behavior) class Dog extends Animal class Cat extends Animal Inheritance Base class Derived classes have their own constructors Derived classes may add their own unique behaviors Derived classes //no additional attributes //no additional attributes public Dog(String n) {..} public Cat(String n) {..} public void chaseCats(Cat c){.} public void runAway( ) {..}

  4. Getters and Setters for name, sound, xpos, ypos public void setName(String newName) { this.name = newName; } public String getName() { return this.name; } public void setxPos(int newxPos) { this.xpos = newxPos; } public int getxPos() { return this.xpos; }

  5. Getters and Setters for name, sound, xpos, ypos public void setSound(String newSound) { this.sound = newSound; } public String getSound() { return this.sound; } public void setyPos(int newyPos) { this.ypos = newyPos; } public int getyPos() { return this.ypos; }

  6. Inheritance Inheritance expresses an is-a association between two (or more) classes. A derived class object inherits ALL of the attributes and behaviors of the base class and may have additional features {attributes and/or behaviors} of its own. This is what is conveyed by the keyword extends. A derived class should NOT inherit from a base class to obtain some, but not all, of its features. Such a use of inheritance is possible in Java (and is done in practice all too frequently), but it is an incorrect use of inheritance and should be avoided! Instead of inheriting to extract some, but not all, of the features of a parent class, extract out (generalize) the common features of the two classes and place them in a third class from which the other two both inherit. Generalization pertains only to classes you build yourself. You don’t have the ability to generalize standard library classes!

  7. Inheritance Generalization

  8. Features of A common to B class A Features belonging only to A attribute1 Features belonging only to B attribute2 method1 class C method2 attribute1 method3 method1 method2 class B attribute1 attribute3 class A extends C class B extends C method1 method2 attribute2 attribute3 method4 method3 method4 Inheritance Generalization Extract common features of A and B and put in new class C Classes A and B extend C with features particular to each Let classes A and B inherit from class C Consider two classes A and B that have some common features

  9. Inheritance Constructors for Derived Classes

  10. Inheritance Constructors for derived classes In the previous example, the constructor for the base class Animal takes two string parameters from the client to initialize its attributes name and sound. publicclass Animal { privateString name, sound; private double xPos, yPos; public Animal( ) { //default constructor name = “”; sound = “mute”; xPos = 0.0; yPos = 0.0; } public Animal(String myName, String mySound) { name = myName; sound = mySound; xPos = 0.0; yPos = 0.0; } public Animal(String myName, String MySound, double myX, double myY) { name = myName; sound = mySound; xPos = myX; yPos = myY;} //other methods of class Animal including getters and setters The constructor may be overloaded with different parameters

  11. default constructor of base class will be implicitly invoked Dog lassie = new Dog(“Lassie”); Dog lassie = new Dog(“Lassie”, 2.5, 3.0); Inheritance Constructors for derived classes Client code in an application publicclass Dog extends Animal { //inherits attributes of Animal and add no additional ones public Dog( ) { } public Dog(String myName) { super (myname, “Bow-wow”); } public Dog(String myName, double myX, double myY) { super (myName, “Bow-wow”, myX, myY); } publicvoid chaseCats(Cat theCat) { moveTo(theCat.getX( ), theCat.getY( )); theCat.runAway( ); } //inherits all other behavior from Animal } Dog lassie = new Dog( ); Invokes the default constructor (even if one is not supplied by the coder). The derived class must initialize the inherited attributes of the base class by invoking its constructor with a call to super. (This must be the first statement in the constructor.)

  12. Inheritance Visibility Modifiers

  13. private features are accessible only to all member functions of the class. protected features are also available to methods of a derived class. public features can be accessed by client code as well as methods of the base and derived classes. Inheritance Visibility modifiers class A privateint mySecret; privatevoid swap(Object x, Object y); method swap( ) is used internally by sort( ) protectedint familySecret; protectedvoid sort( ); publicvoid arrange( ); publicint getMySecret( ); publicvoid setMySecret( );

  14. Inheritance Visibility modifiers In the previous example we assumed that the private method swap( ) was used internally by sort( ) and that method arrange( ) was implemented by calls to sort( ). • Methods of derived classes cannot directly access swap( ), but they do have access to sort( ) that uses swap( ) internally. • Client code (applications) can access method arrange( ) that uses sort( ) and indirectly swap( ) internally. • Derived classes can override any protected methods from the base and change the visibility to public – the “public” gets access to the overriden method in the derived class, but not the original sort( ) method defined in the base class. • If a derived class overrides method sort( ), it cannot use the swap( ) method that was used in the base class.

More Related