Welcome back friends, we are learning object oriented programming with Java. And here in this lecture we're going to learn one of the key concept that is called inheritance. So let's start. Theoretically inheritance is a method by which a child class or derived class can inherit or use the methods and fields of a parent class. It is simple hierarchical model that we use between the class and classes to derive different objects and offer various functionality. A child may inherit different features and functionalities by either modifying adding certain things or removing certain features from the parent class.
There is a typical example here, there could be classes regarding shapes. So, the shape class would be parent of all the classes. A triangle could be derived from the shape, quadrilateral could be similarly derived from the shape right angled triangle is a kind of triangle, which is a child lots of triangles fill the rectangle is a child of quadrilateral and the square is a child of rectangle. So, this is a hierarchy and hierarchy followed by inheritance when we move up to a level or generalization towards generalization or towards the parent class or base class, it is called cost of inheritance. And when we move downward, it is a specialization or moving to the direct or child. So, here our parent class shape has two different methods called draw and it is which has certain generalized features or specifications or simply where we will differentiate declaration with each derived type class like circles And frankly, we have the same function same method inherited with little modifications regarding behavior of different type classes.
So, here is an example of inheritance how it has been done. So, we have a base class called employ having certain features and we have derived all type class called CUDA. The format of writing this inheritance is as we used to we need to use the keyword extends ESP n ds. So, we need to first write the name of the child class. Here is coder is a child class which extends from employee class employee is the base class. So, this is the format of writing and rest everything goes fine.
Here we have created an object called fee for the cooler class and we can access the Variables are different methods of the employ as well as coder class using the same object. An object from childhood could be used to access public or protected variables of the base class. So, here are types of inheritance that could be find in different programming languages. We have a single inheritance, when there is a single superclass and single derived class. We have hierarchy inheritance, when there is one parent and multiple child inheriting from the same single parent. We have multi level inheritance, where I made inheritance in the form of grandmother, mother and daughter.
We have three generations or more generations, each one inheriting from the preceding generation. Then we have hybrid inheritance, a combination of single hierarchical and multi level inheritances then we have a multi fold inheritance where there could be multiple base class or super classes, which may be not possible in Java these days. For however, it is supported by languages like c++ and mode. To avoid various conflicts we have we don't support multiple narratives. So how we can write a single narrative, this is a program for single letters. Here we have an animal class, we have a dog class which is are derived from anyone.
So this simple format of writing single intelligence we need to write extensive keyword once. Then we have multi level inheritance, where we have one base class, which has a derived class and this derived class is the base class of undergrad class. Animal class is the parent of dog class, and dog classes parent of a middle class. So this is how we can create objects of different age and save We have hierarchies in a test, where we have two child classes and one base class parent class or multiple inheritance is not supported in Java, two over simple conflicts. Suppose, there are the two parent classes and both parent class has a common child class or derived class. And both parent class have the same method method with same name, but with different definition and the child class object cause method of a parent class.
So, there is a confusion or ambiguity regarding which method of this class has been called. So, that's for the reason. Java does not support multiple inheritance. However, you can use the features of multiple classes using multi level inheritance. So this is about inheritance. We can use inheritance to drop, make scenarios.
We can win or different kinds of solutions. Using narrative it is a powerful tool and we can perform performing inheritance between different programs and different five tribal lingo solution. Till then keep learning and keep moving ahead