This post explains the HackerRank 30 Days of Code problem Abstract Classes step by step: the problem statement, input and output format, constraints, a sample test case and a working solution in Java that you can study and run.

Day 13: Abstract Classes
Aim
You will be able to know about the abstract class in this problem . Whats actually abstract class are in inheritance
So what is an abstract class :
An abstract class is a class which can have an abstract methods as well as defined methods, but it cannot be instantiated (which means you cannot create a new instance of it). For using an abstract class, you must create and instantiate a subclass that extends the abstract class. Any abstract methods declared in an abstract class must be implemented by its subclasses unless the subclass is also abstract.
Mission
Given a Book class and a Solution class, write a MyBook class that does the following:
Inherits from Book
Has a parameterized constructor taking these parameters:
string title
string author
int price
Implements the Book class' abstract display() method so it prints these 3 lines
Title:, a space, and then the current instance's title.
Author:, a space, and then the current instance's author.
Price:, a space, and then the current instance's price.
Note: As these classes are being written in the same file, you must not use an access modifier (e.g.: ) when declaring MyBook or your code will not execute.
Input Format
You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object.
Output Format
The void display() method should print and label the respective title,author, and price of the MyBook object's instance (with each value on its own line) like so:
Title: $title
Author: $author
Price: $price
Note: The $ is prepended to variable names to indicate they are placeholders for variables.
Sample Input
The following input from stdin is handled by the locked stub code in your editor:
The BillGates
Coding Humans
500
Sample Output
The style output should be printed by your display() method:
Title: The BillGates
Author: Coding Humans
Price: 500
Abstract Classes Solution in Java
import java.util.*;
abstract class Book {
String title;
String author;
Book(String title, String author) {
this.title = title;
this.author = author;
}
abstract void display();
}
class MyBook extends Book{
int price;
MyBook(String title,String author,int price ){
super(title,author);
this.price=price;
}
void display()
{
System.out.println("Title: "+title);
System.out.println("Author: "+author);
System.out.println("Price: "+price);
}
}
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String title = scanner.nextLine();
String author = scanner.nextLine();
int price = scanner.nextInt();
scanner.close();
Book book = new MyBook(title, author, price);
book.display();
}
}
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
Comments
Post a Comment