Inheritance&Polymorphism
Inheritance&Polymorphism
Consider in a project we have loan model in which there are three classes as
given below:
Here all classes are belongs to loan so there should be some common
properties. And suppose there are 10 common methods.
But we have to those 10 methods in all three classes, so code is repeating
here.
Also in maintenance if we want to change in one the common method we
have to change at three location so maintenance also become complex.
To overcome above problems we can take separate class Loan and we can
write those common methods inside this class. And simply we will extend
this Loan class in all three classes as follows:
If we extends one class into another class one class become parent and
another become child.
In above example Loan become parent and HomeLoan and other two
become child classes.
And all the members of parent class is available to child class so we don’t
have to write it again.
Here code reusing and maintenance also become easy because if we change
in parent class method that will reflect automatically in all child classes.
Multiple Inheritance:
Extending more than one class in one class is called multiple
inheritance and java does not support multiple inheritance i.e in java we
cannot extend more than one class.
class Test extends Test1{ Here when we call d.a , jvm will
int a = 20; search a variable in Demo class first
} if he don’t get in Demo class, then it
will search in it’s parent class Test
public class Test1 { and Test1.
int a = 10; But both the classes contain ‘a’
} variable then jvm will get confused.
class Demo extends Test,Test1{ Hence java does not support
public static void main(String[] multiple inheritance.
args) {
Demo d = new Demo();
System.out.println(d.a); Class Test{ Class
} Int a = 10; Test1{
} } Int b = 20;
}
Class Demo
extends
Test,Test1{
main(){
Demo d = new
Demo();
sopln(d.a);
}
}
Multilevel Inheritance:
class Test1 {
Test1
int a = 10;
}
Polymorphism:
Polymorphism in java is a concept by which we can perform a single action
by 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.
There are two types of polymorphism in java: compile time polymorphism
and runtime polymorphism. We can perform polymorphism in java by
method overloading and method overriding.
Overloading:
If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
If we have to perform only one operation, having same name of the methods
increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be
any number of arguments, if you write the method such as a(int,int) for two
parameters, and b(int,int,int) for three parameters then it may be difficult for
you as well as other programmers to understand the behavior of the method
because its name differs.
So, we perform method overloading to figure out the program quickly.
Example:-
class Demo {
public void add(){
int c = 10 + 20;
System.out.println(c);
}
public void add(int a,int b){
int c = a + b;
System.out.println(c);
}
public void add(float a,int b){
float c = a + b;
System.out.println(c);
}
}
Example: