Quantcast
Channel: Oracle Fusion Apps | Oracle Fusion | Oracle Apps Training
Viewing all articles
Browse latest Browse all 103

Overloading, Inheritance and Polymorphism in JAVA

$
0
0

Objective:

In the previous article Arrays and string in JAVA, we learned about the arrays, Strings and different String functions that are used in Java programming. In this article we will learn about the Overloading, Inheritance and Polymorphesim in Java.

Overloading:

Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. In the last tutorial we discussed Constructor overloading that allows a class to have more than one constructors having different argument lists.

Argument lists could differ in –

1. Number of parameters.

2. Data type of parameters.

3. Sequence of Data type of parameters.

Method overloading is also known as Static Polymorphism. Static polymorphism is also known as complie time binding or early binding.

 

Example 1: Overloading – Different Number of parameters in argument list.

d1.png

d2.png

Static variable-

It is a variable which belongs to the class and not toobject(instance)Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn’t need any object.

Syntax : <class-name>.<variable-name>

Static Method:

It is a method which belongs to the class and not to the object(instance).

A static method can access only static data. It can not access non-static data (instance variables)

A static method can call only other static methods and can not call a non-static method from it.

A static method can be accessed directly by the class name and doesn’t need any object.

Syntax : <class-name>.<method-name>

A static method cannot refer to "this" or "super" keywords in anyway

d3.png

// Output:

d4.png

 

Inheritance:

It is an object oriented concept.

It is used to derive the new classes on the basis of existing one.

The main feature of inheritance is code reusablility.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

 

Example of Inheritance:

public class Bicycle {
   // the Bicycle class has three fields
   public int cadence;
   public int gear;
   public int speed;
   // the Bicycle class has one constructor
   public Bicycle(int startCadence, int startSpeed, int startGear) {
       gear = startGear;
       cadence = startCadence;
       speed = startSpeed;
   }
   // the Bicycle class has four methods
   public void setCadence(int newValue) {
       cadence = newValue;
   }
       public void setGear(int newValue) {
       gear = newValue;
   }
       public void applyBrake(int decrement) {
       speed -= decrement;
   }
      public void speedUp(int increment) {
       speed += increment;
   }
       
}

A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:

public class MountainBike extends Bicycle {
       
   // the MountainBike subclass adds one field
   public int seatHeight;

   // the MountainBike subclass has one constructor
   public MountainBike(int startHeight,
                       int startCadence,
                       int startSpeed,
                       int startGear) {
       super(startCadence, startSpeed, startGear);
       seatHeight = startHeight;
   }   
       
   // the MountainBike subclass adds one method
   public void setHeight(int newValue) {
       seatHeight = newValue;
   }   
}
MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a newMountainBike class entirely from scratch, with four fields and five methods. However, you didn't have to do all the work. This would be especially valuable if the methods in the Bicycleclass were complex and had taken substantial time to debug.

Polymorphism:

Polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

Polymorphism can be demonstrated with a minor modification to the Bicycle class. For example, a printDescription method could be added to the class that displays all the data currently stored in an instance.

public void printDescription(){
   System.out.println("\nBike is " + "in gear " + this.gear
       + " with a cadence of " + this.cadence +
       " and travelling at a speed of " + this.speed + ". ");
}
To demonstrate polymorphic features in the Java language, extend the Bicycle class with a MountainBike and a RoadBike class. For MountainBike, add a field for suspension, which is a String value that indicates if the bike has a front shock absorber, Front. Or, the bike has a front and back shock absorber, Dual.

Here is the updated class:

public class MountainBike extends Bicycle {
   private String suspension;

   public MountainBike(
              int startCadence,
              int startSpeed,
              int startGear,
              String suspensionType){
       super(startCadence,
             startSpeed,
             startGear);
       this.setSuspension(suspensionType);
   }

   public String getSuspension(){
     return this.suspension;
   }

   public void setSuspension(String suspensionType) {
       this.suspension = suspensionType;
   }

   public void printDescription() {
       super.printDescription();
       System.out.println("The " + "MountainBike has a" +
           getSuspension() + " suspension.");
   }
}

Note the overridden printDescription method. In addition to the information provided before, additional data about the suspension is included to the output.

Next, create the RoadBike class. Because road or racing bikes have skinny tires, add an attribute to track the tire width. Here is the RoadBike class:

public class RoadBike extends Bicycle{
   // In millimeters (mm)
   private int tireWidth;

   public RoadBike(int startCadence,
                   int startSpeed,
                   int startGear,
                   int newTireWidth){
       super(startCadence,
             startSpeed,
             startGear);
       this.setTireWidth(newTireWidth);
   }

   public int getTireWidth(){
     return this.tireWidth;
   }

   public void setTireWidth(int newTireWidth){
       this.tireWidth = newTireWidth;
   }

   public void printDescription(){
       super.printDescription();
       System.out.println("The RoadBike" + " has " + getTireWidth() +
           " MM tires.");
   }
}
Note that once again, the printDescription method has been overridden. This time, information about the tire width is displayed.

To summarize, there are three classes: Bicycle, MountainBike, and RoadBike. The two subclasses override the printDescription method and print unique information.

Here is a test program that creates three Bicycle variables. Each variable is assigned to one of the three bicycle classes. Each variable is then printed.

public class TestBikes {
 public static void main(String[] args){
   Bicycle bike01, bike02, bike03;
   bike01 = new Bicycle(20, 10, 1);
   bike02 = new MountainBike(20, 10, 5, "Dual");
   bike03 = new RoadBike(40, 20, 8, 23);

   bike01.printDescription();
   bike02.printDescription();
   bike03.printDescription();
 }
}
//Output:

Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10.
Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10.
The MountainBike has a Dual suspension.
Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20.
The RoadBike has 23 MM tires.


Viewing all articles
Browse latest Browse all 103

Trending Articles