Java Inheritance too Polymorphism
Java OOPS- Object Oriented Programming System.
Four fundamentals OOPS:
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
-----------------------------
i) Inheritance
> It is a procedure of Inheriting(reusing) the bird members(Variables too Methods) from i bird to another.
> Non-static bird members alone tin hold upwards Inherited.
> The bird where the bird members are getting Inherited is called every bit Super class/Parent class/Base class.
> The bird to which the bird members are getting Inherited is called every bit Sub class/Child class/derived class.
> The Inheritance betwixt Super bird too Sub bird is achieved using "extends" keyword.
--------------------------------------------------------
How to practise Static bird members?
Using static Non-access modifier.
How to access Static bird members?
Using Class Name nosotros access Static bird members.
How to access Non static bird members?
Using Object/Instance nosotros tin access Non static bird members.
------------------------------------------------------
Example for accessing Static too Non-static Class members:
package package1;
public bird Class1 {
//Declare Static variables
static int a =10, b=20;
//Declare Non-static variables
int c=30, d=40;
//Create Static a method alongside returning a value
public static int add(){
int outcome = a+b;
return result;
}
//Create Static a method without returning whatsoever value
public static void multiply(){
System.out.println(a*b);
}
//Create a Non static method alongside returning a value
public int add2(){
int res = c + d;
return res;
}
//Create a Non static method without returning whatsoever value
public void multiply2(){
System.out.println(c*d);
}
public static void top dog (String [] args){
//Access Static Class Members(Using Class Name)
int x = Class1.add();
System.out.println(x);//30
System.out.println(Class1.a);//10
Class1.multiply();//200
//Access Non static bird members(Using Object)
Class1 obj = novel Class1();
int y = obj.add2();
System.out.println(y);//70
System.out.println(obj.c);//30
obj.multiply2();//1200
}
}
-----------------------------
Three types Inheritance
1) Single Inheritance
Example:
public Class ClassB extends Class Influenza A virus subtype H5N1 {
.
}
-----------------------
2) Multi bird Inheritance
Example:
public Class ClassB extends ClassA {
public Class ClassC extends ClassB {
----------------------------------
iii) Multiple Inheritance (* Java doesn't support)
Example:
public bird ClassB extends ClassA {
public bird ClassB extends ClassD {
.
.
}
}
--------------------------------------------
Example for Inheritance:
Class 1:
public bird ClassA {
int a =10;
int b =20;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassA objA = novel ClassA();
System.out.println(objA.a);//10
objA.add();//30
}
}
----------------------------
Class 2:
public bird ClassB extends ClassA{
int a =100;
int b =200;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassB objB = novel ClassB();
objB.add();//300
System.out.println(objB.a);//100
}
}
----------------------------
Class 3:
public bird ClassC extends ClassB {
int a =1;
int b=2;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassC objC = novel ClassC();
System.out.println(objC.a);
objC.add();
}
}
--------------------------------------
Example 2:
package package1;
public bird ClassX {
protected int a =10;
protected int b =20;
protected void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassX objX = novel ClassX();
System.out.println(objX.a);
objX.add();
}
}
---------------------------------
package package2;
import package1.ClassX;
public bird ClassZ extends ClassX{
public static void main(String[] args) {
ClassZ objZ = novel ClassZ();
objZ.add();
System.out.println(objZ.a);
}
}
-----------------------------
ii) Polymorphism
Existence of Object demeanour inwards many forms
There are 2 types of Polymorphism
1) Compile Time Polymorphism / Method Overloading
2) Run Time Polymorphism or Method Overriding
-----------------------------------
1) Compile Time Polymorphism / Method Overloading
If 2 are to a greater extent than methods having same advert inwards the same bird simply they differ inwards next ways.
a) Number of Arguments
b) Type of Arguments
-----------------------------
Example for Method OverLoading:
public bird MethodOverLoading {
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 void add(double a, double b, double c){
System.out.println(a+b+c);
}
public static void main(String[] args) {
MethodOverLoading obj = novel MethodOverLoading();
obj.add(10, 20);
obj.add(10, 20, 30);
obj.add(1.234, 2.456);
obj.add(1.234, 2.456, 3.567);
}
}
-----------------------------------------
2) Run Time Polymorphism or Method Overriding
If 2 are to a greater extent than methods alongside same advert available inwards the Super bird too Sub class.
Example for Method OverLoading:
Class 1:
public bird ClassNew1 {
public void myMethod(){
System.out.println("Selenium for Test Automation");
}
public static void main(String[] args) {
ClassNew1 obj = novel ClassNew1();
obj.myMethod();
}
}
-------------------------
Class 2:
public bird ClassNew2 extends ClassNew1{
public void myMethod(){
System.out.println("UFT for Test Automation");
}
public static void main(String[] args) {
ClassNew2 obj = novel ClassNew2();
obj.myMethod();//UFT for Test Automation
ClassNew1 obj2= novel ClassNew1();
obj2.myMethod();//Selenium for Test Automation
}
}
-----------------------------------------------
Java OOPS- Object Oriented Programming System.
Four fundamentals OOPS:
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
-----------------------------
i) Inheritance
> It is a procedure of Inheriting(reusing) the bird members(Variables too Methods) from i bird to another.
> Non-static bird members alone tin hold upwards Inherited.
> The bird where the bird members are getting Inherited is called every bit Super class/Parent class/Base class.
> The bird to which the bird members are getting Inherited is called every bit Sub class/Child class/derived class.
> The Inheritance betwixt Super bird too Sub bird is achieved using "extends" keyword.
--------------------------------------------------------
How to practise Static bird members?
Using static Non-access modifier.
How to access Static bird members?
Using Class Name nosotros access Static bird members.
How to access Non static bird members?
Using Object/Instance nosotros tin access Non static bird members.
------------------------------------------------------
Example for accessing Static too Non-static Class members:
package package1;
public bird Class1 {
//Declare Static variables
static int a =10, b=20;
//Declare Non-static variables
int c=30, d=40;
//Create Static a method alongside returning a value
public static int add(){
int outcome = a+b;
return result;
}
//Create Static a method without returning whatsoever value
public static void multiply(){
System.out.println(a*b);
}
//Create a Non static method alongside returning a value
public int add2(){
int res = c + d;
return res;
}
//Create a Non static method without returning whatsoever value
public void multiply2(){
System.out.println(c*d);
}
public static void top dog (String [] args){
//Access Static Class Members(Using Class Name)
int x = Class1.add();
System.out.println(x);//30
System.out.println(Class1.a);//10
Class1.multiply();//200
//Access Non static bird members(Using Object)
Class1 obj = novel Class1();
int y = obj.add2();
System.out.println(y);//70
System.out.println(obj.c);//30
obj.multiply2();//1200
}
}
-----------------------------
Three types Inheritance
1) Single Inheritance
Example:
public Class ClassB extends Class Influenza A virus subtype H5N1 {
.
}
-----------------------
2) Multi bird Inheritance
Example:
public Class ClassB extends ClassA {
public Class ClassC extends ClassB {
----------------------------------
iii) Multiple Inheritance (* Java doesn't support)
Example:
public bird ClassB extends ClassA {
public bird ClassB extends ClassD {
.
.
}
}
--------------------------------------------
Example for Inheritance:
Class 1:
public bird ClassA {
int a =10;
int b =20;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassA objA = novel ClassA();
System.out.println(objA.a);//10
objA.add();//30
}
}
----------------------------
Class 2:
public bird ClassB extends ClassA{
int a =100;
int b =200;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassB objB = novel ClassB();
objB.add();//300
System.out.println(objB.a);//100
}
}
----------------------------
Class 3:
public bird ClassC extends ClassB {
int a =1;
int b=2;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassC objC = novel ClassC();
System.out.println(objC.a);
objC.add();
}
}
--------------------------------------
Example 2:
package package1;
public bird ClassX {
protected int a =10;
protected int b =20;
protected void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassX objX = novel ClassX();
System.out.println(objX.a);
objX.add();
}
}
---------------------------------
package package2;
import package1.ClassX;
public bird ClassZ extends ClassX{
public static void main(String[] args) {
ClassZ objZ = novel ClassZ();
objZ.add();
System.out.println(objZ.a);
}
}
-----------------------------
ii) Polymorphism
Existence of Object demeanour inwards many forms
There are 2 types of Polymorphism
1) Compile Time Polymorphism / Method Overloading
2) Run Time Polymorphism or Method Overriding
-----------------------------------
1) Compile Time Polymorphism / Method Overloading
If 2 are to a greater extent than methods having same advert inwards the same bird simply they differ inwards next ways.
a) Number of Arguments
b) Type of Arguments
-----------------------------
Example for Method OverLoading:
public bird MethodOverLoading {
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 void add(double a, double b, double c){
System.out.println(a+b+c);
}
public static void main(String[] args) {
MethodOverLoading obj = novel MethodOverLoading();
obj.add(10, 20);
obj.add(10, 20, 30);
obj.add(1.234, 2.456);
obj.add(1.234, 2.456, 3.567);
}
}
-----------------------------------------
2) Run Time Polymorphism or Method Overriding
If 2 are to a greater extent than methods alongside same advert available inwards the Super bird too Sub class.
Example for Method OverLoading:
Class 1:
public bird ClassNew1 {
public void myMethod(){
System.out.println("Selenium for Test Automation");
}
public static void main(String[] args) {
ClassNew1 obj = novel ClassNew1();
obj.myMethod();
}
}
-------------------------
Class 2:
public bird ClassNew2 extends ClassNew1{
public void myMethod(){
System.out.println("UFT for Test Automation");
}
public static void main(String[] args) {
ClassNew2 obj = novel ClassNew2();
obj.myMethod();//UFT for Test Automation
ClassNew1 obj2= novel ClassNew1();
obj2.myMethod();//Selenium for Test Automation
}
}
-----------------------------------------------