$show=/label

Polymorphism in Java

SHARE:

Polymorphism in Java. In this tutorial, we will discuss about Polymorphism. Polymorphism represents one cell may have multiple forms, o...

Polymorphism in Java.

In this tutorial, we will discuss about Polymorphism.

Polymorphism represents one cell may have multiple forms, one method can have different behaviors.  In our education, class professor has told that poly indicates "many" and  morph means forms or change. From here Polymorphism word has evolved which has many forms.


Polymorphism in Java


Generally, Polymorphism can be referred between child and parent classes.

Child class can have as same as its parent class behavior or can implement its own behavior.

Polymorphism Example:


For example. We have an interface School, class is Student and class Address.
 

public interface School{
}

public class Address{
}

public class Student implements School{

private Address address;

}

In Java, Object class is base or parent class for all classes in java api as well for user defined class.

We can describe Student object as below.

a) Student is a part of School
b) Student has a address.
c) Student is Student
d) Student is Object

We will demonstrate Polymorphism with the following example.
package com.java.w3schools.core.inheritance;

public class Company {

    private String companyName;
    private String address = "California";

    public Company(String cName, String cAddress) {
        companyName = cName;
        address = cAddress;
    }

    public String getAwards() {
        return "No Awards";
    }
    public String[] getPolicies(String designation) {
        String[] policies = { "HR Policy", "Tax Policy" };
        return policies;
    }
}

package com.java.w3schools.core.inheritance;

class Employee extends Company {
    public Employee(String cName, String cAddress) {
        super(cName, cAddress);
    }

    @Override
    public String[] getPolicies(String designation) {
        String[] policies = new String[2];
        if ("D1".equalsIgnoreCase(designation)) {
            policies[0] = "Policy 3";
            policies[1] = "Policy 4";
        } else if ("D2".equalsIgnoreCase(designation)) {
            policies[0] = "Policy 5";
            policies[1] = "Policy 6";
        } else {
            policies[0] = "Policy 1";
            policies[1] = "Policy 2";
        }
        return policies;
    }
}

public class EmployeeMain {
    public static void main(String[] args) {
        Employee employee1 = new Employee("Michael", "California");
        String[] polices = employee1.getPolicies("D2");
        System.out.println("policy one : " + polices[0]);
        System.out.println("policy Two : " + polices[1]);
    }
}


Output:


policy one : Policy 5
policy Two : Policy 6

Company class has implementation for getPolicies, but child class provided its defined own implementation. Child class Employee now independent of parent class.

By using following code in Employee class, we can make use Employee class implementation as well parent class implementation for getPolicies method. We have invoked parent class method in last else block.
@Override
    public String[] getPolicies(String designation) {
        String[] policies = new String[2];

        if ("D1".equalsIgnoreCase(designation)) {
            policies[0] = "Policy 3";
            policies[1] = "Policy 4";
        } else if ("D2".equalsIgnoreCase(designation)) {
            policies[0] = "Policy 5";
            policies[1] = "Policy 6";
        } else {
            return super.getPolicies(designation);
        }
        return policies;
    }


If we pass apart from "D1", "D2" designation than return parent class method policies.


Output:


policy one : HR Policy
policy Two : Tax Policy

From above code, we observed that behavior is changing during runtime upon input. That returns either child or parent class getPolicies method String array.

We will see 

1) Compile time Polymorphism(Overloading)
2) Runtime Polymorphism(Overriding)

COMMENTS

BLOGGER: 1
Please do not add any spam links in the comments section.

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Polymorphism in Java
Polymorphism in Java
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9auKRbBUkGNezX9BH1ZkHrwzJ9AZy0Rq0gGDhZZlH-UMEsGqjIVaJE5MckgKJsvDonYwOe7QAg2RvinmZVUpR1IJ1-FJcVUUXrkRsw69GWs-bpghsniKmRP3kQWYPwUXW67mEBDq5hY8/s400/Polymorphism+in+Java.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9auKRbBUkGNezX9BH1ZkHrwzJ9AZy0Rq0gGDhZZlH-UMEsGqjIVaJE5MckgKJsvDonYwOe7QAg2RvinmZVUpR1IJ1-FJcVUUXrkRsw69GWs-bpghsniKmRP3kQWYPwUXW67mEBDq5hY8/s72-c/Polymorphism+in+Java.PNG
JavaProgramTo.com
https://www.javaprogramto.com/2015/04/polymorphism.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2015/04/polymorphism.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content