Java Inheritance too Polymorphism
Java OOPS - Object Oriented Programming System
Four Fundamentals of OOPS
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
--------------------------------------------------------
i) Inheritance
> It is a procedure of Inheriting (reusing) the degree members (Variables too Methods) from 1 Class to another.
> The Class where the degree members are getting inherited is called every bit Super Class/Parent Class/Base Class
> The Class to which the degree members are getting inherited is called every bit Sub Class/Child Class/Derived Class.
> The Inheritance betwixt Super Class too Sub Class is achieved using "extends" keyword.
-----------------------------------------
Static vs. Non Static Methods
> Instance Variables can't survive used inward Static Methods, but nosotros tin purpose Static too Instance Variables
in Non Static methods.
> Non Static Methods can't survive called inside the Static Methods, but nosotros tin telephone call upward Static too Non Static Methods inside the Non Static Methods.
-------------------------------------------
Example:
public degree 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 amongst inward Static Method
}
public void abc4(){
System.out.println("It is a Non Static Method");
abc2();//Access Non Static method amongst inward Non Static Method
abc();//Access Static Method inside Non Static Method
}
public static void main(String[] args) {
abc();
abc3();
Class1 obj = novel 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 degree Class1 {
int a =10;
int b =20;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = novel Class1();
obj.add();
System.out.println(obj.a);
}
}
--------------------------------
Class 2 (With Inheritance, Create Object using Sub degree only)
public degree Class2 extends Class1{
public static void main(String[] args) {
Class2 obj2 = novel Class2();
obj2.add();
System.out.println(obj2.a);
}
}
--------------------------------
(Without Inheritance, Create Object using Super degree inward the same Package)
public degree Class2 {
public static void main(String[] args) {
Class1 obj2 = novel Class1();
obj2.add();
System.out.println(obj2.a);
}
}
--------------------------------
(Without Inheritance, Create Object using Super degree inward unopen to other Package yesteryear importing the Package)
import abcd.Class1;
public degree Class3 {
public static void main(String[] args) {
Class1 obj3 = novel Class1();
obj3.add();
}
}
--------------------------------
(With Inheritance, Create Object using Child degree inward unopen to other Package yesteryear importing the Package)
import abcd.Class1;
public degree Class3 extends Class1 {
public static void main(String[] args) {
Class3 obj3 = novel Class3();
obj3.add();
}
}
--------------------------------
(Without Inheritance, Create Object using Super Class inward unopen to other Project)
import abcd.Class1;
public degree Class4 {
public static void main(String[] args) {
Class1 obj4 = novel Class1();
obj4.add();
}
}
--------------------------------
(With Inheritance, Create Object using Child Class inward unopen to other Project)
import abcd.Class1;
public degree Class4 extends Class1 {
public static void main(String[] args) {
Class4 obj4 = novel Class4();
obj4.add();
}
}
--------------------------------
Example for Multi Level Inheritance
Class 1
public degree Class1 {
int a =10;
int b =20;
public void add(){
System.out.println(a + b);
}
public static void main(String[] args) {
Class1 obj = novel Class1 ();
obj.add();
}
}
--------------------------------
Class 2:
public degree 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 = novel Class2();
obj2.add();
}
}
--------------------------------
Class 3:
public void add(){
System.out.println(a + b);
}
public static void main(String[] args) {
Class3 obj3 = novel Class3();
obj3.add();//300
Class2 obj4 = novel Class2();
obj4.add();//3
Class1 obj5 = novel Class1();
obj5.add();//30
}
}
----------------------------------------------
ii) Polymorphism
Existence of Object deportment inward 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 ii or to a greater extent than Methods amongst same cite inward the same degree but they differ inward next ways.
a) Number of Arguments
b) Type of Arguments
--------------------------------
Example for Method Overloading:
public degree 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 = novel Class1();
obj.add(10, 20);//30
obj.add(10.234, 4.567);
obj.add(10, 20, 60);//90
}
}
--------------------------------------
Example2:
Class 1
public degree Class1 {
public void add(int a, int b){
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = novel Class1();
obj.add(10, 20);//30
}
}
-----------------------------------
Class 2:
public degree Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}
public static void main(String[] args) {
Class2 obj2 = novel Class2();
obj2.add();
obj2.add(10, 20);
}
}
--------------------------------
2) Run-Time Polymorphism / Method Overriding
If ii or to a greater extent than methods amongst same cite available inward the Super Class too Sub Class.
Example for Method Overriding
public degree Class1 {
public void add(){
int a =100, b=200;
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = novel Class1();
obj.add();
}
}
------------------------------
public degree Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}
public static void main(String[] args) {
Class2 obj2 = novel Class2();
obj2.add(); //25
}
}
----------------------------------
Java OOPS - Object Oriented Programming System
Four Fundamentals of OOPS
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
--------------------------------------------------------
i) Inheritance
> It is a procedure of Inheriting (reusing) the degree members (Variables too Methods) from 1 Class to another.
> The Class where the degree members are getting inherited is called every bit Super Class/Parent Class/Base Class
> The Class to which the degree members are getting inherited is called every bit Sub Class/Child Class/Derived Class.
> The Inheritance betwixt Super Class too Sub Class is achieved using "extends" keyword.
-----------------------------------------
Static vs. Non Static Methods
> Instance Variables can't survive used inward Static Methods, but nosotros tin purpose Static too Instance Variables
in Non Static methods.
> Non Static Methods can't survive called inside the Static Methods, but nosotros tin telephone call upward Static too Non Static Methods inside the Non Static Methods.
-------------------------------------------
Example:
public degree 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 amongst inward Static Method
}
public void abc4(){
System.out.println("It is a Non Static Method");
abc2();//Access Non Static method amongst inward Non Static Method
abc();//Access Static Method inside Non Static Method
}
public static void main(String[] args) {
abc();
abc3();
Class1 obj = novel 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 degree Class1 {
int a =10;
int b =20;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = novel Class1();
obj.add();
System.out.println(obj.a);
}
}
--------------------------------
Class 2 (With Inheritance, Create Object using Sub degree only)
public degree Class2 extends Class1{
public static void main(String[] args) {
Class2 obj2 = novel Class2();
obj2.add();
System.out.println(obj2.a);
}
}
--------------------------------
(Without Inheritance, Create Object using Super degree inward the same Package)
public degree Class2 {
public static void main(String[] args) {
Class1 obj2 = novel Class1();
obj2.add();
System.out.println(obj2.a);
}
}
--------------------------------
(Without Inheritance, Create Object using Super degree inward unopen to other Package yesteryear importing the Package)
import abcd.Class1;
public degree Class3 {
public static void main(String[] args) {
Class1 obj3 = novel Class1();
obj3.add();
}
}
--------------------------------
(With Inheritance, Create Object using Child degree inward unopen to other Package yesteryear importing the Package)
import abcd.Class1;
public degree Class3 extends Class1 {
public static void main(String[] args) {
Class3 obj3 = novel Class3();
obj3.add();
}
}
--------------------------------
(Without Inheritance, Create Object using Super Class inward unopen to other Project)
import abcd.Class1;
public degree Class4 {
public static void main(String[] args) {
Class1 obj4 = novel Class1();
obj4.add();
}
}
--------------------------------
(With Inheritance, Create Object using Child Class inward unopen to other Project)
import abcd.Class1;
public degree Class4 extends Class1 {
public static void main(String[] args) {
Class4 obj4 = novel Class4();
obj4.add();
}
}
--------------------------------
Example for Multi Level Inheritance
Class 1
public degree Class1 {
int a =10;
int b =20;
public void add(){
System.out.println(a + b);
}
public static void main(String[] args) {
Class1 obj = novel Class1 ();
obj.add();
}
}
--------------------------------
Class 2:
public degree 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 = novel Class2();
obj2.add();
}
}
--------------------------------
Class 3:
public void add(){
System.out.println(a + b);
}
public static void main(String[] args) {
Class3 obj3 = novel Class3();
obj3.add();//300
Class2 obj4 = novel Class2();
obj4.add();//3
Class1 obj5 = novel Class1();
obj5.add();//30
}
}
----------------------------------------------
ii) Polymorphism
Existence of Object deportment inward 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 ii or to a greater extent than Methods amongst same cite inward the same degree but they differ inward next ways.
a) Number of Arguments
b) Type of Arguments
--------------------------------
Example for Method Overloading:
public degree 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 = novel Class1();
obj.add(10, 20);//30
obj.add(10.234, 4.567);
obj.add(10, 20, 60);//90
}
}
--------------------------------------
Example2:
Class 1
public degree Class1 {
public void add(int a, int b){
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = novel Class1();
obj.add(10, 20);//30
}
}
-----------------------------------
Class 2:
public degree Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}
public static void main(String[] args) {
Class2 obj2 = novel Class2();
obj2.add();
obj2.add(10, 20);
}
}
--------------------------------
2) Run-Time Polymorphism / Method Overriding
If ii or to a greater extent than methods amongst same cite available inward the Super Class too Sub Class.
Example for Method Overriding
public degree Class1 {
public void add(){
int a =100, b=200;
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = novel Class1();
obj.add();
}
}
------------------------------
public degree Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}
public static void main(String[] args) {
Class2 obj2 = novel Class2();
obj2.add(); //25
}
}
----------------------------------