CSC/ECE 517 Fall 2007/wiki2 8 c9: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 25: Line 25:
  void show() {
  void show() {


   System.out.println(There are no parameters in this method”);
   System.out.println(" There are no parameters in this method");
  }
  }


void show(int a) {
void show(int a) {
System.out.println(“this has one parameter : a” +a);
System.out.println("this has one parameter : a" +a);
}
}


Void show(int a,int b){
void show(int a,int b){
System.out.println(“this has 2 parameters a”+a+ “b:+b);
System.out.println("this has 2 parameters a"+a+ "b:"+b);
}
}


Double show(double a){
double show(double a){
System.out.println(“this has a double parameter  a”+a);
System.out.println("this has a double parameter  a"+a);
Return a*a
return a*a
}
}
}
}


Class overloadtest {
class overloadtest {
Public static void main(String args[]){
public static void main(String args[]){
Overload obj=new Overload();
Overload obj=new Overload();
Double result;
double result;


Obj.show();
obj.show();
Obj.show(10);
obj.show(10);
Obj.show(10,20);
obj.show(10,20);
Result=obj.show(100.5);
result=obj.show(100.5);
System.out.println(“result of obj.show(100.5) is” + result);
System.out.println("result of obj.show(100.5) is" + result);
}
}
}
}
Line 79: Line 79:


<pre>
<pre>
Class Figure {
class Figure {
Double length;
double length;
Double breadth;
double breadth;


Figure(double a, double b)
Figure(double a, double b)
{
{
Length =a;
length =a;
Breadth=b;
breadth=b;
}
}


Double area(){
double area(){
System.out.println(“area for figure is undefined”);
System.out.println(“area for figure is undefined”);
Return 0;
return 0;
}
}
}
}


Class rectangle extends Figure {
class Rectangle extends Figure {
Rectangle (Double a, double b) {
Rectangle (Double a, double b) {
Super(a,b);
super(a,b);
}
}


// override the area for rectangle
// override the area for rectangle
Double area()   
double area()   
{
{
System.out.println(“inside area for rectangle”);
System.out.println(“inside area for rectangle”);
Return length* breadth
return length* breadth
}
}
}
}




Class triangle extends figure{
class Triangle extends Figure{
Triangle (double a,double b)
Triangle (double a,double b)
{
{
Super(a,b);
super(a,b);
}
}
// override area for triangle
// override area for triangle
Double area() {
double area() {
System.out.println(“inside area of triangle”);
System.out.println(“inside area of triangle”);
Return length*breadth /2;
return length*breadth /2;
}
}
}
}




Class areastest{
class areastest{
Public static void main(String args[]){
public static void main(String args[]){
Figure obf=new figure(10,10);
Figure obf=new figure(10,10);
Rectangle obr=new rectangle(25,50);
Rectangle obr=new rectangle(25,50);
Line 129: Line 129:


Figure ref;
Figure ref;
Ref=obr;
ref=obr;
System.out.println(“area is”+ ref.area());
System.out.println(“area is”+ ref.area());


Ref=obt;
ref=obt;
System.out.println(“area is”+ ref.area());
System.out.println(“area is”+ ref.area());


Ref=obf;
ref=obf;
System.out.println(“area is”+ ref.area());
System.out.println(“area is”+ ref.area());
}
}

Revision as of 23:51, 21 October 2007

Topic 8 : Overloading Vs Overriding

Overloading vs. overriding. Skrien delineates the difference between overloading and overriding in Section 2.8. He does this in the context of Java. How do other languages make this distinction, especially those like CLOS that define multi-methods?


Polymorphism

Polymorphism is usually expressed by the phrase “One interface, multiple methods”. This allows a super class to specify methods that will be common to all of its subclasses, while allowing the subclasses to define their own specific methods.It means design a generic interface and use it for group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler’s job to select the specific action (that is, method ) as it applies to each situation.

Overloading

In Java it is possible to define 2 or more methods within the same class that share the same name, as long as their parameter declarations are different. The methods are said to be overloaded. This process is referred as method overloading. It is one of the way by which java implements polymorphism.Java uses the type and /or number of arguments to determine which of the overloaded methods to be overloaded. The overloaded methods may differ in return type or the number of parameters in the method call.

Example


class Overload{
 void show() {

   System.out.println(" There are no parameters in this method");
 }

void show(int a) {
System.out.println("this has one parameter : a" +a);
}

void show(int a,int b){
System.out.println("this has 2 parameters a"+a+ "b:"+b);
}

double show(double a){
System.out.println("this has a double parameter  a"+a);
return a*a
}
}

class overloadtest {
public static void main(String args[]){
Overload obj=new Overload();
double result;

obj.show();
obj.show(10);
obj.show(10,20);
result=obj.show(100.5);
System.out.println("result of obj.show(100.5) is" + result);
}
}


Output


There are no parameters in this method
this has one parameter : a 10
this has 2 parameters a 10 b 20
this has a double parameter a 100.5
the result of obj.show(100.5) is 10100.25


Overriding

Overriding occurs only with respect to inheritance and occurs when a subclass has a method with the same signature as a parent's method. The method in the subclass is said to override the method in the superclass. Java always invokes the most derived viewable version of a method based on the scope of the present reference. However , if we wish to access superclass version of overridden method, then we can use the keyword “super” and then access it.


Example

class Figure {
double length;
double breadth;

Figure(double a, double b)
{
length =a;
breadth=b;
}

double area(){
System.out.println(“area for figure is undefined”);
return 0;
}
}

class Rectangle extends Figure {
Rectangle (Double a, double b) {
super(a,b);
}

// override the area for rectangle
double area()  
{
System.out.println(“inside area for rectangle”);
return length* breadth
}
}


class Triangle extends Figure{
Triangle (double a,double b)
{
super(a,b);
}
// override area for triangle
double area() {
System.out.println(“inside area of triangle”);
return length*breadth /2;
}
}


class areastest{
public static void main(String args[]){
Figure obf=new figure(10,10);
Rectangle obr=new rectangle(25,50);
Triangle obt=new triangle(20,40);

Figure ref;
ref=obr;
System.out.println(“area is”+ ref.area());

ref=obt;
System.out.println(“area is”+ ref.area());

ref=obf;
System.out.println(“area is”+ ref.area());
}
}

Output

Inside area for rectangle
Area is 1250
Inside area of triangle
Area is 400
Area for figure is undefined
Area is 0


Differences between Overloading and Overriding


Overloaded Overridden
supplements each other replaces the method it overrides
can exist in any number within same class Each method in a parent class can be overridden at most once in any one class
must have different arguments must have argument list of identical type and order
return type may be freely chosen return type must be identical
determined at compile time determined at run time


Note: Overloaded methods should be used sparingly, as they can make code much less readable.