Skip to main content

polymorphism and Overloading in java programming

 Overloading Concepts in java-polymorphism and Overloading in java programming 

Overloading

what is method overloading in java?

The process of defining methods with the same name but different functionalities is called method overloading. In other words, with the same name and different parameters.This is not supported in c language.For example,an overloaded draw() method can be used to draw anything,such as a circle,square,triangle and so on.

how many types of overloading in java?

Types of overloading

In java there are three types of overloading

  • constructor overloading ---> Default constructor(without any arguments)Parameterized constructor(with some arguments)
  • method overloading ---> Method Without any argument and method with arguments
  • operator overloading ---> The + symbol  can be implemented in two ways one is  addition operator and another one is concatenation operator,same symbol used to perform two purposes.It is called operator overloading

Polymorphism

Implimenting same thing in diffrent way,this is called polymorphism.All these three types of overloading term is called as polymorphism.Polymorphism in java,is achieved by implementing the overloading concepts.Since these three overloading occurs in the compile time, so it is called compile time polymorphism.

Types of polymorphism

In java there are two types of polymorphism
  • Compile time polymorphism ---> compile time polymorphism is achieved by implementing method overloading conceptThis is called static binding
  • Runtime polymorphism ---> Runtime polymorphism is achieved by implementing method overriding concepts.This is called dynamic binding

Q:Which java class programming technique shows the use of polymorphism?

A: polymorphism in java, is achieved by implementing the overloading concepts


sample program constructor overloading


class Student { Student() { System.out.println(); System.out.println("This is a default consructor"); System.out.println("____________"); } Student(String str,int i) { System.out.println("This is a parametrized constructor"); System.out.println("Name :"+str+" Age :"+i); System.out.println("____________"); } } class Constructor { public static void main(String args[]) { Student std=new Student(); Student std1=new Student("Bilal",10); } }

simple java program for constructor overloading

sample program method overloading


class Adding
{
void add()
{
System.out.println();
System.out.println("Method without arguments");
System.out.println(10+20);
System.out.println("____________");
}
void add(int i,int j)
{
System.out.println("Method with arguments");
System.out.println(i+j);
System.out.println("____________");
}
}
class Method
{
public static void main(String args[])
{
Adding std=new Adding();
std.add();//calling without argument method
std.add(50,50);// calling argument method
}
}

method overloading in java with example program


Sample program operator overloading

class Operator
{
public static void main(String arg[])
{
String str ="Welcome";
System.out.println(200+300);//adding twonumber with +
System.out.println(str+" to Java progrmming");//added two string with +
}
}
Observe the programs and its output carefully

operator overloading in java example program






 


Comments

Popular posts from this blog

Introduction to java programming-very important theory notes

Introduction to java programming what is java? Java is a high level object oriented computer programming language(OOPS). Java is a general purpose programming language used in computers and other applications. Java, an object-oriented programming language developed by Sun Microsystems under the leadership of James Gosling, Bill Joy, etc., is used today in many electronic devices such as web servers, computers, and mobile phones. Although it is mostly used for web programming, it is a multi-purpose programming language that can be used to do many other things. After Oracle bought Sun Microsystems in mid-2009, Java came under Oracle's control. (Wikipedia). What is a computer? A computer is an electronic device for storing and processing huge amounts of information. What is a computer program? A computer program is a set of instructions for a computer to perform a specific task. A computer cannot do anything by itself, if it wants to do something, it must get clear instructions, the i...

JAVA BUZZWORDS

     JAVA BUZZWORDS & H istory of the development of java java buzzwords explanation     Features of java is called as Java Buzzwords Platform independent —> Run on any Environment Object oriented concept(oops) —> Abstraction,Encapsulation,Inheritance and polymorphism Simple —>  No concept of pointers,explicit memory allocation, structures,operator overloading etc  Secure —> Secure for internet application (Raise  out of boundary exception in arrays and it is strong typed language) Robust —> Early checking of error it can achieved garbage collector, exception handling Portable  —> Java program written on one environment can be executed or implemented another environment Multithreading  —> Concurrent execution of several parts of same program at same time(This will improve CPU utilization) Distributed Application —> This are software that runs on multiple computers connected  to a network at ...

