1 / 20

Inheritance

Inheritance. J. Sant. Inheritance. A programmer can specify that a new class should inherit features (e.g. data and methods) from a previously defined class. Base Class. Derived Class. Inheritance. Inheritance is used to represent “is a” relationships.

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 J. Sant.

  2. Inheritance • A programmer can specify that a new class should inherit features (e.g. data and methods) from a previously defined class. Base Class Derived Class

  3. Inheritance • Inheritance is used to represent “is a” relationships. A savings account IS A type of account. A barrel type IS A type of container. • Composition is used to represent “has a” relationship. A car HAS AN engine. A gas station HAS pumps.

  4. Inheritance Tree Deciduous Coniferous Poplars Oaks Pines Spruce

  5. Inheritance Vehicle Airborne Land Helicopter Fixed Wing Train Car Car

  6. Inheritance Example. class Container { //BASE CLASS private int id; // more attributes and methods. } class Barrel extends Container { //DERIVED CLASS private double diameter; private double height; // more attributes and methods. }

  7. Barrel { Container Attributes and Methods Barrel Attributes and Methods

  8. Inheritance • Barrel inherits all variables and methods from Container. • Barrel can directly access only non-private members of Container. More about scope later. • If a method of a derived class is called and the method is not defined in that class, the method of the same name in the superclass will be called.

  9. class Parent { public void whoAmI(){ System.out.println(“The Parent”); } } class Daughter extends Parent{ } class Son extends Parent { public void whoAmI(){ System.out.println(“The Son”); } } public class TestInheritance { public static void main(String[] s ){ Parent p = new Parent(); Son s = new Son(); Daughter d = new Daughter(); p.whoAmI(); // Print statement 1 s.whoAmI(); // Print statement 2 d.whoAmI(); // Print statement 3 } } What is printed by TestInheritance?

  10. Inheritance and Constructors • When a derived class is constructed the constructor of the parent class is automatically called first. • You can directly call the constructor of the parent in the child constructor by using super(). This is sometimes useful to call the parents custom constructors and pass on arguments.

  11. Inheritance and Constructors class Container { //BASE CLASS // ……data declarations go here public Container( int id ){ this.id = id; } …etc…etc } class Barrel extends Container { //DERIVED CLASS // ……data declarations go here public Barrel( int id, double diam, double height ){ super(id); this.diam = diam; this.height = height; } …etc…etc}

  12. Questions. • What keyword is used to indicate a class definition is using another class definition as its starting point? • Does the derived (or child) class inherit all members of the base (or parent) class? • Can the derived (or child) class access all members of the base (or parent) class? • How can a derived constructor call the constructor of the parent class. • If super() is the parent’s constructor called anyway.

  13. Demo. • Base Class Guitar stores information on the Maker and the Model Number. • Derived Class AcousticGuitar adds a string value to indicate what type of guitar strings it uses (nylon or steel) and a string to indicate what type of top it has (typically cedar, spruce).

  14. Your turn. • Using the base class, Guitar, define a new class called ElectricGuitar which stores information on the number of pickups (these are electronic devices that pickup the vibrations of the strings for amplification) in addition to the information stored by the base class.

  15. Inheritance in scope. • You control which parts of a program can access a specific variable by access modifiers, public, protected and private. • If you don’t provide an access modifier it is assumed that all classes in the same package can access the variable; it is package scope. This typically means all classes in the same directory. • A rule of thumb is that the more restricted the scope of a variable, the safer that variable

  16. Inheritance in scope. • protected scope allows you to share variables with any derived class and restrict access from any classes in other packages. With protected scope however, non-derived classes in the same package will have access to those variables. • private variables are always only accessible to the methods in the same class. • A rule of thumb is that the more restricted the scope of a variable, the safer that variable

  17. Inheritance and Scope class Parent { private int a; protected int b; int c; } class Daughter extends Parent{ } class Son extends Parent { public void aFunc(){ ______________//Line 10 } } class Unrelated{ public void aFunc(){ _____________//Line 15 } } Question 1: Which variables of a,b and c are accessible at Line 10 and why? Question 2: Which variables of a,b and c are accessible at Line 15 and why?

  18. Protected Access

  19. Package Access

  20. Summary. • Inheritance allows a programmer to save work by using pre-defined classes as a starting point for new classes. • In Java, you implement inheritance by using the extends keyword when specifying a new class. • Parent constructors are always called before child constructors. • There are strict rules on scoping when using inheritance.

More Related