so today we will discuss about the
oops concept----inheritance
has a relationship vs is a relationship
what do you mean by inheritance ?
inheritance is a oops concept and java support the oops concepts.
what is inheritance ?
acquiring the properties of parent class to the child class. extends is the keyword
use for inheritance.
we have different kind of inheritance
single level(parent----child)
multi level(grandparent----parent---child)
hierarchical(one parent having more than one child that is hierarchical
inheritance)
multiple----that we will discuss later on
what is the benefit of inheritance ?
with the help of child class object we can call the parent variable and parent
methods.
first of all you will keep the main method in that class only, which you want to
execute.
grandparent
parent
child-----------------------> you will keep the main method in the child class.
***********************************************************************************
***************************
is a relationship vs has a relationship
is a relationship--------------------------------inheritance means is a
relationship
has a relationship---------------------------
as of now we have seen we can create the object of a class
in the main method in the same class
we can create the object of a class in the different class also that is called has
a relationship.
in the different class of same package or different package.
***********************************************************************
Inheritance with constructor
we have two type of calls
1) implicit call----------------------> in the child class constructor by default
there is a keyword defined that is the super() keyword and that is calling the
parent class default constructor.
2) explicit call-------------------> if you want to call the parent parameterized
constructor then you will use the super keyword explicitly
what is the use of super keyword ?
super keyword is used for calling the parent parameterized constructor and super
will be the first statement in the child class constructor and we can't write more
than one super keyword in child class constructor.
this keyword is used for calling from one constructor to another with in same class
super keyword is used for calling parent class parameterized constructor from the
child class constructor.
Assignment1-----------------------------
using super and this keyword
Parent class 2 parameterized constructor
Parent class default constructor
Parent class 3 parameterized constructor
Parent class one parameterized constructor
child class 3 parameterized constructor
child class default constructor
child class 1 parameterized constructor
child class 2 parmaeterized constructor
internal child class-------------->this keyword
from child to parent------------->super keyword
internal parent class------------>this keyword
Inheritance practice
is a vs has a practice
inheritance with constructor practice---task
this()---------------it is used for calling constructor
this.methodname()------it is used for calling method
this.variablename-------it is used for calling variable
super()--------------used for calling constructor
super.methodname()--------used for calling method
super.variablename--------used for calling variable