130 likes | 296 Views
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
E N D
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
A child class of one parent can be the parent of another child, forming class hierarchies StaffMember Volunteer Employee Executive Hourly Class Hierarchies
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
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); } }
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());
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 }
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 + “)”; } }
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 + “)”; } }
The class hierarchy: Point Pixel Circle Cylinder
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
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.
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(); } }