Hacker Rank - Day 13 : Abstract Classes

                    Hacker Rank - Day 13 : Abstract Classes

Solution In Java 8:


import java.util.*;
abstract class Book
{
    String title;
    String author;
    Book(String t,String a){
        title=t;
        author=a;
    }
    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 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();
      
   }
}

Solution In Python 3: 

from abc import ABCMeta, abstractmethod
class Book(object, metaclass=ABCMeta):
    def __init__(self,title,author):
        self.title=title
        self.author=author   
    @abstractmethod
    def display(): pass

class MyBook(Book):
    def __init__(self,title,author,price):
        self.price = price ;
        super(MyBook,self).__init__(title,author);
        
        
    def display(self):
        print("Title:",self.title)
        print("Author:",self.author)
        print("Price:",self.price)
title=input()
author=input()
price=int(input())
new_novel=MyBook(title,author,price)
new_novel.display()
Share:

0 comments:

Post a Comment