filmov
tv
#6.1 Java Tutorial | Inheritance
Показать описание
Check out our courses:
Coupon: TELUSKO10 (10% Discount)
Coupon: TELUSKO10 (10% Discount)
Coupon: TELUSKO20 (20% Discount)
Udemy Courses:
For More Queries WhatsApp or Call on : +919008963671
In this lecture we are discussing :
1)What is inheritance?
2)With example and scenario we understand what is inheritance?
3)Multilevel inheritance
4)Type of relationship
#1
In OOPs inheritance is a process of inheriting the properties of one class to another class.
e.g suppose a class has some property and behaviour and want this behaviour and properties to new class with additional property
we are using inheritance to inherit the property of one class to another.
#2
Scenario based example
Suppose we hire a developer and want to made Calculator which can add
two number.
Step 1:
Developer A - created Calc class which has add method and provide .class file to me but not provide source code file.
class Calc {
public void add(int num1,int num2){
}
}
Now i removed that developer from his post and hire new developer but we want to add subtraction functionality to Calculator.
But problem is that if we have Calc class source file then we said to developer B to add subtraction method.
Step 2:
When we have source code file
then,
class CalcAdv{
public void add(int num1,int num2){
}
public void sub(int num1,int num2){
}
}
But developer A does not provide Calc source code we have only Calc .class file.
Now we have one concept of OOPs that can help the developer B.
Step 3:
Now we have concept of inheritance in OOPs
lets us use extending or inheriting method from Calc class.
//inheriting add method from calc
class CalcAdv extends Calc{
public void sub(int num1,int num2){
}
}
class Main{
public static void main(String []args){
CalcAdv obj=new CalcAdv();
}
}
:- This is know as single level inheritance
#3
Multilevel inheritance
class Calc{
public void add(int num1,int num2){
}
}
class CalcAdv extends Calc{
public void sub(int num1,int num2){
}
//Now we can use the property and methods of Calc also
//Single level inheritence
}
class CalcSuperAdv extends CalcAdv{
public void mul(int num1,int num2){
//But here you can use the property and methods of Calc as well as CalcAdv class
//multilevel inheritance we consider Calc, CalcAdv and CalcSuperAdv
}
}
Note:i) Multilevel inheritance allowed in java but Multiple inheritance not allowed in java we shall understand why this not allowed in next lecture
ii)Term- {super, parent, base}-- same meaning and {sub, child, derived}--same meaning
#4
Type of relationship
i)IS-A relationship means one class is a child of another class.
e.g
class Animal{
}
class Lion extends Animal{
//here we found Lion is a animal
}
ii)HAS-A relationship means one class has using property and method of other class but that class is not a parent of current class.
class Bank{
void withdrawMoney(){
}
}
class Hospital{
A obj=new A();
void accessMoney{
}
//here we can see a hospital has a bank access
}
More Learning :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Coupon: TELUSKO10 (10% Discount)
Coupon: TELUSKO10 (10% Discount)
Coupon: TELUSKO20 (20% Discount)
Udemy Courses:
For More Queries WhatsApp or Call on : +919008963671
In this lecture we are discussing :
1)What is inheritance?
2)With example and scenario we understand what is inheritance?
3)Multilevel inheritance
4)Type of relationship
#1
In OOPs inheritance is a process of inheriting the properties of one class to another class.
e.g suppose a class has some property and behaviour and want this behaviour and properties to new class with additional property
we are using inheritance to inherit the property of one class to another.
#2
Scenario based example
Suppose we hire a developer and want to made Calculator which can add
two number.
Step 1:
Developer A - created Calc class which has add method and provide .class file to me but not provide source code file.
class Calc {
public void add(int num1,int num2){
}
}
Now i removed that developer from his post and hire new developer but we want to add subtraction functionality to Calculator.
But problem is that if we have Calc class source file then we said to developer B to add subtraction method.
Step 2:
When we have source code file
then,
class CalcAdv{
public void add(int num1,int num2){
}
public void sub(int num1,int num2){
}
}
But developer A does not provide Calc source code we have only Calc .class file.
Now we have one concept of OOPs that can help the developer B.
Step 3:
Now we have concept of inheritance in OOPs
lets us use extending or inheriting method from Calc class.
//inheriting add method from calc
class CalcAdv extends Calc{
public void sub(int num1,int num2){
}
}
class Main{
public static void main(String []args){
CalcAdv obj=new CalcAdv();
}
}
:- This is know as single level inheritance
#3
Multilevel inheritance
class Calc{
public void add(int num1,int num2){
}
}
class CalcAdv extends Calc{
public void sub(int num1,int num2){
}
//Now we can use the property and methods of Calc also
//Single level inheritence
}
class CalcSuperAdv extends CalcAdv{
public void mul(int num1,int num2){
//But here you can use the property and methods of Calc as well as CalcAdv class
//multilevel inheritance we consider Calc, CalcAdv and CalcSuperAdv
}
}
Note:i) Multilevel inheritance allowed in java but Multiple inheritance not allowed in java we shall understand why this not allowed in next lecture
ii)Term- {super, parent, base}-- same meaning and {sub, child, derived}--same meaning
#4
Type of relationship
i)IS-A relationship means one class is a child of another class.
e.g
class Animal{
}
class Lion extends Animal{
//here we found Lion is a animal
}
ii)HAS-A relationship means one class has using property and method of other class but that class is not a parent of current class.
class Bank{
void withdrawMoney(){
}
}
class Hospital{
A obj=new A();
void accessMoney{
}
//here we can see a hospital has a bank access
}
More Learning :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Комментарии