How to run java program using while loop

 Java program how to print Happy New Year in 10 times(using while loop) java buzzwords static keyword in java The AI ​​Revolution- Malayalam article step by step explanation Before writing Java code Install Jdk on your system and Set the path properly. Then type the Java code in the notepad and save it as a class name with the .java extension. Select the file type 'all file' while saving. Open cmd, Change the directory where the file is saved, using the cd command. Type javac class name.java then press the enter key, If you type java and give the class name, If there is no error in the program, the output will be generated. Observe the directory change using the cd command. Give the class name and watch it compile and run. How to run java program in notepad and cmd Where to code in Java and how it works? method 1 A text editor is required to write Java code The code can be run using the command prompt method 2  using an integrated development environment (IDE), such as Eclips...

Oops concepts in java

oops concepts in java with example programs Oops concepts in java Oops concept is very very important to make java programming easier and increase your programming skill.Four pillars of oops are Abstraction,Encapsulation,Inheritance,Polymorphism.Another technique to carefully understand in Java is classes and objects. This article covers very simple notes for Java programming beginners. What is object and class? The basics of oops is class and object Class is a blueprint which defines some properties and behaviors.  An object is an instance of a class which has those properties and behaviors attached.A class is not allocated memory when it is defined. An object is allocated memory when it is created Class and object —> object is defined as real word entities .Object consists of property and some task Class is a blueprint that object follows, Class  consists of many number of objects Four pillars of oops are Abstraction   —> Means showing only essential parts and hi...

Command Line Arguments In Java

Command Line Arguments In Java No input statement needed in the program itself. we can pass the input as an argument while running the program. Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command line arguments to main function. To access the command line arguments inside a java program is quite easy are stored as string in string array passed to main function. Example program of command line arguments in java class CommandLineExample {    public static void main(String args[]) {    System.out.println("\nMy first input is  :"+args[0]);    }    }    Addition of two numbers in java using command line arguments  Example:write a program add two integer number    class AddCommandLine { public static void main(String args[]) { int num1=Integer.parseInt(args[0]); //type cast int num2=Integer.parseInt(args[1]); //type ...

Keywords in java programming

Keywords in java programming Keyword is a predefined word given by the compiler which has some specific task. Keywords also known as reserved word. All the java technology keywords are lower case . Java programming has a richer set of keywords than C language or C ++ language. Some java keywords are import —>In java programming implementing classes and methods import the packages that purpose using the keyword import. class —> In order to create a class we have use the keyword class to see an example written using the class keyword ) this —> This keyword is used to represent the method belongs to particular current object Super —> It can used in inheritance concept extends —> Also used in inheritance concepts. Accessing methods one class to another class that can done by extends keyword package —> In java there will be package which consists of number of classes and interfaces in order to access that package, using the key word package return —> Return key wor...

Naming Convention In Java

Naming Convention In Java-camel case example Naming Conventions are rules to be followed by giving a name to the identifier.Identifiers are program elements(such as class,object,variable,method so on ).For building software you should follow this rule to make your code look good, more efficient and more readble.Java has certain rules for naming identifier. This are given below Digit, letters, underscore or currency symbols( £, €, $ ) are allowed                The first letter of any identifier cannot be a digit An identifier name must not be a reserved words or keywords Java follows camelCase rule what is camelcase rule? CamelCase is a way to separate the words in a phrase by making the first letter of each word capitalized and not using spaces (ex :- MyFirstJavaClass) Class name —> should begin with a capital letter(In a single word) If it is multiple words, the first letter of every word begins with a capital letter. Ex :- class Stu...