Skip to main content

← Back to Past Papers

VNSGU BCA Sem 3: OOP & Data Structures (304) Practical Solutions - April 2025 Set B

Paper Information

AttributeValue
SubjectObject Oriented Programming and Data Structures
Subject Code304
SetB
Semester3
Month/YearApril 2025
Max Marks25
PaperView 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: publish contains metadata common to all publications (ID and Name).
  • Subclass: book inherits from publish and 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

  1. Create parent class publish:
    • Add fields: publisherId (int) and publisherName (String).
    • Create a parameterized constructor to initialize fields.
    • Create a method displayPublishDetails() to print publisher info.
  2. Create child class book:
    • Extend publish using the extends keyword.
    • Add unique fields: authorName (String), bookName (String), and price (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 call displayPublishDetails() to display parent info.
  3. Driver Program:
    • Read publisher and book inputs from the console.
    • Instantiate the book class.
    • 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
  1. 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 using super(...) as its very first statement. Failing to do so produces a compilation error.
  2. Naming Confusions: The question specifies lowercase publish and book. Use lowercase class names as requested to prevent examiners from marking down compliance, but keep Java variables distinct.
  3. Encapsulation Standard: Keep class attributes private or protected. Use protected inside parent classes when you want subclasses to access fields directly without getters.

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir