Java Inheritance | Java Inheritance Program Example | Java Inheritance Tutorial | Simplilearn
Summary
TLDRThe video tutorial explains inheritance in Java using multiple examples and classes. It starts with basic concepts of superclasses and subclasses to demonstrate single inheritance. Then it covers multi-level inheritance by extending classes in a chain and hierarchical inheritance with one superclass and multiple subclasses. Real-world mobile company examples are used to show how classes inherit properties and methods from their parent classes. The examples employ getters, setters and constructors to manipulate inherited variables and output details. The summary concludes that various inheritance mechanisms allow code reuse and reduce duplication in Java OOP.
Takeaways
- 😀 Inheritance is when one class inherits properties and attributes from another class
- 👨💻 The types of inheritance in Java: single, multi-level, and hierarchical
- 🔀 In single inheritance, one class inherits from one other class
- ⏩ In multi-level inheritance, features trickle down from parent to child to grandchild classes
- 🌳 Hierarchical inheritance is when multiple child classes inherit from one parent class
- 📝 Use the 'extends' keyword to establish an inheritance link between classes
- 🎯 Inheritance promotes code reuse by allowing derived classes to reuse base class code
- ⚙️ The subclass can override or extend the functionality of the superclass
- 😎 Getter and setter methods allow controlled access to class variables
- 📈 Inheritance is useful in large programs when you need to manipulate many classes
Q & A
What is inheritance in Java?
-Inheritance in Java is a mechanism where one class acquires the properties and behaviors of another class. The class that inherits properties is called subclass and the class whose properties are inherited is called superclass.
What are the different types of inheritance supported by Java?
-The three main types of inheritance supported by Java are: 1) Single inheritance - one class inherits from one superclass 2) Multi-level inheritance - inheritance over multiple levels 3) Hierarchical inheritance - multiple subclasses inheriting from one superclass
How does single level inheritance work in Java?
-In single level inheritance, one class inherits directly from one superclass. For example, class B inherits from class A. So class B can access all public and protected variables and methods of class A.
What is multi-level inheritance in Java?
-In multi-level inheritance, a derived class inherits from a base class and that derived class also acts as a base class to another derived class. For example, Class C inherits from Class B, and Class B inherits from Class A. So Class C can access variables and methods of both Class A and B.
Explain hierarchical inheritance in Java.
-In hierarchical inheritance, one base class has multiple subclasses inheriting from it. For example, Class A is a base class and Class B, Class C, Class D are subclasses derived from Class A. So Class B, C and D inherit the properties and behaviors of Class A.
What is the use of 'extends' keyword in Java?
-The extends keyword is used by a subclass to inherit properties and behaviors from a superclass in Java. When a class extends another class using extends keyword, it can access all the non-private variables and methods of the superclass.
How can a subclass access the variables and methods of a superclass in Java?
-A subclass can directly access the public and protected variables and methods of a superclass. For private variables, the subclass needs to use getter and setter methods defined in the superclass to access them.
Why is inheritance used in Java?
-Inheritance is used in Java for code reuse and to establish is-a relationship between classes. It helps to reduce duplication of code as common properties and behaviors can be defined in a superclass and shared with subclasses.
What cannot be inherited in Java?
-In Java, constructors and private members of a superclass cannot be inherited by subclasses. The subclasses can access private members only through methods of the superclass.
Can a class extend multiple classes in Java?
-No, Java does not support multiple inheritance of classes. A class can extend only one parent class using extends keyword. However, it can implement multiple interfaces.
Outlines
📝 Introduction to inheritance in Java
The paragraph introduces the concept of inheritance in Java. It explains that inheritance allows a class to inherit properties and attributes from another class. It provides an example with a superclass 'mobiles' having variables like cost, color and ram, and subclasses 'samsung', 'xiaomi' and 'nokia' inheriting those variables.
😎 Explaining types of inheritance
The paragraph explains different types of inheritance in Java - single, multi-level and hierarchical. It provides examples for each type. Single inheritance is when one class inherits from another. Multi-level is when a derived class inherits from base class and that derived class serves as base class for another. Hierarchical is when one base class is inherited by multiple derived classes.
💻 Java code demonstration of inheritance types
The paragraph shows Java code to demonstrate the different inheritance types - single, multi-level and hierarchical. It creates classes like 'mobiles', 'android', 'samsung', 'nokia', 'xiaomi' to show single, multi-level and hierarchical inheritance in action by extending classes and accessing inherited fields.
Mindmap
Keywords
💡Inheritance
💡Subclass
💡Single inheritance
💡Multiple inheritance
💡Method overriding
💡Code reuse
💡Abstraction
💡Encapsulation
💡Polymorphism
💡Hierarchical inheritance
Highlights
Inheritance is when one class inherits properties and attributes from another class
Types of inheritance in Java: single, multi-level, and hierarchical
Single inheritance - one class inherits from another class
Multilevel inheritance - derived class inherits from base class and derived class acts as base class to other class
Hierarchical inheritance - one class serves as base class and two or more classes serve as derived classes
Using Java code to demonstrate single inheritance between Mobile, Android and Samsung classes
Demonstrating multilevel inheritance between Mobile, Android and Samsung classes
Hierarchical inheritance shown with Mobile as base class, Android derived, and Nokia, Xiaomi, Samsung derived from Android
Single inheritance uses one base and one derived class
Multilevel inheritance shows variables inherited from base through intermediate to final derived class
Hierarchical inheritance has one base class and multiple derived classes
Inheritance avoids rewriting redundant code by extending classes
Derived classes automatically inherit variables and methods from base classes
Inheritance is very useful for large, complex programs
Video covers the concept and implementation of Java inheritance
Transcripts
hello guys this is lakshya from simple
learn and welcome to this tutorial on
java inheritance this is a part of our
java tutorial series where we will cover
everything you need to know to master
java now without wasting any time let's
move on to our topic now let's see what
we are going to learn today first we
will see what inheritance means then the
types of inheritance that are supported
by java and finally we will see real
time programs on each of the types of
inheritance now let's see what is
inheritance when one class inherits some
properties and attributes of other class
it is known as inheritance let's have a
look at an example
in this example mobiles is of superclass
that have variables like cost color and
ram the classes that inherits the
variables and methods of mobiles are
samsung xiaomi and nokia these classes
are known as subclass now let's see the
types of inheritance in java first we
have single inheritance in single
inheritance one class inherits features
from another class as you can see above
class b inherits all properties of class
a this is a simple concept of
inheritance next we have multi-level
inheritance a derived class inherits
from base class and derived class also
act as a base class to other class
sounds complicated right now see this
example class b is derived from class a
then class c is derived from class b
because of this class c contains
properties of both class a and b this is
very effective when you go for
inheritance in big programs and lastly
we have hierarchical inheritance in this
one class serves as base class and two
or more classes serves as derived class
as you can see above class b class d and
class c have a single base class a
so three classes inherits properties of
single class this is called hierarchical
inheritance now to understand these all
concepts better let's use java code
so now let's create a new project
next
next and i'm gonna name it as
inheritance
so we have our main program here
so i'll just create a new class
new java class and for now let me just
name it as mobiles
so what are the features in mobiles to
start with
the cost of the mobile
the color of the mobile
and what can we do it as or ram of the
mobile
i am making it as string because i'm
gonna write gb as well so that's why
string
and now let's use getters and setters
for this
select all
and okay
so we have our first class mobiles here
so our first type of inheritance was
single inheritance so i'll create one
more class
and i'm gonna name it as android
and i'll make android as extends okay
i'll leave it for now
so this mobile class we are going to use
afterwards for now i'll use only android
so
let me make it as string
android
version
and for this i'm gonna go for getter and
setter
so what is single level inheritance one
class inherits from another class so
i'll create one more program or i'll
create a one more class and i'm gonna
name it as
samsung because samsung is an android
phone and as well as it comes under
mobiles so now i'm gonna extend it to
android
extends android so no need of writing
anything inside it as soon as i write
extends android i can use all the
variables right now i have only android
version as the variable
so now let's go to main program
and i'm right samsung s is equal to new
samsung
so i just created an object of it
so now if i use s dot i'll get android
version so i'll set android version as
android
pie
and now to show it
i'm gonna give it as
samsung
android version
and here s dot
get android version
so now i have android class and a
samsung class and i extended android
class to the samsung so samsung can use
all the variables here and i can even
set the variables here so if i execute
it
and here you go you can see samsung
android version that's android pie
so without even declaring even a single
variable or method here in samsung i use
the variable of android and i did this
so this is called as single level
inheritance so next i'm gonna move it
for multi-level inheritance so what is
multi-level one class is derived from
next and this class is derived from here
so i have three classes right now so i'm
gonna put android
extends
mobile
so now android can use all the variables
of mobiles i can use cost color or ram
and even samsung that extends android
can use all the variables from mobile
so if i put variables for it
s dot see i can get color cost or ram so
i'm gonna go for set color
i'm gonna give it as black
and next
s dot
set cost cost i'm gonna give it as 30
000 let it be dollar or rupees
and the last one s dot
ram
i'm gonna give it as 4 gb
so now if i print
i'll just delete all this
so now i can print them one by one so
let's just do that
so i'm gonna give it a samsung mobile
details
and then
cost
s dot get cost
next i'm gonna give it as color
plus s dot color
sorry s dot get color
and next last one i'm gonna give it as
ram
plus s dot get ram
so now you can see i have not declared
any variable inside samsung but i
inherited all the variables from here
mobiles and android so all the variables
from mobiles are here and as per android
if i put
android version
s dot get
android version
so now if i execute it i'll get all the
values and as a result i'll get
everything that i gave as the value
so now let's just execute it
so instead of cost i should put color
so as you can see i have the cost of
samsung mobile color and ram and even
android version so all these variables i
gave in mobile and android version i
gave in android class
but nothing in samsung class so this is
how multi-level inheritance works so now
let's move on to the third topic that
was hierarchical inheritance so what is
hierarchical inheritance in that three
classes or two classes have a single
base class so i'm gonna create two more
classes that derives android so now
let's create them
next i'm gonna name it as nokia
and the last one
xiaomi
so these are real time examples three
mobiles we have three different
companies and all uses android and they
all come under mobiles
so now in everything i'm gonna write
extends android
so this is the final version of
inheritance that we have and this is
very simple to understand but it might
get complicated when we use big programs
or very large programs for some project
so now i have nothing inside class nokia
or xiaomi or samsung i'm gonna use all
the variables that come from android and
mobiles as android extends mobiles so
this is the concept like i have three
classes three mobile companies that
comes under android and we can use them
to manipulate the variables and assign
values to the variables so i already
have everything from samsung
so first let's create objects for all
these classes
nokia n is equal to new nokia
and xiaomi x is equal to new
xiaomi
so now i'm going to assign variables to
their values so now let's start with
nokia
dot
set color
let's give it as red
n dot set cost i'm gonna give it as 40
000
again doesn't matter because it's a
normal program so it doesn't matter
whether it's dollar or rupees
so next n dot
set ram
ram 4 gb
and lastly n dot set android version
android marshmallow
so and in the end i'm gonna give x dot
let's just copy paste it
so it will be lot easier for
me to code or for you to code
so here x dot
and color i'm gonna give it as
silver
rate 50 000 ram 6 gb
and android let's give it some old
so
get cat
so now we have set all the values to the
variables so now let's print them as
well so again i'm gonna copy paste all
those details
instead of samsung nokia
here it gets n
n
instead of s
and now again copy paste
now xiaomi
x dot
x dot
x
and finally x so with this i have
completed the program i can show you all
the
values that i gave in the starting let's
just revise it
so in mobiles i gave all the variables
cost color and ram android i extended
from mobiles and i gave one more
variable as android version
nokia xiaomi and samsung i gave nothing
but i only extended android
so now let's execute it
so we have successfully executed our
program let's have a look at it
so here samsung mobile details in the
starting color cost
after that
nokia mobiles and in the end xiaomi
mobile and everything that we gave as
the value are here so with this we have
completed our program i hope you
understood what inheritance is so i'll
just revise it one more time so in
single level inheritance i used only
samsung android and mobiles and samsung
i derived from android and i used only
android version in the starting next i
used multi-level inheritance in that i
used mobile's android and samsung
with the use of mobile variables
i use samsung and i printed the values
of color cost ram and android version as
samsung
in the next that is hierarchical
inheritance i used nokia xiaomi and
samsung
so i hope you understood what the
concept of inheritance means so this
brings us to the end of this video we
learnt everything about java inheritance
and all its types using java code i hope
you like the video thank you for
watching and stay tuned for more from
simply learn
hi there if you like this video
subscribe to the simply learn youtube
channel and click here to watch similar
videos turn it up and get certified
click here
5.0 / 5 (0 votes)