package org.megha.blog.example.part7; public class Cat { public String whoAmI() { return "I am a cat."; } } package org.megha.blog.example.part7; public class Tiger extends Cat { // a tiger is also a cat :) @Override public String whoAmI() { return "I am a tiger."; } } package org.megha.blog.example.part7; public class Main { public static void main(String args[]) { Cat cat1 = new Cat(); Tiger tiger1 = new Tiger(); Cat cat2 = tiger1; System.out.println(cat1.whoAmI()); // prints "I am a cat." System.out.println(tiger1.whoAmI()); // prints "I am a tiger." System.out.println(cat2.whoAmI()); // prints "I am a tiger." } }
Showing posts with label method. Show all posts
Showing posts with label method. Show all posts
Thursday, September 15, 2011
Overriding Methods
A derived class (Tiger) can override methods of a base class (Cat). The overridden methods are called even when a reference to an instance of the derived class (tiger1) is stored in a variable of the base class (cat2).
Abstract Classes
An abstract class for a three dimensional object (ThreeDObject) and two concrete classes (Sphere and Cylinder) derived from that abstract class.
The abstract class defines the method getSurfaceToVolumeRatio() which uses the abstract methods getSurfaceArea() and getVolume(). Any ThreeDObject derived class can have it's own mechanism to calculate surface area and volume.
package org.megha.blog.example.part6; /** * A 3-Dimensional object, which has a surface area, volume and * a surface area to volume ratio. This abstract class has a * concrete surface-area to volume ratio method implementation * which depends on the object specific surface-area and volume * definitions. */ public abstract class ThreeDObject { public abstract double getSurfaceArea(); public abstract double getVolume(); public double getSurfaceToVolumeRatio() { return getSurfaceArea()/getVolume(); } } package org.megha.blog.example.part6; /** A sphere with a volume and surface area */ public class Sphere extends ThreeDObject { double radius; public Sphere(double radius) { this.radius = radius; } @Override public double getSurfaceArea() { return (4 * Math.PI * radius * radius); } @Override public double getVolume() { // note that doing 4 / 3 * Math.PI * radius * radius * radius // yields a differnt result as // 4 / 3 (integer division) equals 1 return (4 * Math.PI * radius * radius * radius) / 3; } @Override public String toString() { return "sphere radius(" + radius + ")"; } } package org.megha.blog.example.part6; /** A Cylinder 3-D object */ public class Cylinder extends ThreeDObject { private double height; private double radius; public Cylinder(double height, double radius) { this.height = height; this.radius = radius; } @Override public double getSurfaceArea() { return 2 * Math.PI * radius * (radius + height); } @Override public double getVolume() { return Math.PI * radius * radius * height; } @Override public String toString() { return "cylinder radius(" + radius + "), height(" + height + ")"; } } package org.megha.blog.example.part6; /** Creates some ThreeDObjects and prints their details */ public class Main { private static void printSurfaceAreaRatio(ThreeDObject obj) { System.out.println("------------------"); System.out.println("for object " + obj); System.out.println("surface area " + obj.getSurfaceArea()); System.out.println("volume " + obj.getVolume()); System.out.println("surface area to volume ratio " + obj.getSurfaceToVolumeRatio()); } public static void main(String args[]) { printSurfaceAreaRatio(new Sphere(1)); printSurfaceAreaRatio(new Sphere(2)); printSurfaceAreaRatio(new Cylinder(1, 1)); printSurfaceAreaRatio(new Cylinder(1, 2)); printSurfaceAreaRatio(new Cylinder(2, 1)); printSurfaceAreaRatio(new Cylinder(2, 2)); } }
Instantiation
This program shows how you create an object/instance of a class, and call methods in the created object.
package org.megha.blog.example.part2; /** * A Circle that can calculate it's area and perimeter. */ public class Circle { private double radius; /** * Constructor to create a circle of a given radius. * Note: how you cannot create a Circle without specifying it's radius! */ public Circle(double radius) { this.radius = radius; } /** @return the radius of the circle */ public double getRadius() { return radius; } /** @return the area of a circle (PI*r*r) */ public double getArea() { double area = Math.PI*radius*radius; return area; } /** @return the perimeter of the circle (2*PI*r) */ public double getPerimeter() { double perimeter = 2 * Math.PI * radius; return perimeter; } /** prints the radius, area and perimeter of this circle */ public void print() { System.out.println("Circle, with radius " + getRadius()); System.out.println("Area = " + getArea()); System.out.println("Perimeter = " + getPerimeter()); } public static void main(String args[]) { // create a circle of radius 10 Circle circle1 = new Circle(10); // print information about the circle we just created circle1.print(); } }
Labels:
beginner,
class,
constructor,
instantiation,
java,
method
Subscribe to:
Posts (Atom)