alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Java
    .net Framework
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Java - Object-Oriented Programming (OOPS) Concept - Polymorphism (Runtime & Compile Time) Tutorial

Polymorphism is the ability of an object to take more than one form.

There is two way to achieve polymorphism.

  • Method Overloading(to achieve compile-time polymorphism)
  • Method Overriding(to achieve runtime polymorphism)

 

 

Method Overloading (Use to achieve Compile Time Polymorphism)

Suppose in a class, multiple methods with the same name but different parameters exist. Then this is known as method overloading.

 

Method overloading can be performed by changing the no. of argument and by changing the datatype.

class Addition{
static int arg(int a,int b){return a+b;}
static double arg(double a,double b){return a+b;}
static int arg(int a,int b,int c){return a+b+c;}
}

public class methodoverloading{
public static void main(String[] args){
Addition a=new Addition();
System.out.println(a.arg(1,7));
System.out.println(a.arg(1.0,9.0));
System.out.println(a.arg(1,7,8));
}}

Run Program

In the above example, there are two methods with name arg but with different no. of parameter. In the first method, there is a two-parameter and in the second method, there is a three-parameter.

In java, the correct method is automatically detected using no. of argument or datatype of the argument.

 

Method Overloading (Use to achieve Runtime Time Polymorphism)

Suppose a parent class and child class that is achieved using inheritance has the same method name with the same parameter. Then this is known as method overriding.

class Bird{//Parent Class
void fly(){
System.out.println("Bird can Fly");
}
}

public class Parrot extends Bird{ //Child Class

void fly(){
System.out.print("Parrot can Fly ");
}

public static void main(String args[]){
Parrot p=new Parrot();
p.fly();
}
}

Run Program

 

Java

Java

  • Introduction
  • Installation and running a first java program
    • JDK Installation
    • Environment Variable Path Setup
    • Running a first java program
  • 1st Java Program Explanation
    • Program Syntax
  • JDK,JRE,JVM
    • JRE
    • JDK
    • JVM
  • Thing to know before proceed
    • Variable
    • Datatype
    • Operator
    • Keyword
    • Access Modifier
  • Decision Control and looping statement
    • If-else
    • Switch
    • For
    • While
    • do-while
    • break
    • continue
  • Object-Oriented Programming (OOPS) Concept
    • About OOPs
    • Object & Class
    • Inheritance
    • Polymorphism (Runtime & Compile Time)
    • Abstraction (Using abstract and interface)
    • Encapsulation

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.