Class 10 ICSE - Java Polymorphism Programs
Class 10th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 10th java topics includes revision of class 9th, constructors, user-defined methods, objects and classes, library classes , etc.
Program 1: Write a program using method overloading to add two numbers by changing the type of the parameters.
Solution:
class Sum { public int add(int a, int b) { return a+b; } public double add(double a, double b) { return a+b; } public static void main(String[] args) { Sum obj = new Sum(); System.out.println(obj.add(17,13)); System.out.println(obj.add(10.4,10.6)); } }
In the above example, we have created two methods names add(). These methods takes two parameters a
and b.
public int add(int a, int b)
public double add(double a, double b)
Program 2: Write a program using method overloading to multiply numbers by changing the no. of parameters
Solution:
class Multi { public int multiply(int a,int b) //method takes two parameters { return a*b; } public int multiply(int a,int b,int c) //method takes three parameters { return a*b*c; } public static void main(String[] args) { Multi obj = new Multi(); System.out.println(obj.multiply(110,110));//invoking method with two parameters System.out.println(obj.multiply(110,110,110));//invoking method with three parameters } }
In the above example, we have created two methods named multiply(). These method takes parameters .
obj.multiply(110,110)
obj.multiply(110,110,110)
Program 3: Write a program to overload display method by changing the type of parameters.
Solution:
class MethodOverloading { // this method accepts int void display(int a){ System.out.println("Got Integer data." + a); } // this method accepts String object void display(String a){ System.out.println("Got String data." + a); } public static void main(String[] args) { MethodOverloading obj = new MethodOverloading(); obj.display(1); obj.display("Hello"); } }
In the above example, we have created two method named display(). These method takes single
parameter.
Program 4: Write a program to display two messages using method overloading, one with no parameter and other with a parameter.
Solution:
class Main { // method with no parameter public void display1() { System.out.println("Method with no parameter"); } // method with single parameter public void display1(int n) { System.out.println("Method with a parameter: " + n); } public static void main(String[] args) { // create an object Main obj = new Main(); // calling method obj.display1(); // calling method obj.display1(24); } }
In the above example, we have created two methods named display1() . We are creating object of the class and then calling both the methods and displaying the result accordingly
Program 5: Write a program to create two methods which takes two parameters and overload them by changing the sequence of parameters .
Solution:
class Display { //method overloading by changing Sequence of data type of arguments public void disp(char c, int num)// first with character input and other integer { System.out.println("one character with integer input" + c +" " + num); } public void disp(int num, char c) // first integer and other character { System.out.println("one integer and character input" + num + " " + c ); } public static void main(String args[]) { Display obj = new Display(); obj.disp('x', 51 ); obj.disp(52, 'y'); } }