filmov
tv
Selenium automation testing course with Java- Inheritance in Java- class 7

Показать описание
You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.
class 7:
=========
Java Inheritance:-
==================
Inheritance in Java means one class (child) can take properties and behaviors from another class (parent).
Just like a child inherits features from their parents (like eyes, hair, etc.), in Java, a class can inherit methods and variables from another class.
Why Use Inheritance?
Code Reusability: No need to write the same code again and again.
Easy Maintenance: Changes in parent class automatically affect child classes.
Improves Code Structure: Logical grouping of common features.
Real-Life Analogy:
=================
In Real Life:
=============
A Father has a Car and a House.
The Son automatically gets access to the Car and House.
The Son can also have his own bike.
class Father {
String car = "BMW";
void showHouse() {
}
}
class Son extends Father {
String bike = "KTM";
}
public class Main {
public static void main(String[] args) {
Son son = new Son();
}
}
Keywords:
=========
extends -- Used to inherit from a class
class Child extends Parent {
// Inherits everything from Parent
}
Important Points:
=================
Concept Description
Parent / Super Class The class whose properties are inherited
Child / Sub Class The class that inherits the properties
extends keyword Used to establish inheritance
Single Inheritance Java supports only single inheritance with classes (one parent only)
Method Overriding Child class can modify parent class method behavior
Object Creation Use child class object to access both parent and child members
Types of inheritance:
======================
single inheritance
Multiple inheritance
Multilevel inheritance
Hybrid inheritance
Hierarchical inheritance
single inheritance:
===================
One class inherits from one superclass OR
Combination of one parent and one child class
Multiple Inheritance (Using Interfaces)
=======================================
Java does not support multiple inheritance with classes due to ambiguity (Diamond problem), but supports it with interfaces.
When one child class has more than 1 parent classes, there is a confusion creation for the main thread in calling the methods with the same names in the super classes, that's why it is not possible in java. Because of Ambiguity.
Note:-
It is not possible for classes, but possible for interfaces
Multilevel inheritance:(possible)
==================================
A class inherits from a class which is already a child class of another class OR
more than one parent will access one child in a tree like structure
Hierarchical inheritance(possible)
===================================
Multiple child classes inherit from a single parent class OR
If one parent class has more than one child classes, it is called as Hierarchical
Hybrid inheritance:(not possible)
==================================
Combination of more than one type of inheritance (like hierarchical + multiple). Not directly supported via classes but possible using interfaces
class 7:
=========
Java Inheritance:-
==================
Inheritance in Java means one class (child) can take properties and behaviors from another class (parent).
Just like a child inherits features from their parents (like eyes, hair, etc.), in Java, a class can inherit methods and variables from another class.
Why Use Inheritance?
Code Reusability: No need to write the same code again and again.
Easy Maintenance: Changes in parent class automatically affect child classes.
Improves Code Structure: Logical grouping of common features.
Real-Life Analogy:
=================
In Real Life:
=============
A Father has a Car and a House.
The Son automatically gets access to the Car and House.
The Son can also have his own bike.
class Father {
String car = "BMW";
void showHouse() {
}
}
class Son extends Father {
String bike = "KTM";
}
public class Main {
public static void main(String[] args) {
Son son = new Son();
}
}
Keywords:
=========
extends -- Used to inherit from a class
class Child extends Parent {
// Inherits everything from Parent
}
Important Points:
=================
Concept Description
Parent / Super Class The class whose properties are inherited
Child / Sub Class The class that inherits the properties
extends keyword Used to establish inheritance
Single Inheritance Java supports only single inheritance with classes (one parent only)
Method Overriding Child class can modify parent class method behavior
Object Creation Use child class object to access both parent and child members
Types of inheritance:
======================
single inheritance
Multiple inheritance
Multilevel inheritance
Hybrid inheritance
Hierarchical inheritance
single inheritance:
===================
One class inherits from one superclass OR
Combination of one parent and one child class
Multiple Inheritance (Using Interfaces)
=======================================
Java does not support multiple inheritance with classes due to ambiguity (Diamond problem), but supports it with interfaces.
When one child class has more than 1 parent classes, there is a confusion creation for the main thread in calling the methods with the same names in the super classes, that's why it is not possible in java. Because of Ambiguity.
Note:-
It is not possible for classes, but possible for interfaces
Multilevel inheritance:(possible)
==================================
A class inherits from a class which is already a child class of another class OR
more than one parent will access one child in a tree like structure
Hierarchical inheritance(possible)
===================================
Multiple child classes inherit from a single parent class OR
If one parent class has more than one child classes, it is called as Hierarchical
Hybrid inheritance:(not possible)
==================================
Combination of more than one type of inheritance (like hierarchical + multiple). Not directly supported via classes but possible using interfaces