Ex 10

  • Uploaded by: premsrikanth
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ex 10 as PDF for free.

More details

  • Words: 689
  • Pages: 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



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

1

Class Hierarchies ❂

A child class of one parent can be the parent of another child, forming class hierarchies StaffMember

Volunteer

Employee

Executive

Hourly 2

Indirect Access I



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

3

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); } }

4

Indirect Access III class Pizza extends FoodItem { public Pizza (int fatGrams) { super (fatGrams, 8); } } ❂

The following code works: Pizza special = new Pizza (275); System.out.println ("Calories per serving: " + special.caloriesPerServing());

5

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 }

6

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 + “)”; } }

7

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 + “)”; } }

8

The class hierarchy:

Point

Pixel

Circle

Cylinder

Final classes and final methods



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 10

Constructor Invocation ❂



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. 11

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(); } }

12

Related Documents

Ex 10
April 2020 17
Ex 10
October 2019 23
Ex 10
May 2020 9
An Ex A 10
May 2020 7
Ex
December 2019 40
Ex
April 2020 22

More Documents from ""

Ex 10
May 2020 9