Java Inheritance and Polymorphism

Java Inheritance and Polymorphism

Java OOPS – Object Oriented Programming System

Four Fundamentals of OOPS

i) Inheritance

ii) Polymorphism

iii) Abstraction

iv) Encapsulation

i) Inheritance

> It is a process of Inheriting (reusing) the class members (Variables and Methods) from one Class to another.

> The Class where the class members are getting inherited is called as Super Class/Parent Class/Base Class

> The Class to which the class members are getting inherited is called as Sub Class/Child Class/Derived Class.

> The Inheritance between Super Class and Sub Class is achieved using “extends” keyword.
—————————————–
Static vs. Non Static Methods

> Instance Variables can’t be used in Static Methods, but we can use Static and Instance Variables
in Non Static methods.

> Non Static Methods can’t be called within the Static Methods, but we can call Static and Non Static Methods within the Non Static Methods.
——————————————-
Example:

public class Class1 {
//Static Variable
static int a =10;
//Instance Variable
int b=20;
//Static Method
public static void abc(){
System.out.println(a);
}
//Non Static Method
public void abc2(){
System.out.println(a + b);
}
public static void abc3(){
System.out.println(“It is a Staic Method”);
//abc2();// We can’t Access Non Static Methods
abc();//Access Static Method with in Static Method
}
public void abc4(){
System.out.println(“It is a Non Static Method”);
abc2();//Access Non Static method with in Non Static Method
abc();//Access Static Method within Non Static Method
}

public static void main(String[] args) {
abc();
abc3();

Class1 obj = new Class1();
obj.abc2();
obj.abc4();
}
}
——————————–
Types of Inheritance

i) Single Inheritance

Ex:

ClassB extends ClassA

ClassA – Super Class
ClassB – Sub Class

ii) Multi Level Inheritance

Ex:

ClassB extends ClassA
ClassC extends ClassB

ClassC – Sub-sub Class / Child Class
ClassB – Parent Class for ClassC, Child Class for ClassA
ClassA – Grand Parent Class for ClassC, Parent Class for ClassB

iii) Multiple Inheritance (* Java Doesn’t support)

Ex:

ClassB extends ClassA
ClassB extends ClassD
——————————–
Example:

Class 1

public class Class1 {
int a =10;
int b =20;

public void add(){
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add();
System.out.println(obj.a);
}
}
——————————–
Class 2 (With Inheritance, Create Object using Sub class only)

public class Class2 extends Class1{

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add();
System.out.println(obj2.a);
}
}
——————————–
(Without Inheritance, Create Object using Super class in the same Package)

public class Class2 {

public static void main(String[] args) {
Class1 obj2 = new Class1();
obj2.add();
System.out.println(obj2.a);
}
}
——————————–
(Without Inheritance, Create Object using Super class in another Package by importing the Package)

import abcd.Class1;

public class Class3 {

public static void main(String[] args) {
Class1 obj3 = new Class1();
obj3.add();
}
}
——————————–
(With Inheritance, Create Object using Child class in another Package by importing the Package)

import abcd.Class1;

public class Class3 extends Class1 {

public static void main(String[] args) {
Class3 obj3 = new Class3();
obj3.add();
}
}
——————————–
(Without Inheritance, Create Object using Super Class in another Project)

import abcd.Class1;

public class Class4 {

public static void main(String[] args) {
Class1 obj4 = new Class1();
obj4.add();
}
}
——————————–
(With Inheritance, Create Object using Child Class in another Project)

import abcd.Class1;

public class Class4 extends Class1 {

public static void main(String[] args) {
Class4 obj4 = new Class4();
obj4.add();
}
}
——————————–
Example for Multi Level Inheritance

Class 1

public class Class1 {
int a =10;
int b =20;

public void add(){
System.out.println(a + b);
}
public static void main(String[] args) {
Class1 obj = new Class1 ();
obj.add();
}
}
——————————–
Class 2:

public class Class2 extends Class1 {
int a =1;
int b =2;

public void add(){
System.out.println(a + b);
}

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add();
}
}
——————————–
Class 3:

public void add(){
System.out.println(a + b);
}

public static void main(String[] args) {
Class3 obj3 = new Class3();
obj3.add();//300

Class2 obj4 = new Class2();
obj4.add();//3

Class1 obj5 = new Class1();
obj5.add();//30

}
}

Java Inheritance and Polymorphism Tutorial

ii) Polymorphism

Existence of Object behavior in many forms

Two types of Polymorphism

1) Compile Time Polymorphism / Method Overloading

2) Run-Time Polymorphism / Method Overriding
——————————————–
1) Compile Time Polymorphism / Method Overloading

If two or more Methods with same name in the same class but they differ in following ways.

a) Number of Arguments

b) Type of Arguments
——————————–
Example for Method Overloading:

public class Class1 {
public void add(int a, int b){
System.out.println(a+b);
}
public void add(int a, int b, int c){
System.out.println(a+b+c);
}
public void add(double a, double b){
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add(10, 20);//30
obj.add(10.234, 4.567);
obj.add(10, 20, 60);//90
}
}
————————————–
Example 2:

Class 1

public class Class1 {

public void add(int a, int b){
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add(10, 20);//30
}
}
———————————–
Class 2:

public class Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add();
obj2.add(10, 20);
}
}
——————————–
2) Run-Time Polymorphism / Method Overriding

If two or more methods with same name available in the Super Class and Sub Class.

Example for Method Overriding

public class Class1 {

public void add(){
int a =100, b=200;
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add();
}
}
——————————
public class Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add(); //25
}
}
———————————-

(265)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *