Java Inheritance | Java Inheritance Program Example | Java Inheritance Tutorial | Simplilearn

Simplilearn
3 Mar 202014:01

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

00:00

📝 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.

05:01

😎 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.

10:01

💻 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

Inheritance refers to when one class acquires the properties and behaviors of another class. As explained in the video, when the Mobiles class is set as a superclass, the Samsung, Xiaomi and Nokia classes can inherit its variables like cost, color and RAM. This allows code reuse and modeling of real-world relationships.

💡Subclass

A subclass is a class that inherits from another class. As mentioned in the example, Samsung, Xiaomi and Nokia are subclasses that inherit from the Mobiles superclass and the Android class.

💡Single inheritance

Single inheritance is when a subclass inherits from one superclass. As demonstrated in the code, the Samsung class inherits from the Android class through extends Android. This allows Samsung to access the androidVersion variable.

💡Multiple inheritance

Multiple inheritance is when a derived class inherits from multiple base classes. The video shows how the Android class inherits from Mobiles, and Samsung further inherits from Android. So Samsung gains access to variables in both parent classes.

💡Method overriding

Though not explicitly named in the video, method overriding occurs when subclasses provide their own implementation of methods defined in parent classes. This is seen with the use of getters and setters in the code.

💡Code reuse

A key benefit of inheritance demonstrated in the code is code reuse. The Samsung, Nokia and Xiaomi classes reuse Mobiles and Android code without needing to rewrite variables and methods.

💡Abstraction

Abstraction refers to exposing only essential details and hiding complexity. The use of inheritance in the video abstracts lower-level details of features like cost, color etc into the parent Mobiles class.

💡Encapsulation

By making variables and methods private and accessing them through getters/setters, encapsulation is achieved. This is applied in the code to control access to class state.

💡Polymorphism

Polymorphism allows subclasses to define their own versions of methods. This is used with getters and setters overridden in subclasses inheriting the Mobiles class.

💡Hierarchical inheritance

Hierarchical inheritance is when multiple subclasses inherit from the same superclass. This is demonstrated through Samsung, Nokia and Xiaomi all inheriting capabilities from the Android class.

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

play00:07

hello guys this is lakshya from simple

play00:09

learn and welcome to this tutorial on

play00:11

java inheritance this is a part of our

play00:13

java tutorial series where we will cover

play00:16

everything you need to know to master

play00:17

java now without wasting any time let's

play00:20

move on to our topic now let's see what

play00:22

we are going to learn today first we

play00:24

will see what inheritance means then the

play00:26

types of inheritance that are supported

play00:29

by java and finally we will see real

play00:31

time programs on each of the types of

play00:33

inheritance now let's see what is

play00:35

inheritance when one class inherits some

play00:38

properties and attributes of other class

play00:40

it is known as inheritance let's have a

play00:43

look at an example

play00:44

in this example mobiles is of superclass

play00:47

that have variables like cost color and

play00:49

ram the classes that inherits the

play00:51

variables and methods of mobiles are

play00:54

samsung xiaomi and nokia these classes

play00:57

are known as subclass now let's see the

play01:00

types of inheritance in java first we

play01:03

have single inheritance in single

play01:05

inheritance one class inherits features

play01:07

from another class as you can see above

play01:10

class b inherits all properties of class

play01:12

a this is a simple concept of

play01:14

inheritance next we have multi-level

play01:17

inheritance a derived class inherits

play01:19

from base class and derived class also

play01:22

act as a base class to other class

play01:24

sounds complicated right now see this

play01:27

example class b is derived from class a

play01:30

then class c is derived from class b

play01:33

because of this class c contains

play01:35

properties of both class a and b this is

play01:38

very effective when you go for

play01:39

inheritance in big programs and lastly

play01:42

we have hierarchical inheritance in this

play01:45

one class serves as base class and two

play01:48

or more classes serves as derived class

play01:51

as you can see above class b class d and

play01:54

class c have a single base class a

play01:57

so three classes inherits properties of

play01:59

single class this is called hierarchical

play02:02

inheritance now to understand these all

play02:05

concepts better let's use java code

play02:08

so now let's create a new project

play02:13

next

play02:15

next and i'm gonna name it as

play02:17

inheritance

play02:20

so we have our main program here

play02:22

so i'll just create a new class

play02:26

new java class and for now let me just

play02:29

name it as mobiles

play02:34

so what are the features in mobiles to

play02:36

start with

play02:37

