What is Abstract Class in Java?
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.
So if any other class extends abstract class then it must provide implementation of abstract method of that class otherwise you can also make child class as abstract.
Learn more about Abstract class :
See problem Description in HackeRank :
Solution Explanation :
In code editor, we already have Book class and Main method. We have to simply create new class MyBook that extends Book class.
As setTitle method is abstract in Book class, we must have to implement in our MyBook class if we extends Book class. So create method setTitle in MyBook class and set the title variable. And our solution is done.
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) {
this.title = s;
}
}
public class Main{
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());
sc.close();
}
}
Other articles you may like :
Comments
Post a Comment