We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
abstract class Book {
String title;
abstract void setTitle(String s);
String getTitle() {
return title;
}
}
class MyBook extends Book {
// Implementing the abstract method setTitle
void setTitle(String s) {
this.title = s;
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of MyBook
MyBook myBook = new MyBook();
// Reading the title from input
Scanner scanner = new Scanner(System.in);
String title = scanner.nextLine();
scanner.close();
// Setting the title using the setTitle method
myBook.setTitle(title);
// Printing the title
System.out.println("The title is: " + myBook.getTitle());
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Abstract Class
You are viewing a single comment's thread. Return to all comments →
mport java.util.Scanner;
abstract class Book { String title; abstract void setTitle(String s); String getTitle() { return title; } }
class MyBook extends Book { // Implementing the abstract method setTitle void setTitle(String s) { this.title = s; } }
public class Main { public static void main(String[] args) { // Creating an instance of MyBook MyBook myBook = new MyBook();
}