the cost of the mobile

play02:40

the color of the mobile

play02:42

and what can we do it as or ram of the

play02:46

mobile

play02:47

i am making it as string because i'm

play02:49

gonna write gb as well so that's why

play02:52

string

play02:53

and now let's use getters and setters

play02:55

for this

play02:58

select all

play02:59

and okay

play03:01

so we have our first class mobiles here

play03:04

so our first type of inheritance was

play03:06

single inheritance so i'll create one

play03:08

more class

play03:10

and i'm gonna name it as android

play03:15

and i'll make android as extends okay

play03:19

i'll leave it for now

play03:21

so this mobile class we are going to use

play03:23

afterwards for now i'll use only android

play03:26

so

play03:29

let me make it as string

play03:31

android

play03:32

version

play03:34

and for this i'm gonna go for getter and

play03:37

setter

play03:40

so what is single level inheritance one

play03:42

class inherits from another class so

play03:44

i'll create one more program or i'll

play03:47

create a one more class and i'm gonna

play03:49

name it as

play03:50

samsung because samsung is an android

play03:53

phone and as well as it comes under

play03:55

mobiles so now i'm gonna extend it to

play03:58

android

play04:02

extends android so no need of writing

play04:05

anything inside it as soon as i write

play04:07

extends android i can use all the

play04:10

variables right now i have only android

play04:12

version as the variable

play04:14

so now let's go to main program

play04:18

and i'm right samsung s is equal to new

play04:23

samsung

play04:24

so i just created an object of it

play04:27

so now if i use s dot i'll get android

play04:30

version so i'll set android version as

play04:34

android

play04:36

pie

play04:38

and now to show it

play04:40

i'm gonna give it as

play04:43

samsung

play04:44

android version

play04:48

and here s dot

play04:50

get android version

play04:52

so now i have android class and a

play04:54

samsung class and i extended android

play04:57

class to the samsung so samsung can use

play05:00

all the variables here and i can even

play05:03

set the variables here so if i execute

play05:05

it

play05:09

and here you go you can see samsung

play05:11

android version that's android pie

play05:14

so without even declaring even a single

play05:16

variable or method here in samsung i use

play05:19

the variable of android and i did this

play05:22

so this is called as single level

play05:24

inheritance so next i'm gonna move it

play05:26

for multi-level inheritance so what is

play05:29

multi-level one class is derived from

play05:31

next and this class is derived from here

play05:34

so i have three classes right now so i'm

play05:37

gonna put android

play05:40

extends

play05:41

mobile

play05:43

so now android can use all the variables

play05:46

of mobiles i can use cost color or ram

play05:49

and even samsung that extends android

play05:52

can use all the variables from mobile

play05:55

so if i put variables for it

play05:58

s dot see i can get color cost or ram so

play06:02

i'm gonna go for set color

play06:05

i'm gonna give it as black

play06:08

and next

play06:09

s dot

play06:11

set cost cost i'm gonna give it as 30

play06:15

000 let it be dollar or rupees

play06:18

and the last one s dot

play06:21

ram

play06:22

i'm gonna give it as 4 gb

play06:26

so now if i print

play06:29

i'll just delete all this

play06:31

so now i can print them one by one so

play06:34

let's just do that

play06:40

so i'm gonna give it a samsung mobile

play06:42

details

play06:45

and then

play06:47

cost

play06:50

s dot get cost

play06:56

next i'm gonna give it as color

play07:00

plus s dot color

play07:03

sorry s dot get color

play07:08

and next last one i'm gonna give it as

play07:10

ram

play07:12

plus s dot get ram

play07:16

so now you can see i have not declared

play07:18

any variable inside samsung but i

play07:20

inherited all the variables from here

play07:22

mobiles and android so all the variables

play07:25

from mobiles are here and as per android

play07:29

if i put

play07:32

android version

play07:35

s dot get

play07:38

android version

play07:39

so now if i execute it i'll get all the

play07:42

values and as a result i'll get

play07:44

everything that i gave as the value

play07:47

so now let's just execute it

play07:50

so instead of cost i should put color

play07:55

so as you can see i have the cost of

play07:57

samsung mobile color and ram and even

play08:00

android version so all these variables i

play08:03

gave in mobile and android version i

play08:05

gave in android class

play08:08

but nothing in samsung class so this is

play08:10

how multi-level inheritance works so now

play08:13

let's move on to the third topic that

play08:15

was hierarchical inheritance so what is

