Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.

Similar presentations


Presentation on theme: "Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have."— Presentation transcript:

1 Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have written before

2 Using a class similar to one already written in text “Sphere” page 193 Sphere has radius and position (actually draws an Oval.) protected will be used for inheritance Class “Bubble” page 194 will “extend” Sphere. Bubble inherits from Sphere. Bubble is a sub class of Sphere “protected” less private than private –More private than public

3 private int x, y; // x and y can not be used by another class protected int x, y; // can be used by another subclass

4 Scope rules: access public – from anywhere protected – from the class and sub classes private – only from the current class – local variables only inside a particular method

5 Overriding/Overloading Overriding: same name and parameters Overloading: method with same name as class but different parameters Subclass may add additional methods, variables and override methods of it’s parent

6 Class Diagrams: show which classes inherit from others Object Bubble Ball A sub class

7 Java uses single line inheritance Any public or protected method may be used by any sub class if the child “extrended” the parent class “super” – used by subclass to call it’s immediate superclass super.display(paper); // uses super class method “display” and not the method in the child class (sub class) “display”

8 constructors Use “new” A class with no constructors – Java assumes a single one with zero parameters A class with one or more constructors that require zero parameter constructor must be explicitly written. (Java does not assume it) Constuctors not inherited – must be explicitly called by sub class.

9 “Balloon” class page 199 Holds a constructor “Balloon” which is passed through parameters “DifferentBalloon” page 200 – holds a constructor passed in two parameters – super( ) is called from the immediate super class. “ModifiedBalloon” – holds a constructor, passed three parameters –Super is called with three parameters

10 final: Methods: declared final can not be overridden Variables: declared final can not be changed Classes: declared final can not be sub classed

11 Abstract classes A program that maintains graphical shapes – they are similar (position, color, size) Super class “Shape” Page 202 – public abstract class Shape { … – public class Circle extends Shape { … paper.drawOval ( x, y, size, size);

12 Shape is incomplete The keyword ‘abstract’ used in Shape prevents attempts to create an instance of the class Shape display method in Shape is just a header the subclass must implement it as ‘display’ in Circle does. Any class that includes methods that are abstract must also be abstract Abstract classes higher up the class hierarchy become more general or abstract


Download ppt "Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have."

Similar presentations


Ads by Google