Hi welcome back here in this practical exercise we will be learning inheritance, which is one of the key concepts of object oriented programming. We will be creating three different classes, where child classes would inherit from parent classes, both having different methods. We will be calling method from main class using objects the child might have may have a varied copy of the parent objects. Just write import java.util.as quick, it will import important all the libraries and methods of the util library. And we need to write a Java dot Lang dot astrick and we'll import all the language libraries. Similarly, we could have java.io dot estrich when importing input For methods, we could write these things or comment that thing.
Some compilers already input those useful libraries. On for separate things like a scanner or different classes you have to include, like servlet on. Here we create a first class that is a parent class called alpha. It has a method called display alpha of return type void. It displays a message on the screen. System dot out dot print ln a write any message that you want to display here, because it's a method of alpha.
We will be interested in printing assembly alpha Similarly, we could create a different class called beta here, we should inherit alpha, so we need to write extends keyword. In order to inherit the name we need to create a class, which is a child class, which extends the pattern. So here alpha is pattern, we need to write alpha after the extends keyword. So, Rita is a child class alpha is a parent class here. We have another method in the child, our display data of the same return type void. make it public so that it will be accessible.
From outside to me. I like a striker System dot out dot print lm in order to display a simple message on the screen, colliders custom message here we want to display data Because it's omega dot beta, then we can create a mean class or the class name, make it public class, or the class name, or main, or the main block, public static void main string args. Then we can create objects of different classes void child and parent. Or if you want to access the parent, we could also access it using an object of the child. Because child has inherited certain traits of the parent be Tabby equals to male beta. It will be creating an object for beta class or child class.
We have called me display method off alpha By using the object of child, you could do so, because the method is being made public. It could be also called if the method was being made protected, so that our only inherited persons could call it a classical one if it was private, it could not be called China class to execute this contract, so it is displaying the correct message on the screen. method of parent class is accessible through object of child class. And this shows the inheritance is complete and we could incorporate inheritance. inheritance is useful in different cases. All Java Java library metrics are emitted from one or the other classes.
We don't use all of them directly. We don't need to directly write each logic in each program. We could inherit traits from our methods from an existing In class, we could use a feature a which will reduce the lines of code in our program and reduce the time or effort required to write a program. It is a useful concept. You can use this thing. Other different types of inheritance, multiple inheritance, multi level inheritance, hybrid inheritance and such more inheritances that we could use.
Try building your own solution