You are on page 1of 6

Difference between Overloading and Overriding

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.

FINAL AND STATIC KEYWORDS


Static Keyword

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

1. It makes programs memory efficient.


2. The static variable can be used to refer the common property of all objects.
3. The static variable gets memory only once in class area at the time of class loading.

Consider the Example

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:

1. Any static method belongs to class instead of object of a class.


2. It can be invoked without using the need of creating an instance of class.
3. These methods can access static data member.
4. These methods are able to modify static data members.

Consider the Example

class Company
{
int id;
String name;
static long salary;

static void change()


{
salary=salary + 50000;
}
Company(int i,String n,long s)
{
id=i;
name=n;
salary=s;
}
void show()
{
System.out.println("Id of employee is "+id);
System.out.println("Name of Employee is "+name);
System.out.println("Salary of Employee is "+salary);
}
public static void main(String arg[])
{
Company.change();
Company c1=new Company(100,"Shiva",100000);
Company c3=new Company(102,"Karishma",150000);

c1.show();
c3.show();
}
}
Output

Id of employee is 100

Name of Employee is Shiva

Salary of Employee is 150000

Id of employee is 102

Name of Employee is Karishma

Salary of Employee is 15000

Java - final keyword

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.

Use of final keyword

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.

Following are uses final keyword:

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:

final class <classname>

//define class

final Method

It is used to avoid method overriding in java.


In other words if final keyword is assigned before any function then one cannot use the name
of function to create another function.

Syntax:

class classname

final void functionname()

final data member:

It is used to define constant identifier.

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()

final int x=100;

Characteristics of final keyword

1) A constructor cannot be declared as final.


2) Local final variable must be initializing during declaration.
3) All variables declared in an interface are by default final.
4) We cannot change the value of a final variable.
5) A final method cannot be overridden.
6) A final class not be inherited.
7) If method parameters are declared final then the value of these parameters
cannot be changed.
8) It is a good practice to name final variable in all CAPS.
9) final, finally and finalize are three different terms. finally is used in exception
handling and finalize is a method that is called by JVM during garbage
collection.

You might also like