Java Abstract Class - HackerRank Solution

Java Abstract Class - HackerRank Solution

Hello Friends, How are you? Today I am going to solve the Java Abstract Class Hackerrank Problem with a very easy explanation. In this article, you will get more than one approach to solving this problem. So let's start-

{tocify} $title={Table of Contents}

Java Abstract Class HackerRank Solution

HackerRank Java Abstract Class Solution - Problem Statement

A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.

Following is an example of an abstract class:

abstract class Book{ String title; abstract void setTitle(String s); String getTitle(){ return title; } } {codeBox}

If you try to create an instance of this class like the following line you will get an error:

Book new_novel=new Book(); {codeBox}

You have to create another class that extends the abstract class. Then you can create an instance of the new class.

Notice that setTitle method is abstract too and has nobody. That means you must implement the body of that method in the child class.

In the editor, we have provided the abstract Book class and a Main class. In the Main class, we created an instance of a class called MyBook. Your task is to write just the MyBook class.

Your class mustn't be public.

Sample Input

A tale of two cities {codeBox}

Sample Output

The title is: A tale of two cities {codeBox}

Java Abstract Class - Hacker Rank Solution

Approach I: Abstract Class HackerRank Java Solution

import java.util.*;

abstract class Book {
    String title;

    abstract void setTitle(String s);

    String getTitle() {
        return title;
    }
}

class MyBook extends Book {
    @Override
    void setTitle(String s) {
        title = s;
    }
}

public class Main {

    public static void main(String[] args) {
        //Book new_novel=new Book(); This line prHMain.java:25: error: Book is abstract; cannot be instantiated
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        MyBook new_novel = new MyBook();
        new_novel.setTitle(title);
        System.out.println("The title is: " + new_novel.getTitle());
        sc.close();
    }
}

Approach II: HackerRank Abstract Class in Java Solution

import java.util.*;
abstract class Book
{
   String title;
   abstract void setTitle(String s);
   String getTitle()
   {
      return title;
   }
   
}

//Write MyBook class here
 class MyBook extends Book
{
    void setTitle(String s)
    {
        title = s;
    }
    
}

public class JavaAbstractClass
{
   
   public static void main(String []args)
   {
      Scanner sc=new Scanner(System.in);
      String title=sc.nextLine();
      MyBook new_novel=new MyBook();
      new_novel.setTitle(title);
      System.out.println("The title is: "+new_novel.getTitle());
      
   }
}

Approach III: HackerRank Java Abstract Class Solution

import java.util.Scanner;

abstract class Book {
	String title;
	String author;

	Book(String t, String a) {
		title = t;
		author = a;
	}

	abstract void display();

}

// Write MyBook Class
class MyBook extends Book {
	private int price;

	MyBook(String title, String author, int price) {
		super(title, author);

		this.price = price;
	}

	@Override
	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 sc = new Scanner(System.in);
		String title = sc.nextLine();
		String author = sc.nextLine();
		int price = sc.nextInt();
		Book new_novel = new MyBook(title, author, price);
		new_novel.display();

		sc.close();
	}
}

Approach IV: Java Abstract Class HackerRank Solution

//Java 8
// Declare your class here. Do not use the 'public' access modifier.
    // Declare the price instance variable
class MyBook extends Book{
    int price;
    

    /**   
    *   Class Constructor
    *   
    *   @param title The book's title.
    *   @param author The book's author.
    *   @param price The book's price.
    **/
    // Write your constructor here
    MyBook(String title, String author, int price)
    {
        super(title, author);
        this.price = price;
    }
    
    /**   
    *   Method Name: display
    *   
    *   Print the title, author, and price in the specified format.
    **/
    // Write your method here
    @Override
    void display(){
        System.out.println("Title: "+title+"\nAuthor: "+author+"\nPrice: "+price);
    }
}


Read Also: 

Disclaimer: The above Problem ( Java Abstract Class ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Java Abstract Class HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post