Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.

Similar presentations


Presentation on theme: "1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with."— Presentation transcript:

1 1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with the same name but different signatures b Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature b Overloading lets you define a similar operation in different ways for different data b Overriding lets you define a similar operation in different ways for different object types

2 2 Class Hierarchies b A child class of one parent can be the parent of another child, forming class hierarchies StaffMember VolunteerEmployee ExecutiveHourly

3 3 Indirect Access I b An inherited member can be referenced directly by name in the child class, as if it were declared in the child class b But even if a method or variable is not inherited by a child, it can still be accessed indirectly through parent methods

4 4 Indirect Access II class FoodItem { final private int CALORIES_PER_GRAM = 9; private int fatGrams; protected int servings; public FoodItem (int fatGrams, int servings) { this.fatGrams = fatGrams; this.servings = servings; } private int calories () { return fatGrams * CALORIES_PER_GRAM; } public int caloriesPerServing () { return (calories() / servings); }

5 5 Indirect Access III class Pizza extends FoodItem { public Pizza (int fatGrams) { super (fatGrams, 8); } b The following code works: Pizza special = new Pizza (275); System.out.println ("Calories per serving: " + special.caloriesPerServing());

6 6 Inheritance Example class Point { public Point( int x, int y) { setPoint(x, y); } public Point() { this(0, 0); } public void setPoint( int x, int y) { this.x = x; this.y = y; } public String toString() { return “(“ + x + “,” + y + “)”; } public int getX() {return x; } // get x coordinate public int getY() { return y; } // get y coordinate protected int x,y; // accessible by derived classes }

7 7 class Circle extends Point { private double radius; public Circle(int x, int y, double r) { super(x, y); setRadius( r ); } public Circle() { // done automatically anyway this(0, 0, 0); } public void setRadius( double r ) { radius = ( r >= 0 ? r : 0 ); } public double getRadius() { return radius; } public double getArea() { return Math.PI* radius * radius; } public String toString() { return “(“ + super.toString() + “,” + radius + “)”; } Example (cntnd.)

8 8 class Cylinder extends Circle { private double height; public Cylinder(double h, double r, int x, int y ) { super( r, x, y ) // call base-class constructor setHeight( h ); } public void setHeight( double h ) { height = h > 0 ? h : 0; } public double getHeight() { return height; } public double getArea() { return 2 * super.area() + 2 * Math.PI * getRadius() * height; } public double getVolume() { return super.area() * height; } public String toString() { return “(“ + super.toString() + “,” + height + “)”; }

9 The class hierarchy: Point PixelCircle Cylinder

10 10 Final classes and final methods b Final class, why? - increase system security: disables creating a subclass of a class and then substituting for the original one - good object-oriented design: your class should have no subclasses  To protect some of your class's methods from being overridden declare them final. - synchronization in java.lang.Object - methods of java.io. DataInputStream

11 11 Constructor Invocation b b The constructors are called either explicitly or implicitly from base-class to sub-class down the inheritance hierarchy. b The compiler forces invocation of base-class constructor as the first thing that happens in the sub-class constructor, this means you cannot catch any exception thrown in the base-class's constructor.

12 12 Inheritance hierarchy public class NoInheritence { public NoInheritence() { System.out.println("No Inheritance."); } public class OneInheritence extends NoInheritence { public OneInheritence() { System.out.println("One Inheritance."); } public class TwoInheritence extends OneInheritence { public TwoInheritence() { System.out.println("Two Inheritance."); } public static void main(String args[]) { TwoInheritence ti = new TwoInheritence(); }


Download ppt "1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with."

Similar presentations


Ads by Google