Inheritance With Example
JAVA Programming Language Inheritance

Inheritance With Example

In this article, we are going to discuss the concept of Java Inheritance with examples. So, first, we should have some knowledge of Java and its concepts. So, let’s start the article.

Inheritance With Example:

Inheritance is the main concept of Java. It is the method used to create a hierarchy between one or more classes. It is used to inherit the properties, methods, and attributes of the superclass. All the properties are inherited or transferred to the child class or subclass also called the derived class.

/*Create a class named Movie that can be used with your video rental business. The Movie class
should have ID Number and movie title with appropriate getter() and setter() methods. Also
create an equal() method that overrides Object’s equals() method, where two movies are equal if
their ID number is identical. Next, create three additional classes named Action, Comedy, and
The drama is derived from a Movie. Finally, create an overridden method named calcLateFees()
that takes as input the number of days a movie is late and returns the late fee for that movie. Action movies have a late fee of Rs. 30/day, comedies are Rs. 20/day and dramas are Rs. 10/day. Test your classes from the main method.*/

The above is the Question that we have to program using Inheritance. We are going to discuss the question and implement them step by step. First, we have to create a superclass named Movie. In this class, we have two attributes Id Number of the movie and the name or title of the movie. After declaring these two attributes we should create 4 different methods 2 is for the getter and 2 is for the setter. Single Level Inheritance is used in this program.

After appropriate getter and setter methods, we have to create equal methods which mean we have to compare two movies to check whether they are equal or not. If both the id numbers are the same then print Two Movies Are Identical. Now create Action, Comedy, and Drama classes that have the extension of the Subclass named movie.

All the attributes and methods are now extended to all these child classes. It means that now all the methods and variables copy now in the child classes. But the original (parent class or base class or superclass) always remains the same. Superclass will not be affected. We have to set the values of the id number by using constructors.

For action, comedy, and drama we have to separately create the objects and set the values of the id numbers and movie titles. The function of the getter method is to return the value to the main function and in the main function, we have to declare the variables to store these values that are been sent to the main function from all of the above classes and simply print the values in the main.

The same is the process and logic for calculating the late fees simply return the late fees to the main function from the over-riden calcLatefees() method. And then print all these values. So, this is the logic of the whole program.

Source Code Inheritance With Example:

import java.util.*;
class Movie{
    String movieTitle;
    int idNumber;
    void setter(int id1, String mt){
        idNumber = id1;
        movieTitle = mt;
    }
    String getMovieTitle(){
        return movieTitle;
    }
    int getIdNumber(){
        return idNumber;
    }
    void setter2(int id, String movieName){
        idNumber = id;
        movieTitle = movieName;
    }
    String getMovieName(){
        return movieTitle;
    }
    int getId(){
        return idNumber;
    }
}
class Action extends Movie{
    Scanner sc = new Scanner(System.in);
    int days;
    int calcLateFees(){
        System.out.println("Enter The Days That The Movie Is Late ");
        days = sc.nextInt();
        return (days*30);
    }
}
class Comedy extends Movie{
    Scanner sc = new Scanner(System.in);
    int days;
    int calcLateFees(){
        System.out.println("Enter The Days That The Movie Is Late ");
        days = sc.nextInt();
        return (days*20);
    }
}
class Drama extends Movie{
    Scanner sc = new Scanner(System.in);
    int days;
    int calcLateFees(){
        System.out.println("Enter The Days That The Movie Is Late ");
        days = sc.nextInt();
        return (days*10);
    }
}


public class q1{
    public static void main(String[] args) {
    Movie ob1 = new Movie();
    ob1.setter(130, "RRR");
    int id1 = ob1.getIdNumber();
    String title1 = ob1.getMovieTitle();
    ob1.setter2(101, "Maula Jutt");
    int id2 = ob1.getId();
    String title2 = ob1.getMovieName();
    System.out.println("Name Of First Movie = "+title1+"\tId Of First Movie = "+id1+"\nName Of 2nd Movie = "+title2+"\tId Of 2nd Movie = "+id2);
    Action ob2 = new Action();
    ob2.setter(102, "Yeh Jawani Phir Nahi Aani");
    String title3 =ob2.getMovieTitle();
    int id3 = ob2.getIdNumber();
    ob2.setter2(134, "Shivaay");
    String title4 = ob2.getMovieName();
    int id4 = ob2.getId();
    System.out.println("Name Of First Action Movie = "+title3+"\tId Of First Action Movie = "+id3+"\nName Of 2nd Action Movie = "+title4+"\tId Of 2nd Action Movie = "+id4);
    Comedy ob3 = new Comedy();
    ob3.setter(95, "Dhamaal");
    String title5 = ob3.getMovieTitle();
    int id5 = ob2.getIdNumber();
    ob3.setter2(28, "Golmaal Unlimited");
    int id6 = ob3.getId();
    String title6 = ob3.getMovieName();
    System.out.println("Name Of First Comedy Movie = "+title5+"\tId Of First Comedy Movie = "+id5+"\nName Of 2nd Comedy Movie = "+title6+"\tId Of 2nd Comedy Movie = "+id6);
    Drama ob4 = new Drama();
    ob4.setter(111, "Kaisi Teri KhudGharzi");
    String title7 = ob4.getMovieTitle();
    int id7 = ob4.getIdNumber();
    ob4.setter2(112, "Woh Pagal Si");
    String title8 = ob4.getMovieName();
    int id8 = ob4.getId();
    System.out.println("Name Of First Drama = "+title7+"\tId Of First Drama = "+id7+"\nName Of 2nd Drama = "+title8+"\tId Of 2nd Drama = "+id8);
    int lateFees1 = ob2.calcLateFees();
    System.out.println("Late Fees Of Action Moive = "+lateFees1);
    int lateFees2 = ob3.calcLateFees();
    System.out.println("Late Fees Of Comedy Movies = "+lateFees2);
    int lateFees3 = ob4.calcLateFees();
    System.out.println("Late Fees Of Dramas = "+lateFees3);
    }
}

 

Output:

Inheritance With Example
1st OutPut
Inheritance With Example
Inheritance With Example

 

 

 

 

 

From the above program and images, we conclude how to implement the inheritance concept in Java. We recently published an article on How to print the Fibonacci series using arrays.

 

Leave a Reply

Your email address will not be published. Required fields are marked *