Exercise on Object Oriented Programming in Java

This article presents an Exercise on Object Oriented Programming in Java.

The following Exercise on Object Oriented Programming in Java will help you understand the concepts of classes, objects, encapsulation, static methods, polymorphism, method overriding, and inheritance. Also, the solution for each program is provided here.

  1. Create a class named Box with three attributes – width, height, and depth. Also, define a method that returns the volume of the box. The class should include appropriate constructors. Write code to create the objects of the Box class and call its methods.
  2. Implement the functionality of a Calculator by defining the appropriate static methods. Also include the methods to find the power of a number of type integer and double. Call these functions in the main() method.
  3. Create a class Author. Another class Book should contain the object of the class Author as its data member. The following are the data members of the Author class – author_name, email id, and gender. Also, create appropriate constructors for this class. Likewise, the Book class contains these methods – book_title, author (object of the Author class), book_price, and quantity_in_stock. It should also contain constructors, getter, and setter methods. Further, create another class that contains the main() method. Display the details of the Book in the main() method.
  4. Define a class called Animal containing the two methods named eat(), and sleep(). Also, create another class Bird, which is a subclass of the Animal class that inherits the methods of the Animal class. The Bird class defines a method fly(). Another class MyClass contains the main() method that creates the objects of the above two classes. Call the different methods of each class using these objects.

Exercise on Object Oriented Programming in Java – Encapsulation

  1. Create a file and define the class Person in it. Another file should contain the class Employee which is a subclass of the Person class. The file should contain the class TestEmployee. It defines the main() method. The person class contains a data member – name. The following attributes are there in the Employee class – salary, joining_year, and insurance_number. Define the appropriate constructors in both classes. Also, define the setter, and getter methods. Finally, display the employee details in the main() method.

Exercise on Object Oriented Programming in Java – Inheritance

  1. The following classes need to be defined. Firstly, create a class Fruit. After that, create its subclasses Banana, and Mango. The Fruit class should have two data members – name, and taste. While it has one method called eat() that displays the name and taste of the fruit. Further, override this method in subclasses and demonstrate the overriding.
  2. Create a class Shape containing three methods – area(), draw(), and erase(). Also, create its three subclasses – Circle, Rectangle, and Triangle. Demonstrate the runtime polymorphism using these classes.

Further Reading

Java Practice Exercise



You may also like...

Leave a Reply

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