VNSGU BCA Sem 3: OOP & Data Structures (304) Practical Solutions - April 2025 Set B
Paper Information
| Attribute | Value |
|---|---|
| Subject | Object Oriented Programming and Data Structures |
| Subject Code | 304 |
| Set | B |
| Semester | 3 |
| Month/Year | April 2025 |
| Max Marks | 25 |
| Paper | View Paper | Download PDF |
Questions & Solutions
Q1: Publisher & Book Inheritance
Max Marks: 20
Create a class publish which has members like publisher id and publisher name.
Create another class book with members like author name, book name, and price which is inherited from class publish.
Write a program to display information of both the classes.
1. Concept Explanation
What is Inheritance in OOP?
Inheritance is a key mechanism of Object-Oriented Programming that allows a new class (subclass/child class) to inherit features (fields and methods) of an existing class (superclass/parent class).
In this exercise:
- Superclass:
publishcontains metadata common to all publications (ID and Name). - Subclass:
bookinherits frompublishand adds attributes specific to a book (author, title, price).
Key Java Keywords
extends: Used to establish an inheritance relationship between classes.super: Used to refer to the immediate parent class. In a child constructor,super(...)is called to execute the constructor of the parent class.
2. Algorithm & Step-by-Step Logic
- Create parent class
publish:- Add fields:
publisherId(int) andpublisherName(String). - Create a parameterized constructor to initialize fields.
- Create a method
displayPublishDetails()to print publisher info.
- Add fields:
- Create child class
book:- Extend
publishusing theextendskeyword. - Add unique fields:
authorName(String),bookName(String), andprice(double). - Define a parameterized constructor that accepts parent and child fields and routes parent values using
super(publisherId, publisherName). - Define a method
displayBookDetails()to print book-specific info and calldisplayPublishDetails()to display parent info.
- Extend
- Driver Program:
- Read publisher and book inputs from the console.
- Instantiate the
bookclass. - Invoke display methods to output information.
3. Implementation Code & Output
View Solution & Output
// 📚 VNSGU BCA Sem 3 - OOP & Data Structures (Practical 304)
// 🚀 Action: Establish inheritance between publish and book classes, initialize and print data.
import java.util.Scanner;
// Superclass
class publish {
// Fields
protected int publisherId;
protected String publisherName;
// Constructor
public publish(int publisherId, String publisherName) {
this.publisherId = publisherId;
this.publisherName = publisherName;
}
// Method to display publisher information
public void displayPublishDetails() {
System.out.println(" Publisher ID : " + publisherId);
System.out.println(" Publisher Name: " + publisherName);
}
}
// Subclass inheriting from publish
class book extends publish {
// Unique Fields
private String authorName;
private String bookName;
private double price;
// Constructor
public book(int publisherId, String publisherName, String authorName, String bookName, double price) {
// Initialize superclass attributes using super()
super(publisherId, publisherName);
// Initialize subclass attributes
this.authorName = authorName;
this.bookName = bookName;
this.price = price;
}
// Method to display all details (both classes)
public void displayAllDetails() {
System.out.println("\n=============================================");
System.out.println(" BOOK INFORMATION ");
System.out.println("=============================================");
System.out.println(" Book Title : " + bookName);
System.out.println(" Author Name : " + authorName);
System.out.printf(" Price : Rs. %.2f\n", price);
System.out.println("---------------------------------------------");
// Call superclass method to print inherited details
super.displayPublishDetails();
System.out.println("=============================================");
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- ENTER PUBLISHER DETAILS ---");
System.out.print("Enter Publisher ID : ");
int pubId = scanner.nextInt();
scanner.nextLine(); // Flush input buffer
System.out.print("Enter Publisher Name: ");
String pubName = scanner.nextLine();
System.out.println("\n--- ENTER BOOK DETAILS ---");
System.out.print("Enter Book Title : ");
String bookName = scanner.nextLine();
System.out.print("Enter Author Name : ");
String author = scanner.nextLine();
System.out.print("Enter Price : ");
double price = scanner.nextDouble();
// Create Subclass object (routes parent attributes automatically)
book myBook = new book(pubId, pubName, author, bookName, price);
// Display combined information
myBook.displayAllDetails();
scanner.close();
}
}
Expected Console Output:
--- ENTER PUBLISHER DETAILS ---
Enter Publisher ID : 1024
Enter Publisher Name: Pearson India
--- ENTER BOOK DETAILS ---
Enter Book Title : Introduction to Java Programming
Enter Author Name : Daniel Liang
Enter Price : 750
=============================================
BOOK INFORMATION
=============================================
Book Title : Introduction to Java Programming
Author Name : Daniel Liang
Price : Rs. 750.00
---------------------------------------------
Publisher ID : 1024
Publisher Name: Pearson India
=============================================
💡 Common Examination Mistakes & Tips
Key Pitfalls to Avoid
- Missing
super()Call: In Java inheritance, if a parent class doesn't have a default parameterless constructor, the child constructor must explicitly call the parent constructor usingsuper(...)as its very first statement. Failing to do so produces a compilation error. - Naming Confusions: The question specifies lowercase
publishandbook. Use lowercase class names as requested to prevent examiners from marking down compliance, but keep Java variables distinct. - Encapsulation Standard: Keep class attributes
privateorprotected. Useprotectedinside parent classes when you want subclasses to access fields directly without getters.