Java Abstract Class Hackerrank Solution
For Explanation Watch Video:
Sample Input
A tale of two cities
Sample Output
The title is: A tale of two cities
Code:
import java.util.*;abstract class Book{ String title; abstract void setTitle(String s); String getTitle(){ return title; } }
//Write MyBook class hereclass MyBook extends Book{ 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(); }}
Comments
Post a Comment