Advanced Java Programming

Image
                          Advanced Java Programming Advanced Java Programming :-   Introduction to advance java   As most of us already know that if we want to make normal applications it  can be easily built using core Java concepts. But, when it we need to develop web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc. needed, so to add capabilities and features of the application advance java is essential for developers. Through the motive of this blog is to explain about Advanced Java, I will be giving you a complete insight into the fundamental concepts of Advance Java. Figure - 1.2 If you want to see complete video on this please  have a look the video below.                              Learn with Resh u Advanced Java Programming Course Figure - 1.3 I hope you understood by the slide why Advanced Java is essential. For your better understanding, I have divided this article int

Polymorphism in Java Method Overriding and Method OverLoading in Java

Polymorphism in Java Method Overriding and Method OverLoading in Java

Java Polymorphism



Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Like we specified in the previous Blog; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
Polymorphism are two types


  1. Method Overriding : It calls as compile time polymorphism or static or early binding. 

  1. Method Overloading: It calls as Runtime polymorphism or dynamic or late binding.


For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.): This is a example of Method overriding in Polymorphism.


Example

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

Now we can create Pig and Dog objects and call the animalSound() method on both of them:


class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class MyMainClass {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

Method Overriding

In Method Overriding the overridden method has same name but different arguments as number of arguments Sequence of arguments or type of arguments would be different.

In The example below the Class Adder has three add method with different arguments this is a example of method overriding.


package com.polymorphism.examples;

public class Adder {
 
 void add(double a, int b) {
  
  System.out.println(a+b); 
 }
   
 void add(int b, double a) {
  
  System.out.println(a+b);
  
   System.out.println("Sequence changed");
 }
   void add(int a, int b) {
   
    System.out.println("no arguments");
 }
   
   public static void main(String[] args) {
    
    Adder test = new Adder();
      test.add(4,7);
      test.add(3.3, 7);
      test.add(8 , 4.9 );
   }
   
}

There are some Difference between Method Overriding And Method Overloading 


 Difference Between Method Overloading and Method Overriding





Example of Polymorphism




For Complete Explanation in Hindi Please visit My Youtube Channel 


Complete Explanation on Method Overloading In Hindi



Complete Explanation on Method Overriding In Hindi


                                              
Thanks For Watching!!!

Recommended Posts:


·       Overriding in Java
·       Overloading in Java
·       Interfaces in Java
·       Loops in Java
·       Variables in Java

Advanced Java Programming

         Abstraction in Java
·       Encapsulation in java
·       What is Java
·       Enabling Remote Debug

Comments

Popular posts from this blog

Advanced Java Programming

Updating version numbers of modules in a multi-module Maven project