Method Overloading Overriding Presented by S Aravind Overloading
Method Overloading & Overriding Presented by S. Aravind
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
Class Hierarchies b A child class of one parent can be the parent of another child, forming class hierarchies Staff. Member Volunteer Employee Executive Hourly 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
Indirect Access II class Food. Item { final private int CALORIES_PER_GRAM = 9; private int fat. Grams; protected int servings; public Food. Item (int fat. Grams, int servings) { this. fat. Grams = fat. Grams; this. servings = servings; } private int calories () { return fat. Grams * CALORIES_PER_GRAM; } public int calories. Per. Serving () { return (calories() / servings); } } 5
Indirect Access III class Pizza extends Food. Item { public Pizza (int fat. Grams) { super (fat. Grams, 8); } } b The following code works: Pizza special = new Pizza (275); System. out. println ("Calories per serving: " + special. calories. Per. Serving()); 6
Inheritance Example class Point { public Point( int x, int y) { set. Point(x, y); } public Point() { this(0, 0); } public void set. Point( int x, int y) { this. x = x; this. y = y; } public String to. String() { return “(“ + x + “, ” + y + “)”; } public int get. X() {return x; } // get x coordinate public int get. Y() { 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); set. Radius( r ); } public Circle() { // done automatically anyway this(0, 0, 0); } public void set. Radius( double r ) { radius = ( r >= 0 ? r : 0 ); } public double get. Radius() { return radius; } public double get. Area() { return Math. PI* radius; } public String to. String() { return “(“ + super. to. String() + “, ” + 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 set. Height( h ); } public void set. Height( double h ) { height = h > 0 ? h : 0; } public double get. Height() { return height; } public double get. Area() { return 2 * super. area() + 2 * Math. PI * get. Radius() * height; } public double get. Volume() { return super. area() * height; } public String to. String() { return “(“ + super. to. String() + “, ” + height + “)”; } } 9
The class hierarchy: Point Pixel Circle Cylinder
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 b To protect some of your class's methods from being overridden declare them final. - synchronization in java. lang. Object - methods of java. io. Data. Input. Stream 11
Constructor Invocation b b The constructors are called either explicitly or implicitly from base-class to sub-class down the inheritance hierarchy. 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 No. Inheritence { public No. Inheritence() { System. out. println("No Inheritance. "); } } public class One. Inheritence extends No. Inheritence { public One. Inheritence() { System. out. println("One Inheritance. "); } } public class Two. Inheritence extends One. Inheritence { public Two. Inheritence() { System. out. println("Two Inheritance. "); } public static void main(String args[]) { Two. Inheritence ti = new Two. Inheritence(); } } 13
- Slides: 13