Thursday, July 8, 2010

Another Java Inheritance Sample

 Posted in a previous post, another illustration of inheritance is the program herein with methods that return values to their calling program.

[+/-] show/hide

import javax.swing.*;
public class SampleInheritance2{
public static void main (String args[]){
String numstring1=JOptionPane.showInputDialog("Input a number");
int mynum1=Integer.parseInt(numstring1);

Parent2 obj1=new Parent2();

System.out.println("Your input from parent : "+obj1.num);
System.out.println("Square: "+obj1.square());
System.out.println("Cube: "+obj1.cube());

String numstring2=JOptionPane.showInputDialog("Input a number");
int mynum2=Integer.parseInt(numstring2);

Child2 obj2=new Child2();

System.out.println("Your input from child class: "+obj2.getnum());
System.out.println("Square: "+obj2.square());
System.out.println("Cube: "+obj2.cube());
System.out.println("Nth from child class: "+obj2.nth());



class Parent2{
int num;
void setnum(int x)

int square()
{int mysquare=num*num;
return mysquare;

int cube()
{int mycube =num*num*num;
return mycube;

int getnum()
{return num;}

class Child2 extends Parent2{
double nth()
{ System.out.println(" last value of num "+num);
double myquo=num/2.0;
return myquo;}


No comments: