Inheritance is a way to make a copy of an existing class as the starting point for another. In addition to the term subclass, inherited classes are also called.
Java supports class reuse through inheritance and composition. In this two-part miniseries we'll focus on inheritance, one of the fundamental concepts of object-oriented programming. First, you'll learn how to use the extends keyword to derive a child class from a parent class, invoke parent class constructors and methods, and override methods. Then, in, we'll tour java.lang.Object and its methods. Object is Java's ultimate superclass, from which every other class inherits. To complete your learning about inheritance, be sure to check out the Java 101 primer. You'll learn why composition is an important complement to inheritance, and how to use it to guard against issues with encapsulation in your Java programs.
![Inheritance program in java netbeans Inheritance program in java netbeans](/uploads/1/2/5/4/125438371/313801204.jpg)
Here we have created a class called Entertainment, where the common variables and methods from related classes Movie, Drama and Circus are included. These classes ( Movie, Drama and Circus) inherit those properties (variables and methods) from the class Entertainment. Here Entertainment is the super-class and the Movie, Drama and Cirucs are the sub-classes. In the main method, both the variables belonging the super-class and the sub-class can be accessed as if they belong to a single class.
E.g., On the Drama object ramayan, we are accessing the super-class variables name, numberOfArtists, releaseDate along with the sub-class ( Drama) variables writer and stageSetter. There is no difference in the way the super-class variables and the sub-class variables are accessed. Similarly there is no difference in accessing the methods belonging to super-class and sub-class. Also observe that we can access the methods from super-class in the sub-class as if they are in the same class. E.g., The printEntertainment method of Entertainment class is called in the print method of the Movie class. Add two more integer variables - totalNumberOfAudience and numberOfAudienceLiked.
Create a method getLikePercentage to calculate the like percentage. Like Percentage = 100.0. Number Of Audience Liked / Total Number Of Audience. Make this change in the super-class Entertainment and see how all the three sub-classes - Movie, Drama and Circus - automatically inherit those variables and methods. Change the printEntertainment method in the Entertainment class, to print the details of totalNumberOfAudience, numberOfAudienceLiked and the getLikePercentage. Also observe how making change in the super-class Entertainment impacts all the sub-classes.