Topics

6/recent/ticker-posts

Types of classes part 5

 Types of classes part 5 continuation 



Enum: 

  • Enumeration purpose is to declare constant strings. For example to declare directions, colors, priority, risk levels etc..

enum Direction {
NORTH, EAST, SOUTH, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST;
}

enum Color {
RED, GREEN, BLUE, WHITE, YELLOW, ORANGE, BLACK;
}

enum Priority {
HIGH, MEDIUM, LOW;
}

A simple enum example where enum is declared outside any class (Note enum keyword instead of class keyword) 

enum Color { 
    RED, GREEN, BLUE; 
  
public class Test 
    // Driver method 
    public static void main(String[] args) 
    { 
        Color c1 = Color.RED; 
        System.out.println(c1); 
    } 
}

  Polymorphism (Many shapes of method)

  • One method with different signatures - method overloading - takes place in the same class.
  • One method with one signature having many implementations - method overriding - takes place between one super class and many sub classes.
  • Method overloading and Method overriding comes under polymorphism also called as static and dynamic polymorphism.
  • + Method overloading is static polymorphism.
  • + Method overriding is dynamic polymorphism.
 

Method overloading:

Writing same method many times with different method signature is called as overloading. Overloading considers number of parameters, type of parameters and order of parameters.

// OverloadingDemo.java
public class OverloadingDemo {
 
    public double calTax(double amt) {
   double taxOnAmt=0.0;
   taxOnAmt=amt*12.5/100;
   return taxOnAmt;
}
    public double calTax(double amt, String state) {
   double taxOnAmt=0.0;
   if(state.equals("AP") {
      taxOnAmt=amt*12.5/100;
      return taxOnAmt;
   } else if(state.equals("UP")) {
      taxOnAmt=amt*11.5/100;
      return taxOnAmt;
   } else {
      taxOnAmt=amt*10.5/100;
      return taxOnAmt;
   }
}

/*
public int add2Int(int i, int j) {}
public int addThreeInt(int i, int j, int k) {}
public int add_Four_Int(int i, int j, int k, int l) {}
public int add_5_Int(int i, int j, int k, int l, int m) {}
*/

public int add(int i, int j) {}
public int add(int i, int j, int k) {}
public int add(int i, int j, int k, int l) {}
public int add(int i, int j, int k, int l, int m) {}
/*
int arr[]={1,2,3,4,5,6,7,8,9};
add(arr);
*/
public int add(int i[]) {
int sum=0;
int len=i.length;
for(int a=0;a<len;a++) {
sum=sum+i[a];
}
return sum;
}

// add(1,2,3,4,5,6,7,8,9)
public int add(int... i) {
int sum=0;
// for-each loop
for(int a:i) {
sum+=a;
}
return sum;
}
public int add(String str, int... i) { // correct syntax
int sum=0;
// enhanced for loop
for(int j:i) {
sum+=j;
}
return j;
}
public int add(int i..., String str) { // wrong syntax
   int sum=0;
   // enhanced for loop
   for(int j:i) {
   sum+=j;
   }
   return j;
}

public int add(String str, int i[]) { // correct syntax
  int len=i.length;
  int sum=0;
  for(int j=0;j<len;j++) {
// sum=sum+i[j];
sum+=j;
  }
  return sum;
}
public int add(int[] i, String str) { // correct syntax
  int len=i.length;
  int sum=0;
  for(int j=0;j<len;j++) {
// sum=sum+i[j];
sum+=j;
  }
  return sum;
}

}
 
 If method overloading doesn't exist we need to write the same method with different names for different signatures.
 
  •  In contrast with the above example if we want to add two ints, three ints and so on, to add 20 or 30 ints we need to overload add() method for those many times which is not a practical solution. 
  • The solution is given in JDK 1.6 as var args (variable arguments). In var args in the method the parameters must be written as:
 
public int add(int[] i) { // int array, correct syntax
}
public int add(int i[]) { // int array, correct syntax
}
public int add(int... i) { // int var-args, correct syntax
}
public int add(int i...) { // wrong syntax
}
 
If different parameters are passed into method along with vararg parameter, vararg parameter should be the last parameter, after var_arg parameter no other parameter should not be passed.

public int add(String str, int... i) { // correct syntax
   int sum=0;
   // enhanced for loop
   for(int j:i) {
   sum+=j;
   }
   return j;
}
public int add(int i..., String str) { // wrong syntax
   int sum=0;
   // enhanced for loop
   for(int j:i) {
   sum+=j;
   }
   return j;
}

public int add(String str, int i[]) { // correct syntax
  int len=i.length;
  int sum=0;
  for(int j=0;j<len;j++) {
// sum=sum+i[j];
sum+=j;
  }
  return sum;
}
public int add(int i[], String str) { // correct syntax
  int len=i.length;
  int sum=0;
  for(int j=0;j<len;j++) {
// sum=sum+i[j];
sum+=j;
  }
  return sum;
}

  • Note: In a java program main() method can be overloaded, but there is no concept of overriding main() method. main() method must be written with String[] as parameter or String... rags

Post a Comment

1 Comments