Java Program For Method Overloading and Overriding

Get Job-ready: Java Course with 45+ 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;
    }
}

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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