Java Program For Method Overloading and Overriding

Free Java courses with 37 real-time projects - Learn Java

Program 1

package pack1;

public class MyClass1 
{
     double area(double r)
     {
         double A;
         A=3.14*r*r;
         return A;
     }
      double area(double l,double b)
     {
         double A;
         A=l*b;
         return A;
     }  
}

Program 2

package pack1;


public class TestMain 
{
     public static void main(String[] args) 
     {
         MyClass1 M=new MyClass1();
         //System.out.println("Area of Circle: "+M.area(12.34));
         System.out.println("Area of Rectangle: "+M.area(12.34,2.4));
     }
    
}

Program 3

package pack3;


public class SecondClass extends FirstClass
{
   void display(int a)
   {
       System.out.println("This is display method of class Second");
   }
   
    public static void main(String[] args) 
    {
         SecondClass S=new SecondClass();
         S.display(123);
                 
    }
   
}

Program 4

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package pack3;

/**
 *
 * @author admin
 */
public class TestMain {
    public static void main(String[] args) 
    {
       FirstClass F=new FirstClass(200);
       String S=new String("WElcome");
        System.out.println(F);
    }
}

Program 5

package pack3;


public class FirstClass 
{
    private int a;
    FirstClass(int a)
    {
        this.a=a;
    }
    FirstClass()
    {}
    void display()
   {
       System.out.println("This is display method of class First");
   }
    //override
    public String toString()
    {
        return ""+a;
    }
}

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *