Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Fundamentals. Inheritance A derived class extends a base class. It inherits all of its methods (behaviors) and attributes (data) and it may.

Similar presentations


Presentation on theme: "Inheritance Fundamentals. Inheritance A derived class extends a base class. It inherits all of its methods (behaviors) and attributes (data) and it may."— Presentation transcript:

1 Inheritance Fundamentals

2 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. class A class B extends A Base class Derived class Base class attributesattributes inherited from base Additional attributes Base class methods methods inherited from base Additional methods

3 Inheritance class Dog extends Animal class Cat extends Animal class Animal private String name, sound; private double xpos, ypos; 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 class name attributes (data) methods (behavior) public Dog(String n) {..} public void chaseCats(Cat c){.} public Cat(String n) {..} public void runAway( ) {..} //no additional attributes Base class Derived classes Derived classes have their own constructors Derived classes may add their own unique behaviors

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

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. public class 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 Inheritance Constructors for derived classes public class 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); } public void chaseCats(Cat theCat) { moveTo(theCat.getX( ), theCat.getY( )); theCat.runAway( ); } //inherits all other behavior from Animal } Client code in an application Dog lassie = new Dog( ); Invokes the default constructor (even if one is not supplied by the coder). default constructor of base class will be implicitly invoked Dog lassie = new Dog(“Lassie”); Dog lassie = new Dog(“Lassie”, 2.5, 3.0); 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 Inheritance Visibility modifiers class A private int mySecret; private void swap(Object x, Object y); protected int familySecret; protected void sort( ); public void arrange( ); public int getMySecret( ); public void setMySecret( ); private features are accessible only to all member functions of the class. method swap( ) is used internally by sort( ) 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.

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.


Download ppt "Inheritance Fundamentals. Inheritance A derived class extends a base class. It inherits all of its methods (behaviors) and attributes (data) and it may."

Similar presentations


Ads by Google