Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

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.


Varun Kapila

Comments   

0 #1 s 128 2022-02-18 11:23
I do not even know how I ended up here, however
I assumed this submit used to be good. I do not recognize who you are but definitely you
are going to a famous blogger if you are not already.
Cheers!
Quote
0 #2 s 128 2022-03-24 03:55
Hello there! Do you use Twitter? I'd like to follow you if that would be okay.
I'm undoubtedly enjoying your blog and look forward to new posts.
Quote
0 #3 s128 2022-03-25 09:32
Hi, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot
of spam comments? If so how do you prevent it, any plugin or
anything you can suggest? I get so much lately it's driving me mad so any assistance is very
much appreciated.
Quote
0 #4 s 128 2022-03-25 17:52
Excellent goods from you, man. I have be
aware your stuff prior to and you're simply too excellent.
I actually like what you've got right here, certainly like what you're saying and the way during which you are saying it.
You're making it entertaining and you continue to take care of to keep it smart.
I can not wait to learn much more from you.
That is actually a wonderful website.
Quote
0 #5 s 128 2022-03-26 06:41
Link exchange is nothing else but it is only placing the other person's blog link
on your page at suitable place and other person will also do same in support of you.
Quote
0 #6 s128 2022-03-26 11:37
Pretty section of content. I just stumbled upon your weblog and in accession capital to
assert that I get in fact enjoyed account your blog posts.
Any way I'll be subscribing to your feeds and
even I achievement you access consistently rapidly.
Quote
0 #7 s 128 2022-03-26 15:15
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! However, how could we communicate?
Quote
0 #8 s128 2022-03-29 07:29
I think this is one of the most important information for me.
And i'm glad reading your article. But should remark on few
general things, The site style is perfect, the articles is
really great : D. Good job, cheers
Quote

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner