$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: 2
  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
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,16,Arrays,7,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,22,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,81,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,4,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,9,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,29,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,66,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,2,Java Programs,65,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,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,1,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,21,String,58,String Programs,9,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Polymorphism in Java
Polymorphism in Java
https://2.bp.blogspot.com/-Hfif0WM6-14/XKxprakCeiI/AAAAAAAABSI/YQd8z_qSG6gxZ0EdDDouKQOZYIYAIPr9wCLcBGAs/s400/Polymorphism%2Bin%2BJava.PNG
https://2.bp.blogspot.com/-Hfif0WM6-14/XKxprakCeiI/AAAAAAAABSI/YQd8z_qSG6gxZ0EdDDouKQOZYIYAIPr9wCLcBGAs/s72-c/Polymorphism%2Bin%2BJava.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