+4 votes
61 views
i know the purpose of function overloading and overriding .i want to know at what situation function overloading and function overriding needed.explain with real time example is better to understand.thank you
asked Jun 1, 2016 in Computer Science - IT by gypsiii | 61 views
good question +1

1 Answer

+1 vote
Selected Ans.

Good question !..I assume you already know what is function overloading and overriding , still let me show two simple real time examples on both , Have a clear look :

Program 1 :

public class Main {
    
    public static void searchstudent(String studentname)
    {
System.out.println("student found ");
}

        public static void searchstudent(int rollnumber,int mobilenumber)
    {
System.out.println("student found");
}

   public static void main( String args[]) {

       searchstudent("raina");
       searchstudent(1,88447755);

   }
}

 

Program 2:

class Game {
   static void players() {
      System.out.println("2 players");
   }
}

class cricket extends Game {
   static void players() {
      System.out.println("11 players");
   }
}

class rugby extends Game {
   static void players() {
      System.out.println("15 players");
   }
}

public class NewClass {
   public static void main(String args[]) {
      rugby game = new rugby();
      game.players(); 
   }
}

 

You may be thinking both are similar in some case , i.e name is repeating ..Yes ,but have a look at parameters being passed !

Program 1 has function name same but parameters different and operation too different , you can search a student by roll number,mobile number or name ,hence i have created neatly only 2 functions with same name not to get untidy or lengthy if i write :

  • searchstudentname()
  • searchstudentmobile()
  • searchstudentroll()

There comes our real time use of function overloading to search a student in school management system 

Next , Program 2 same function name , same number of parameters but they override or overshadow the same function in parent class , right ? If you dont have players function in rugby still the default 2 players will be shown

If function is same then we can inherit or if u have different purpose as above just override them !

If you have comments , welcome :)

answered Jun 2, 2016 by kevin
selected Jun 2, 2016 by gypsiii
ya thank you for a simple answer to understand. i have one query about overriding example.

-> how we can call the player() function in cricket class using rugby object.
is that another question related to your question ? then please post as separate one
you can override cricket player() with main class player() by :
      Game game = new rugby();
      game.players();
Thank you,
but

Game game=new rugby();
game.players();

it will call the players() method in Game class.
it show the result is "2 players"..

i want to call the players() method in cricket class using derived class.
class Game {
   static void players() {
      System.out.println("2 players");
   }
}

 class cricket extends Game {
   static void players() {
      System.out.println("11 players");
   }
}

class rugby extends cricket {
   static void players() {
      System.out.println("15 players");
   }
}

public class NewClass {
   public static void main(String args[]) {
      cricket game = new rugby();
      game.players();
   }
}
good explanation