Polymorphism:
- If one task is performed in different ways it is known as polymorphism.
- In Java, we use method overloading and method overriding to achieve polymorphism.
- Example:
- Normal methods: student1(), student2(), student3(), … and so no.
- In Polymorphism: student(), student(), student(), … and so no. (Same names)
Method Overloading:
If a class has multiple methods (or Constructors) with the same name but different parameters, it is known as method overloading.
Way to achieve method overloading:
- By changing the number of arguments
- By changing the data type.
- Example Program:
public
class Overloading
{
void
add(int
a, int
b)
{
System.out.println(a+b);
}
void
add(double
a,
double
b)
{
System.out.println(a+b);
}
void
add(int
a, int
b, int
c)
{
System.out.println(a+b+c);
}
public
static
void
main(String[] args)
{
Overloading ol
= new
Overloading();
ol.add(1, 3);
ol.add(2.3, 3.1);
ol.add(1, 2, 3);
}
}
|
Method Overriding:
If the subclass (child class) has the same method as declared in the parent class, it is known as method overriding.
Rules for method overriding:
- The method must have the same name as in the parent class.
- The method must have the same parameter as in the parent class.
- Must be an IS-A relationship (Inheritance).
Uses of method overriding:
- It is used to provide specific implementation of a method already provided by its super class (parent class).
public
class
OverridingParent { int
num1 = 10; public
void addition() { int
num2 = 20; int
res =
num1+num2; System.out.println(res); } }
|
public
class OverridingChild
extends OverridingParent
{ int
num1 = 20; public
void addition() { int
num2 = 30; int
num3 = 40; int
res =
num1+num2+num3; System.out.println(res); } public
static
void main(String
args[]) { OverridingChild oc =
new OverridingChild(); oc.addition(); } }
|
Super Keyword:
The ‘super’ keyword
is a reference variable that is used to refer to the immediate parent
class object. Or the ‘super’ keyword is used in subclasses
to access the superclass method.
Usage of the ‘super’ keyword:
- To refer immediate parent class instance variable.
- To invoke the immediate parent class method.
- To invoke immediate parent class constructor.
Example Program:
public
class
SuperParentClass
{
String name = "Parent";
public void display()
{
System.out.println(name);
}
}
|
public
class SuperChildClass
extends SuperParentClass { String
name =
"child"; public
void display() { System.out.println(name); System.out.println(super.name); } public
void show() { super.display(); } public
static
void main(String
args[]) { SuperChildClass
sc =
new SuperChildClass(); System.out.println("****************"); sc.display(); System.out.println("****************"); sc.show(); } }
|
Final Keyword:
- The ‘final’ keyword is a non-access modifier application only to a variable, a method, or a class.
- The final keyword is used to denote Constants.
- Final Variable: To create constant variables.
- Final Method: To prevent method overriding.
- Final Class: To prevent Inheritance.
public
class FinalKey {
public static void main(String[] args) {
int a = 10;
final int b = 20;
a = 20;
//Error: because b is final
b = 40;
}
}
|
No comments:
Post a Comment