DEV Community

Aniket pagedar
Aniket pagedar

Posted on

Inheritance in java using publisher

import java.util.Scanner;

class publisher1 {
String publisherName;
int publisherId;

public void getData() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter Publisher Name: ");
    publisherName = scanner.nextLine();
    System.out.print("Enter Publisher ID: ");
    publisherId = scanner.nextInt();
}
Enter fullscreen mode Exit fullscreen mode

public void showData() {
System.out.println("Publisher Name:" + publisherName);
System.out.println("Publisher ID: " + publisherId);
}
}

class Book extends publisher {
String bookName;
int bookId;
String authorName;

public void getData() {
    super.getData();
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter Book Name: ");
    bookName = scanner.nextLine();
    System.out.print("Enter Book ID: ");
    bookId = scanner.nextInt();
    scanner.nextLine(); // Consume the new line character
    System.out.print("Enter Author Name: ");
    authorName = scanner.nextLine();
}

public void showData() {
    super.showData();
    System.out.println("Book Name: " + bookName);
    System.out.println("Book ID: " + bookId);
    System.out.println("Author Name: " + authorName);
}
Enter fullscreen mode Exit fullscreen mode

}

public class publisher {
public static void main(String[] args) {
Book book = new Book();
book.getData();
book.showData();
}

public void showData() {
}

public void getData() {
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)