| Overriding | |||||||||||
| Intro to Overriding | Using "super" | ||||||||||
| Overloading vs. Overriding | |||||||||||
| Examples of Overriding | |||||||||||
| Overloading Examples | Home | ||||||||||
| Example:
Public class Circle { //declaring the instance variable protected double radius; public Circle(double radius) { this.radius = radius; } // other method definitions here public double getArea() { return Math.PI*radius*radius; }//this method returns the area of the circle }// end of class circle public class Cylinder extends Circle { //declaring the instance variable protected double length; public Cylinder(double radius, double length) { super(radius); this.length = length; } // other method definitions here public double getArea() { // method overriden here return 2*super.getArea()+2*Math.PI*radius*length; }//this method returns the cylinder surface area }// end of class Cylinder Below is the code to instantiate the above two classes: Circle myCircle; myCircle = new Circle(1.20); Cylinder myCylinder; myCylinder = new Cylinder(1.20,2.50); |
|||||||||||