Abstraction: Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user.
Encapsulation: Encapsulation simply means binding object state (fields) and
behaviour(methods) together. If you are creating class, you are doing encapsulation.
Message passing
A single object by itself may not be very useful. An application contains many objects. One
object interacts with another object by invoking methods on that object. It is also referred to
as Method Invocation a diagram below.
Polymorphism
Polymorphism means a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly"
means many and "morphs" means forms. So polymorphism means many forms.
two types of polymorphism in Java: static polymorphism or compile time binding
or early binding and runtime polymorphism or run time binding.
Polymorphism can be implemented by overloading and overriding process
Method
block of code an actual part of the program that perform task and exposes the
behaviour of an object
we can pass data, known as parameters, into a method
A method must be declared within a class also called instance method.
It is defined with the name of the method, followed by parentheses ().
Java provides some pre-defined methods, such as println(), but you can also create
your own methods to perform certain actions
class Demo
{ void Method_name()
{
// code to be execute
}
Method Overloading
Class having more than one method with the same name but whose argument lists are
different is called method overloading
return type may or may not be the same depending on the parameter type
Its advantage is to increase the readability of the program
An example of static polymorphism is where the binding of a method calls to its definition at
Compile time.
int add(int a, int b)
int add(int a, int b, int c)
int add(float a, int b, double d)
float add(float f1,float f2)
e.g
class Overloading
{
public void display(char c)
{
System.out.println(c);
}
public void display(char c, int num)
{
System.out.println(c + " "+num);
}
public void display(String s, int num)
{
System.out.println(s + " "+num);
}
}
class Demo
{
public static void main(String args[])
{
Overloading o = new Overloading();
o. display('a'); //char
o. display('a',10);
o.display(“sonam”,23);
o.display(.90f, 23.4f); // if display(double d, double d) and we passed float value then
automatic conveted or promoted but de-promoted may not support
}
}
Method Overloading and Type Promotion
One type is promoted to another implicitly if no matching data type is found
byte can be promoted to short, int, long, float or double e.g is highlighted in the above
program
The char data type can be promoted to int, long, float or double
As displayed in the above diagram, byte can be promoted to short, int, long, float or double.
The short datatype can be promoted to int, long, float or double.
The char datatype can be promoted to int, long, float or double and so on.
Note: One type is not de-promoted implicitly for example double cannot be
de-promoted to any type or it cannot be de-promoted to a lower type
implicitly.
public class Overloading
{
void sum (short a,byte b)
{
System.out.println("a method invoked");
}
void sum(double a,double b) //only this method will called
{
System.out.println("b method invoked");
}
public static void main(String args[])
{
Overloading obj=new Overloading ();
obj.sum(20.3f,20.0f); //now ambiguity
}
}
Can we overload java main() method?
Yes, we can have any number of main methods in a class by method
overloading. But JVM calls main() method which receives string array as
arguments only.
class MainOverloading
{
public static void main(String[] args)
{
System.out.println("main with String[]");
}
public static void main(String args)
{
System.out.println("main with String");
}
public static void main()
{
System.out.println("main without args");
}
}
Invalid
Same parameter with same data types
int add(int,int)
int add(int,int)
Different return type but the same parameter
int add(int,int)
double add(int,int)
H.W
Write a program to overload the area method of circle, square, rectangle
Write a program to create the method for promoting and de-promoting concepts in method
overloading.