Speed up development with full-stack environments for every branch.

Learn More

Java Polymorphism Example

604 Runs 2093 Views 538 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

/*
* Java Polymorphism Example
*/

//a basic class Box
class Box
{
  int w,h;
	void info()
	{
		System.out.println("This is a simple box");
	}	
}
 
//create a new class that extends the base Box class
class WoddenBox extends Box
{
  //each "child" can have it's own attributes
	int life;
	void info( )
    {
		System.out.println("This is a Wodden box");
	}
}

//create a new class that extends the base Box class
class SteelBox extends Box
{
	int wg;
	void info( )
	{
		System.out.println("This is a steel box");
	}
}

//create a new class that extends the base Box class
class LargeWoddenBox extends WoddenBox
{
    void info()
    {
        System.out.println("This is a Huge Wodden box");
    }
}

class HelloWorld
{
    public static void main ( String ary[ ] )
    {
		Box x;
    Box b1 =new Box( );
    WoddenBox wb=new WoddenBox( );
    SteelBox s1=new SteelBox( );
    LargeWoddenBox p1=new LargeWoddenBox( );
    b1.info( );
    wb.info( );
    s1.info( );
    p1.info( );
    }
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).