play08:18

hierarchical inheritance in that three

play08:21

classes or two classes have a single

play08:23

base class so i'm gonna create two more

play08:25

classes that derives android so now

play08:28

let's create them

play08:31

next i'm gonna name it as nokia

play08:34

and the last one

play08:37

xiaomi

play08:40

so these are real time examples three

play08:42

mobiles we have three different

play08:44

companies and all uses android and they

play08:47

all come under mobiles

play08:49

so now in everything i'm gonna write

play08:51

extends android

play09:01

so this is the final version of

play09:02

inheritance that we have and this is

play09:04

very simple to understand but it might

play09:07

get complicated when we use big programs

play09:09

or very large programs for some project

play09:12

so now i have nothing inside class nokia

play09:15

or xiaomi or samsung i'm gonna use all

play09:18

the variables that come from android and

play09:20

mobiles as android extends mobiles so

play09:23

this is the concept like i have three

play09:25

classes three mobile companies that

play09:27

comes under android and we can use them

play09:29

to manipulate the variables and assign

play09:31

values to the variables so i already

play09:34

have everything from samsung

play09:37

so first let's create objects for all

play09:40

these classes

play09:42

nokia n is equal to new nokia

play09:48

and xiaomi x is equal to new

play09:53

xiaomi

play09:56

so now i'm going to assign variables to

play09:59

their values so now let's start with

play10:01

nokia

play10:03

dot

play10:04

set color

play10:06

let's give it as red

play10:10

n dot set cost i'm gonna give it as 40

play10:14

000

play10:15

again doesn't matter because it's a

play10:17

normal program so it doesn't matter

play10:18

whether it's dollar or rupees

play10:21

so next n dot

play10:24

set ram

play10:26

ram 4 gb

play10:30

and lastly n dot set android version

play10:37

android marshmallow

play10:42

so and in the end i'm gonna give x dot

play10:46

let's just copy paste it

play10:49

so it will be lot easier for

play10:51

me to code or for you to code

play10:57

so here x dot

play11:01

and color i'm gonna give it as

play11:05

silver

play11:08

rate 50 000 ram 6 gb

play11:12

and android let's give it some old

play11:15

so

play11:16

get cat

play11:18

so now we have set all the values to the

play11:20

variables so now let's print them as

play11:23

well so again i'm gonna copy paste all

play11:25

those details

play11:30

instead of samsung nokia

play11:32

here it gets n

play11:37

n

play11:38

instead of s

play11:43

and now again copy paste

play11:49

now xiaomi

play11:53

x dot

play11:55

x dot

play11:58

x

play11:59

and finally x so with this i have

play12:02

completed the program i can show you all

play12:05

the

play12:06

values that i gave in the starting let's

play12:08

just revise it

play12:10

so in mobiles i gave all the variables

play12:13

cost color and ram android i extended

play12:16

from mobiles and i gave one more

play12:18

variable as android version

play12:20

nokia xiaomi and samsung i gave nothing

play12:22

but i only extended android

play12:25

so now let's execute it

play12:30

so we have successfully executed our

play12:32

program let's have a look at it

play12:35

so here samsung mobile details in the

play12:37

starting color cost

play12:39

after that

play12:40

nokia mobiles and in the end xiaomi

play12:43

mobile and everything that we gave as

play12:46

the value are here so with this we have

play12:49

completed our program i hope you

play12:50

understood what inheritance is so i'll

play12:53

just revise it one more time so in

play12:55

single level inheritance i used only

play12:58

samsung android and mobiles and samsung

play13:01

i derived from android and i used only

play13:04

android version in the starting next i

play13:06

used multi-level inheritance in that i

play13:09

used mobile's android and samsung

play13:12

with the use of mobile variables

play13:15

i use samsung and i printed the values

play13:18

of color cost ram and android version as

play13:20

samsung

play13:21

in the next that is hierarchical

play13:23

inheritance i used nokia xiaomi and

play13:26

samsung

play13:27

so i hope you understood what the

play13:29

concept of inheritance means so this

play13:32

brings us to the end of this video we

play13:34

learnt everything about java inheritance

play13:36

and all its types using java code i hope

play13:39

you like the video thank you for

play13:41

watching and stay tuned for more from

play13:43

simply learn

play13:49

hi there if you like this video

play13:51

subscribe to the simply learn youtube

play13:52

channel and click here to watch similar

play13:55

videos turn it up and get certified

play13:57

click here