Choose any entity of your choice to represent a superclass. It must not be any of the following: - Human, Student, Vehicle, Person, Car, Animal, Book
• Create a class for your entity with the following: - 3 instance variables - 3 instance methods - 2 of the instance methods must show overloading - A default constructor - A parameterized constructor that initializes all instance variables - Getters and setters for the instance variables - Method to print all the details for the class objects with captions for the data.
• Create 2 subclasses for the superclass • Each subclass must have the following: - 2 instance variables - A constructor that first initializes its object variables using the superclass constructor, then initializes the rest. - Getters and setters - A method that overrides the print method in the superclass
• Create a test class and test the print methods in all the classes. java language.
Answer :
import java.io.*;
import java.lang.*;
class Book
{
//instance variables
String name;
String author_name;
double price;
//default constructor
Book()
{
System.out.println("Default constructor called");
}
//parameterized constructor to initialize all instance
variables
public Book(String name, String author_name, double price)
{
this.name=name;
this.author_name=author_name;
this.price=price;
}
//instance methods
public void story_book(String genre)
{
System.out.println("This is "+genre+" type story book");
}
public void story_book(String genre, int upper_age_limit) //method
overloading
{
System.out.println("This is "+genre+" type story book for upto
"+upper_age_limit+" years");
}
public void sub_book(String sub_name)
{
System.out.println("This is "+sub_name+" subject book");
}
// Getters and Setters
// Returns the name of this book
public String getName() {
return name;
}
// Returns the name of the author of this book
public String getAuthor() {
return author_name;
}
// Returns the price of this book
public double getPrice() {
return price;
}
// Sets the price of this book
public void setPrice(double price) {
this.price = price;
}
}
class TestBook
{
public static void main (String[] args)
{
//Test Book's constructor
Book book1 = new Book("The Secret Seven","Enid
Blyton",27.72);
//Test setters and getters
System.out.println("Book's name is: "+book1.getName());
System.out.println("Book author's name is:
"+book1.getAuthor());
System.out.println("Book's price is: "+book1.getPrice()+"
USD");
book1.setPrice(25.42);
System.out.println("Now, the book's price is: "+book1.getPrice()+"
USD");
//Test method overloading of instance methods
book1.story_book("Children detective");
book1.story_book("Children detective",18);
}
}
I hope this answer is helpful to you, Please Upvote(Thums Up) my answer. Thank you.
Get Answers For Free
Most questions answered within 1 hours.