Pg1
Wednesday, June 10, 2020 12:05 PM
Relationships
is-a relationship:- one class is using a properties and beh
of another class with inheritance
ex:-all the properties of emp class inherited to testing
class
has-a relationship :-one class is using a properties and
behavior of another class without inheritance
ex:-encapsulated class and driver class
Inheritance
Taking all the properties and behavior of super
class to a sub class by using extends keyword is
called as inheritance
super class:-class which is providing all the properties and behaviors to a
child class
sub class:-class which is inheriting members from a super class
we are inheriting by using extends keyword
extends
syntax
class sub_class_name extends super_class_name
Inheritance Page 1
class sub_class_name extends super_class_name
explanation :- all the properties and behavior of super class is inherited to
sub class
NOTE:-if class doesn't extends any class then by default compiler will
extend Object class
note
constructer calling statement
this():-to call the constructer of same class
super():-to call the constructor immediate parent class constructor
1)one constructor can contain either one this() or one super()
2)CCS should be declared at the beginning
3)when there is no CCS then by default compiler will add super()
4)to use super() extends is mandatory(to use super()-classes should
follow is-a relationship)
object
ename
eid
sal
deptno
Emp
desg
work()
extends
domain
Inheritance Page 2
domain
TE
object
Emp
company
main()
method area
TE
main()
heap
object
object
work() main frame ename=punga
eid=1 ename=pungi
emp obj1= sal=35000 Emp eid=2
deptno=1 sal=30000 emp
TE obj2= desg="MTE" deptno=1
desg="MTE"
work() work()
object
domain="ecomerce"
stack memory/thread TE
Inheritance Page 3
extends
two-wheeler
showrrom
Inheritance Page 4
is it possible to inherit static members ? yes we can
extends keyword can inherit static and non static members
imp
prove that static members also inheritable by using extends?
driver:- school
create an object for HSS and PSS
Type of inheritance by using extends keyword
single level inheritance
Inheritance Page 5
multi level inheritance
hierarchical inheritance
multiple inheritance
hybrid /derived inheritance
Inheritance Page 6
super keyword : it is used to access a members of super class
we can't use super inside static context ,
Diamond problem
Inheritance Page 7
Diamond problem :- it is a major problem accurse when we
try to inherit more than one parent/super classes
problem-1 if both parent class contains default constructor
and if sub class calling parent class constructor with super() then
it will get confused whether to call which parent class
problem-2 if both parent/super class contains a parameterized
constructor with a same type of Formal Arg and from a sub class
if you try to call a parent class constructor by using super() with
a same actual arg -->again it will get confused whether to call
which parent class
problem-3 if both a parent class contains a members
(var/method) with a same name then from a sub class if we try
Inheritance Page 8
(var/method) with a same name then from a sub class if we try
to call a parent class member by using super
(super.member_name) then again it will get confused whether
to call which parent class
1) what is inheritance ?
flow defn
explain extends keyword
explain types
2) what is diamond problem
reasoon-1
reason-2
reason-3
3) difference between this() and super()
4) difference between this and super
Inheritance Page 9
Inheritance Page 10