Professional Documents
Culture Documents
Overloading Overriding
Whenever same method or Constructor is Whenever same method name is existing
existing multiple times within a class either multiple time in both base and derived
1 with different number of parameter or with class with same number of parameter or
different type of parameter or with different same type of parameter or same order of
order of parameter is known as Overloading. parameters is known as Overriding.
Arguments of method must be different at Argument of method must be same
2
least arguments. including order.
3 Method signature must be different. Method signature must be same.
Private, static and final methods can be Private, static and final methods can not
4
overloaded. be override.
Access modifiers point of view not
5 Access modifiers point of view no restriction. reduced scope of Access modifiers but
increased.
Also known as compile time polymorphism Also known as run time polymorphism or
6
or static polymorphism or early binding. dynamic polymorphism or late binding.
Overloading can be exhibited both are Overriding can be exhibited only at
7
method and constructor level. method label.
The scope of Overriding is base class and
8 The scope of overloading is within the class.
derived class.
Overloading can be done at both static and Overriding can be done only at non-static
9
non-static methods. method.
For overloading methods return type may or For overriding method return type should
10
may not be same. be same.
Static keyword is used java for memory management. Static variables are usually stored in
static memory. Static variables are rarely used other than being declared as constants.
We can use static keyword with methods, variable, blocks and nested class. The static
keyword belongs to the class than instance of the class.
static can be:
a. Method
b. Variable
c. Block
d. Nested class
a) static Variable
static variables are formed by declaring static keyword before variable. The static key word
is used to create variables that will exist independently of any instances created for the class.
Only one copy of the static variable exists regardless of the number of instances of the class.
static variables are also known as class variables. Local variables cannot be declared static.
Properties
class Count
{
static int c=0;
Count()
{
c++;
System.out.println(c);
}
public static void main(String arg[])
{
Count c1=new Count();
Count c2=new Count();
Count c3=new Count();
}
Output
1
2
3
b) static Method
Static methods are formed by declaring static keyword before any method. Static methods
take the data from the parameters and compute the result from those parameters with no
reference variables.
Properties:
class Company
{
int id;
String name;
static long salary;
c1.show();
c3.show();
}
}
Output
Id of employee is 100
Id of employee is 102
Final keyword defines itself that once final keyword is used then one cannot extend or change
its value. In java the final keyword is used in different methods to define any variable that can
be only assigned one time in program.
The final keyword has mainly three uses one of them is to create final class. Second one is to
use final methods and third one is to use final data member.
1. Stop Inheritance.
2. Stop Method overriding.
3. Stop value change.
final class
It is used to avoid inheritance. Once a final word assigned before class then this class cannot
be inherited further. In other words final class cannot have its derived class.
Syntax:
//define class
final Method
Syntax:
class classname
If final keyword is used before any variable then one cannot change its value. In other words
once final keyword is used then one cannot overwrite or change its value.
Syntax:
class classname
void functionname()