The document discusses various object-oriented programming concepts in Java including method overloading, constructor overloading, passing objects as parameters, argument passing, and returning objects from methods. It provides examples of each concept and explains that method overloading is implemented at compile-time using the method signature, while overriding is implemented at run-time using dynamic method dispatch. Objects are passed by reference in Java so changes to the object inside a method affect the original argument.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
38 views22 pages
6-A Closer Look at Classes and Methods-1
The document discusses various object-oriented programming concepts in Java including method overloading, constructor overloading, passing objects as parameters, argument passing, and returning objects from methods. It provides examples of each concept and explains that method overloading is implemented at compile-time using the method signature, while overriding is implemented at run-time using dynamic method dispatch. Objects are passed by reference in Java so changes to the object inside a method affect the original argument.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22
A Closer Look at
Methods and Classes
• Overloading Methods • Overloading Constructors • Objects as Parameters • Argument Passing • Returning Objects Overloading Methods • If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. • Method overloading is one of the ways that Java implements polymorphism. • Advantage of method overloading • Method overloading increases the readability of the program. • Different ways to overload the method 1. By changing number of arguments 2. By changing the data type
In Java, Method Overloading is not possible by changing
the return type of the method only. • When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. • Thus, overloaded methods must differ in the type and/or number of their parameters. • While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. • When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); } } O/p-> No parameters a: 10 a and b: 10 20 double a: 123.25 Result of ob.test(123.25): 15190.5625 Automatic type conversion and Overloading
• When an overloaded method is called, Java
looks for a match between the arguments used to call the method and the method’s parameters. However, this match need not always be exact. In some cases Java’s automatic type conversions can play a role in overload resolution. class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); int i = 88; ob.test(); ob.test(10, 20); ob.test(i); // this will invoke test(double) ob.test(123.2); // this will invoke test(double) } } This program generates the following output: No parameters a and b: 10 20 Inside test(double) a: 88.0 Inside test(double) a: 123.2 // Automatic type conversions apply to overloading. class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter void test(double a) { System.out.println("Inside test(double) a: " + a); } } • This version of OverloadDemo does not define test(int). Therefore, when test( ) is called with an integer argument inside Overload, no matching method is found. • But, Java can automatically convert an integer into a double, and this conversion can be used to resolve the call. • Therefore, after test(int) is not found, Java elevates i to double and then calls test(double). • Java will employ its automatic type conversions only if no exact match is found. [*Complied time polymorphism is implemented by method overloading. Run-time polymorphism is implemented by Method overriding.(dynamic method dispatch: a call to an overridden method is resolved at run time, rather than compile time.)] • Consider the absolute value function. In languages that do not support overloading, there are usually three or more versions of this function, each with a slightly different name. • For instance, in C, the function abs( ) returns the absolute value of an integer, labs( ) returns the absolute value of a long integer, and fabs( ) returns the absolute value of a floating point value. Since C does not support overloading, each function has to have its own name, even though all three functions do essentially the same thing and the underlying concept of each function is the same. • This situation does not occur in Java, because each absolute value method can use the same name. In java abs() method is overloaded by Java’s Math class to handle all numeric types. Java determines which version of abs( ) to call based upon the type of argument. Overloading Constructors class Box { double width, height, depth; // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; } } class OverloadCons { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); double vol; vol = mybox1.volume(); // get volume of first box System.out.println("Volume of mybox1 is " + vol); vol = mybox2.volume(); // get volume of second box System.out.println("Volume of mybox2 is " + vol); vol = mycube.volume(); // get volume of cube System.out.println("Volume of mycube is " + vol); } } The output produced by this program is shown here: Volume of mybox1 is 3000.0 Volume of mybox2 is -1.0 Volume of mycube is 343.0 class Demo { public void show(int x) { System.out.println("In int" + x); } public void show(String s) { System.out.println("In String" + s); } public void show(byte b) { System.out.println("In byte" + b); } } class UseDemo { public static void main(String[] args) { byte a = 25; Demo obj = new Demo(); obj.show(a); // String obj.show("hello"); // Int obj.show(250); obj.show('A'); // String obj.show("A"); obj.show(7.5); } } Objects as Parameters
// Objects may be passed to methods.
class Test { int a, b; Test(int i, int j) { a = i; b = j; } // return true if o is equal to the invoking object boolean equals(Test o) { if(o.a == a && o.b == b) return true; else return false; } } class PassOb { public static void main(String args[]) { Test ob1 = new Test(100, 22); Test ob2 = new Test(100, 22); Test ob3 = new Test(-1, -1); System.out.println("ob1 == ob2: " + ob1.equals(ob2)); System.out.println("ob1 == ob3: " + ob1.equals(ob3)); } } This program generates the following output: ob1 == ob2: true ob1 == ob3: false • One of the most common uses of object parameters involves constructors. If we want to construct a new object which is initially as same as some existing object, then we must define a constructor that takes an object of its class as a parameter. class Box { double width; double height; double depth; // construct clone of an object Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; } Box(double w, double h, double d) { width = w; height = h; depth = d; } double volume() { return width * height * depth; } } class OverloadCons2 { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box myclone = new Box(mybox1); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of clone vol = myclone.volume(); System.out.println("Volume of myclone is " + vol); } } O/p-> Volume of mybox1 is 3000 Volume of myclone is 3000 Argument Passing • In Java, when you pass a simple type to a method, it is passed by value. Thus, what occurs to the parameter that receives the argument has no effect outside the method. (call by value). • Objects are passed by reference. Changes to the object inside the method do affect the object used as an argument.(call by reference). Returning Objects • A method can return any type of data, including class types that you create. // Returning an object. class Test { int a; Test(int i) { a = i; } Test incrByTen() { //return type is ‘class instance’ of class Test Test temp = new Test(a+10); return temp; } } class RetOb { public static void main(String args[]) { Test ob1 = new Test(2); Test ob2; ob2 = ob1.incrByTen(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob2.a: " + ob2.a); ob2 = ob2.incrByTen(); System.out.println("ob2.a after second increase: " + ob2.a); } } The output generated by this program is shown here: ob1.a: 2 ob2.a: 12 ob2.a after second increase: 22