1 / 12

Overloading vs. Overriding

Don't confuse the concepts of overloading and overriding Overloading deals with multiple methods in the same class with the same name but different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature

rigg
Télécharger la présentation

Overloading vs. Overriding

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

  2. A child class of one parent can be the parent of another child, forming class hierarchies StaffMember Volunteer Employee Executive Hourly Class Hierarchies

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

  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. The following code works: Indirect Access III class Pizza extends FoodItem { public Pizza (int fatGrams) { super (fatGrams, 8); } } Pizza special = new Pizza (275); System.out.println ("Calories per serving: " + special.caloriesPerServing());

  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. Example (cntnd.) 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 + “)”; } }

  8. Example (cntnd.) 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 Pixel Circle Cylinder

  10. 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 Final classes and final methods

  11. The constructors are called either explicitly or implicitly from base-class to sub-class down the inheritance hierarchy. Constructor Invocation • 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. 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(); } }

More Related