WEBVTT

00:00.000 --> 00:03.960
and method overriding.

00:03.960 --> 00:06.000
In this video, we will understand

00:06.000 --> 00:10.040
inheritance and ISA relationships.

00:10.040 --> 00:13.480
Explain and use method overriding.

00:13.480 --> 00:18.240
Apply superclass and subclass methods in examples.

00:18.240 --> 00:21.520
Inheritance in Java.

00:21.520 --> 00:24.920
Think of inheritance like a family tree in programming.

00:24.920 --> 00:29.160
A superclass is the parent and a subclass is the child.

00:29.160 --> 00:31.840
The child automatically gets the parent's features,

00:31.840 --> 00:35.360
meaning it can use the parent's variables and methods.

00:35.360 --> 00:39.680
In Java, we create a subclass using the extends keyword.

00:39.680 --> 00:43.240
For example, student extends person.

00:43.240 --> 00:45.640
This means student is a type of person.

00:45.640 --> 00:47.800
This is called in is a relationship.

00:47.800 --> 00:50.240
A student is a person.

00:50.240 --> 00:54.120
This helps us reuse code instead of writing everything again.

00:54.120 --> 00:57.680
But remember, not everything is is AA.

00:57.720 --> 01:00.600
For example, a car has an engine.

01:00.600 --> 01:05.120
That is a has a relationship, not inheritance.

01:05.120 --> 01:07.280
Method overriding.

01:07.280 --> 01:10.160
Sometimes a subclass needs to change how a method

01:10.160 --> 01:12.120
from the superclass works.

01:12.120 --> 01:14.760
This is called method overriding.

01:14.760 --> 01:17.040
We use the at override annotation

01:17.040 --> 01:19.520
to show that a method is being redefined.

01:19.520 --> 01:23.800
For example, the person class might have a method say hello.

01:23.800 --> 01:25.960
But in the student class, we can override it

01:25.960 --> 01:29.200
to print a different message.

01:29.200 --> 01:35.000
This lets each subclass have its own version of the same method.

01:35.000 --> 01:37.440
Super keyword.

01:37.440 --> 01:39.640
If the subclass still wants to use something

01:39.640 --> 01:44.240
from the superclass, like a constructor or method,

01:44.240 --> 01:46.760
we use the super keyword.

01:46.760 --> 01:50.880
For example, super dot say hello.

01:50.880 --> 01:53.160
Why is this useful?

01:53.160 --> 01:56.680
Inheritance makes code flexible, reusable, and easier

01:56.680 --> 01:58.280
to manage.

01:58.280 --> 02:01.720
Overriding helps us create polymorphic behavior.

02:01.720 --> 02:05.960
Different objects can respond differently to the same method.

02:05.960 --> 02:09.640
In short, inheritance lets a subclass reuse

02:09.640 --> 02:11.960
and extend a superclass.

02:11.960 --> 02:14.560
Method overriding lets a subclass change

02:14.560 --> 02:17.160
how inherited methods work.

02:17.160 --> 02:20.160
Together, they help us write clean, efficient,

02:20.160 --> 02:22.400
and powerful object-oriented code.

