Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

17. Polymorphism in Java

 

Polymorphism

  • The word polymorphism means having many forms.
  • Polymorphism is the ability of a message to be displayed in more than one form.

In Java, polymorphism is mainly divided into two types:

  • Compile-time polymorphism
  • Run-time polymorphism

Compile-time polymorphism

It is also known as static polymorphism. This type of polymorphism is achieved by method overloading.

Method Overloading

  • When there are multiple method with the same name but different parameters, then these methods are said to be overloaded.
  • Methods can be overloaded by change in the number of arguments or/and a change in the data type of arguments.
class MethodOverloading {
    static void sum(int a, int b) {
        int ans = a + b;
        System.out.println("The integer sum of two numbers is " + ans);
    }

    static void sum(double a, double b, double c) {
        double ans = a + b + c;
        System.out.println("The double sum of three numbers is " + ans);
    }

    static void sum(double a, double b) {
        double ans = a + b;
        System.out.println("The double sum of two numbers is " + ans);
    }

    public static void main(String[] arr) {
        sum(10, 20);
        sum(20, 30);
        sum(10, 20, 30);
        sum(10.5, 19.5);
        sum(10.5, 19.5, 20);
    }
}

Output :

The integer sum of two numbers is 30
The integer sum of two numbers is 50
The double sum of three numbers is 60.0
The double sum of two numbers is 30.0
The double sum of three numbers is 50.0

Variable Arguments (Varargs)

  • Variable Arguments (Varargs) in Java is a method that takes a variable number of arguments.
  • Variable Arguments in Java simplifies the creation of methods that need to take a variable number of arguments.
class VariableArguments {
    static int sum(int... arr) { // Minimum number of elements that can be passed = 0
        int ans = 0;
        for (int element : arr) {
            ans += element;
        }
        return ans;
    }

    static int add(int x, int... arr) { // Atleast one element should be passed
        int ans = x;
        for (int element : arr) {
            ans += element;
        }
        return ans;
    }

    public static void main(String[] arr) {
        System.out.println("The sum of nothing is = " + sum());
        System.out.println("The sum of 1, 2 and 3 is = " + sum(1, 2, 3));
        System.out.println("The sum of 1 is = " + add(1));
        System.out.println("The sum of 12, 20 and 15 is = " + add(12, 20, 15));
    }
}

Output :

The sum of nothing is = 0
The sum of 1, 2 and 3 is = 6
The sum of 1 is = 1
The sum of 12, 20 and 15 is = 47

Constructor Overloading

class GrandParent {
    public GrandParent() {
        System.out.println("This is a constructor in GrandParent class.");
    }

    public GrandParent(int x) { // Overloaded Constructor
        System.out.println("This is a constructor in GrandParent class with value: "
                + x);
    }
}

class Parent extends GrandParent {
    public Parent() {
        System.out.println("This is a constructor in Parent class.");
    }

    public Parent(int x, int y) { // Overloaded Constructor
        super(x); // Calls the constructor having 1 argument
        System.out.println("This is a constructor in Parent class with value: " + y);
    }
}

class Child extends Parent {
    public Child() {
        System.out.println("This is a constructor in Child class.");
    }

    public Child(int x, int y, int z) { // Overloaded Constructor
        super(x, y); // Calls the constructor having 2 arguments
        System.out.println("This is a constructor in Child class with value: " + z);
    }
}

class ConstructorOverloading {
    public static void main(String[] arr) {
        new Child(); // Constructor Calling
        new Child(10, 20, 30); // Constructor Calling
    }
}

Output :

This is a constructor in GrandParent class.
This is a constructor in Parent class.
This is a constructor in Child class.
This is a constructor in GrandParent class with value: 10
This is a constructor in Parent class with value: 20
This is a constructor in Child class with value: 30

Note: The super keyword in Java is a reference variable that is used to refer parent class object. When the super class has same named data members or methods or in case of constructors, then we use super keyword to resolve ambiguity.

Run-time polymorphism

It is also known as Dynamic polymorphism. This type of function is achieved by method overriding.

Method Overriding

  • When a method in a sub-class has the same name, same parameters or same signature, and same return type as a method in its super-class, then the method in the sub-class is said to override method in the super-class.
  • If an object of parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the sub-class is used to invoke the method, then the version in the child class will be executed.
  • Final, private (access modifiers) methods and static methods cannot be overridden. When we define a static method with same signature as a static method in base class, it is known as the method hiding.
class GrandParent {
    public void method() {
        System.out.println("This is a method of GrandParent class");
    }

    public void anotherMethod(int x) {
        System.out.println("This is another value of GrandParent class with value: "
                + x);
    }
}

class Parent extends GrandParent {
    @Override
    public void method() {
        System.out.println("This is a method of Parent class");
    }

    @Override
    public void anotherMethod(int x) {
        System.out.println("This is another value of Parent class with value: " + x);
    }
}

class Child extends Parent {
    @Override
    public void method() {
        System.out.println("This is a method of Child class");
    }

    @Override
    public void anotherMethod(int x) {
        System.out.println("This is another value of Child class with value: " + x);
    }
}

class MethodOverriding {
    public static void main(String[] arr) {
        GrandParent obj1 = new GrandParent();
        Parent obj2 = new Parent();
        Child obj3 = new Child();
        obj1.method();
        obj2.method();
        obj3.method();
        GrandParent obj4 = new GrandParent();
        Parent obj5 = new Parent();
        Child obj6 = new Child();
        obj4.anotherMethod(30);
        obj5.anotherMethod(20);
        obj6.anotherMethod(10);
    }
}

Output :

This is a method of GrandParent class
This is a method of Parent class
This is a method of Child class
This is another value of GrandParent class with value: 30
This is another value of Parent class with value: 20
This is another value of Child class with value: 10

Note: @Override is not necessary to write, but it is beneficial to write while overriding a method in very large programs. If we change the method in super-class, then @Override will give an error, as the sub-class method was overridden with that method.

Dynamic Method Dispatch

  • It is a technique in which a super-class reference variable refers to the object of the sub-class.
  • Dynamic method dispatch is also known as run time polymorphism.
  • It is the process through which a call to an overridden method is resolved at run time.
class Parent {
    void relation() {
        System.out.println("This is a Parent class.");
    }

    void elder() {
        System.out.println("Parents give birth to their children's.");
    }
}

class Child extends Parent {
    @Override
    void relation() {
        System.out.println("This is a Child class.");
    }

    void young() {
        System.out.println("Child is because of parents.");
    }
}

class DynamicMethodDispatch {
    public static void main(String[] arr) {
        Parent obj1 = new Child();
        obj1.relation();
        obj1.elder();
        Child obj2 = new Child();
        obj2.young();
    }
}

Output :

This is a Child class.
Parents give birth to their children's.
Child is because of parents.


Previous

Next

Comments