Understanding Dynamic Method Dispatch

Sphere.java

package test3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
 *
 * @author taher
 */
public class Sphere {

    final static float PI = 3.14f;
    int r=0;

    public void get() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        r= Integer.parseInt(br.readLine());
    }

    public float volume(){
        return (4*PI*r*r*r)/3;
    }
}

HemiSphere.java

package test3;

import java.io.IOException;

/**
 *
 * @author taher
 */
public class HemiSphere extends Sphere{

    public HemiSphere() {
        super();
    }

    @Override
    public float volume(){
        return (2*PI*r*r*r)/3;
    }

    public static void main(String[] args) throws IOException {
        Sphere sphere = new HemiSphere();
        sphere.get();
        System.out.println(sphere.volume());
    }
}
Advertisements
This entry was posted in Core JAVA. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s