Program illustrating Method Overloading and Method Overriding in Java.

 Program illustrating Method Overloading and Method Overriding in Java.



class Shape

{

 void display() 

 {

   System.out.println("This is a shape.");

 }

 void draw() 

 {

   System.out.println("Drawing a shape.");

 }

}

class Circle extends Shape 

{

    //Override

 void display() 

 {

   System.out.println("This is a circle.");

 }

 void draw(int radius) 

 {

   System.out.println("Drawing a circle with radius: " + radius);

 }

}

class Test 

{

 public static void main(String[] args) 

 {

  // Method Overriding     

  Shape shape = new Circle();

  shape.display();  // Calls the overridden method in the Circle class


  // Method Overloading

  Circle circle = new Circle();

  circle.draw();    // Calls the method from the base class

  circle.draw(5);   // Calls the overloaded method in the Circle class

 }

}

Output




Post a Comment

0 Comments