0% found this document useful (0 votes)
4 views

Inheritance&Polymorphism

The document explains the concepts of inheritance, polymorphism, method overloading, and method overriding in Java. It illustrates how inheritance allows for code reuse and simplifies maintenance by creating a parent class from which child classes can inherit common properties and methods. It also discusses the differences between compile-time and runtime polymorphism, highlighting the ambiguity issues with multiple inheritance and the significance of method signatures in overloading and overriding.

Uploaded by

truptinik27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Inheritance&Polymorphism

The document explains the concepts of inheritance, polymorphism, method overloading, and method overriding in Java. It illustrates how inheritance allows for code reuse and simplifies maintenance by creating a parent class from which child classes can inherit common properties and methods. It also discusses the differences between compile-time and runtime polymorphism, highlighting the ambiguity issues with multiple inheritance and the significance of method signatures in overloading and overriding.

Uploaded by

truptinik27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Inheritance:

Reusing parent class properties into child class is called as inheritance.

Consider in a project we have loan model in which there are three classes as
given below:

Class HomeLoan{ Class PersonalLoan { Class VehicleLoan{


10 common methods 10 common methods 10 common methods
} } }

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:

Class Loan{ Class HomeLoan Class Class


extends Loan{ PersonalLoan VehicleLoan
10 common extends Loan { extends Loan{
methods }
} }
}

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.

class Demo{ Here all the members of Demo class


int a = 10; are available to Test class.
public void m1(){
System.out.println("m1 When we call t.a ,JVM will search ‘a’
method"); variable in Test class first , if don’t
} get , it will search in Demo class
} because Demo class member are
available to Test class.
class Test extends Demo{
Similarly When we call t.m1(), Jvm
public static void will search m1() first in Test class , if
main(String[] args) { don’t get there , it will call from
Test t = new Test(); Demo class.
System.out.println(t.a);
t.m1();
}
}

When we create object of a class , it allocates memory for same class as


well as it’s parent class non static variables. But parent class object will not
create.
class Demo{
int a = 10;
}
a
10
class Test extends Demo{
int b = 20;
public static void b 20
main(String[] args) {
Test t = new Test();
System.out.println(t.a);
System.out.println(t.b); 1010
}
}
t 1010

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 Demo extends Test,Test1{} Error: Multiple inheritance

Why java does not support multiple inheritance?


Java does not support multiple inheritance because of ambiguity problem i.e
if child class is trying to access a member which is present in both parent
class then jvm will get confused from where it should access that member.
Consider following case:

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;
}

class Test extends Test1{


int b = 20; Test
}

class Demo extends Test{


public static void main(String[] Demo
args) {
Demo d = new Demo();
Here all the members of Test1 and
System.out.println(d.b);
Test class are available to Demo
System.out.println(d.a);
class.
}
}

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:

class Demo { class Demo {


public void m1(){ public void m1(int b){
System.out.println("zero System.out.println("zero
param"); param");
} }
public void m1(int a){ public void m1(int a){
System.out.println("int System.out.println("int
param"); param");
} }
public static void }
main(String[] args) {
Demo d = new Demo(); Here we will get error because both
d.m1(); the methods have same signature.
d.m1(10);
}
}

Why overloading is called as compile time polymorphism?


Overloading is also called as compile time polymorphism because in
overloading takes place at compile time and depends on reference type.
Overloading is also called as static polymorphism or early binding.
Overriding:
When child class is not satisfied with parent class implementation,
child class will redefined that method in it’s own class.

Redefining parent class method into child class is called as overriding.


In overriding method signature should be same.

class Test { class Demo extends Test{


public void m1(){ @Override
public void m1() {
System.out.println("parent
method"); System.out.println("child
} method");
} }
}

Why method overriding is also called as run time polymorphism?


In method overriding, method resolution take place at run time and
depends on object type, so it is called as run time polymorphism.
Overriding is also called as dynamic polymorphism or late binding.

You might also like