0% found this document useful (0 votes)
11 views7 pages

Java OOP Inheritance Practical Assignments

core java practice question with solutions.sem 5

Uploaded by

ot7sakshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Java OOP Inheritance Practical Assignments

core java practice question with solutions.sem 5

Uploaded by

ot7sakshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The Poona Gujrati Kelvani Mandal’s

Haribhai V. Desai College of Arts, Science and Commerce, Pune

DEPARTMENT OF COMPUTER SCIENCE

Academic Year: 2024-25

Class – [Link] (Comp. Sci.) Div -A and B

Subject- CS-355 Object oriented programming in Java-I

Practical ASSIGNMENT3 CHAPTER3- Inheritance and interfaces

Q. Write following java programs.


[Link] a Super class Customer (name, phone-number). Derive a class Depositor(accno , balance)
from Customer. Again, derive a class Borrower (loan-no, loan-amt) from Depositor. Write necessary
member functions to read and display the details of ‘n’customers.

2Write a Program to illustrate multilevel Inheritance such that country is inherited from continent.
State is inherited from country. Display the place, state, country and continent

3. Write a program to create parent class College(cno, cname, caddr) and derived class
Department(dno, dname) from College. Write a necessary methods to display College details.

[Link] a program to create an abstract class named Shape that contains two integers and an empty
method named printArea(). Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape. Each one of the classes contain only the method
printArea() that prints the area of the given shape. (use method overriding)

[Link] an abstract class 'Bank' with an abstract method 'getBalance'. Rs.100, Rs.150 and Rs.200 are
deposited in banks A, B and C respectively. 'BankA', 'BankB' and 'BankC' are subclasses of class 'Bank',
each having a method named 'getBalance'. Call this method by creating an object of each of the
three classes.

[Link] an Employee class with suitable attributes having getSalary() method, which returns salary
withdrawn by a particular employee. Write a class Manager which extends a class Employee,
override the getSalary() method, which will return salary of manager by adding traveling allowance,
house rent allowance etc.

[Link] an abstract class Staff with protected members id and name. Define a parameterized
constructor. Define one subclass OfficeStaff with member department. Create n objects of OfficeStaff
and display all details.

[Link] an abstract class “order” having members id, description. Create two subclasses
“PurchaseOrder” and “Sales Order” having members customer name and Vendor name respectively.
Definemethods accept and display in all cases. Create 3 objects each of Purchase Order and Sales
Order and accept and display details.

[Link] a program to using marker interface create a class Product (product_id,

product_name, product_cost, product_quantity) default and parameterized constructor. Create

objectsof class product and display the contents of each object and Also display the object

count.
import [Link].*;

interface MarkerInt {

class product implements MarkerInt {

int pid, pcost, quantity;

String pname;

static int cnt;

// Default constructor

product() {

pid = 1;

pcost = 10;

quantity = 1;

pname = "pencil";

cnt++;

// Parameterized constructor

product(int id, String n, int c, int q) {

pid = id;

pname = n;

pcost = c;

quantity = q;

cnt++;

[Link]("\nCOUNT OF OBJECT IS : " + cnt + "\n");

public void display() {

[Link]("\t" +pid + "\t" + pname + "\t" + pcost + "\t" + quantity);

class MarkerInterface {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number of Product : ");

int n = [Link]();
product pr[] = new product[n];

for (int i = 0; i < n; i++) {

[Link]("\nEnter " + (i + 1) + " Product Details :\n");

[Link]("Enter Product ID: ");

int pid = [Link]();

[Link]();

[Link]("Enter Product Name: ");

String pn = [Link]();

[Link]("Enter Product Cost:");

int pc = [Link]();

[Link]("Enter Product Quantity:");

int pq = [Link]();

pr[i] = new product(pid, pn, pc, pq);

[Link]("\n\t\t Product Details\n");

[Link]("\tId\tPname\tCost\tQuantity\n");

for (int i = 0; i < n; i++) {

pr[i].display();

[Link]();

[Link] a program to find the cube of given number using functional interface.

import [Link].*;

interface Cube

float cube();

class Draw implements Cube

public float cube()

{
[Link]("enter the number");

Scanner sc= new Scanner ([Link]);

float cu = [Link]();

double cue = cu*cu*cu;

[Link]("cube of no is:"+cue);

return 0;

public static void main(String args[])

Draw d = new Draw();

[Link]();

[Link] a program to create a super class Vehicle having members Company and price. Derive two
different classes LightMotorVehicle(mileage) and HeavyMotorVehicle (capacity_in_tons). Accept the
information for "n" vehicles and display the information in appropriate form. While taking data, ask
user about the type of vehicle first.

Solution for reference

import [Link].*;
class Vehicle
{
String company;
double price;
public void accept() throws IOException
{
[Link]("Enter the Company and price of the
Vehicle: ");
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
company=[Link]();
price=[Link]([Link]());
}
public void display()
{
[Link]("Company: "+company+" Price: "+price);
}

}
class LightMotorVehicle extends Vehicle
{
double mileage;
public void accept() throws IOException
{
[Link]();
[Link]("Enter the mileage of the vehicle: ");
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
mileage=[Link]([Link]());
}
public void display()
{
[Link]();
[Link]("Mileage: "+mileage);
}
}
class HeavyMotorVehicle extends Vehicle
{
double captons;
public void accept() throws IOException
{
[Link]();
[Link]("Enter the capacity of vehicle in tons: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
captons=[Link]([Link]());
}
public void display()
{
[Link]();
[Link]("Capacity in tons: "+captons);
}
}
class Sa3
{
public static void main(String [] args) throws IOException
{
int i;
[Link]("Enter the type of vehicle: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("[Link] Vehicle");
[Link]("[Link] Vehicle");
int ch=[Link]([Link]());
switch(ch)
{
case 1:
[Link]("Enter the number of Light vehicles: ");
int n=[Link]([Link]());
LightMotorVehicle [] l=new LightMotorVehicle[n];
for(i=0;i<n;i++)
{
l[i]=new LightMotorVehicle();
l[i].accept();
}
for(i=0;i<n;i++)
{
l[i].display();
}
break;
case 2:
[Link]("Enter the number of Heavy vehicles: ");
int m=[Link]([Link]());
HeavyMotorVehicle [] h=new HeavyMotorVehicle[m];
for(i=0;i<m;i++)
{
h[i]=new HeavyMotorVehicle();
h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
}
break;
}
}
}

Common questions

Powered by AI

Method overriding facilitates polymorphism by allowing subclasses to provide specific implementations of methods that are already defined in their superclass, thus enabling the calling of correct methods during runtime. This is essential for dynamic method binding, an aspect of runtime polymorphism in Java .

Interfaces with abstract methods are significant because they provide a contract that ensures implementing classes define specific behaviors, thus supporting abstract data typing and multiple inheritance in Java. Interfaces promote modularity and flexibility by allowing different implementations to be used interchangeably .

Abstract methods enhance the design of a hierarchical class structure by mandating that subclasses provide specific implementations, ensuring consistent behavior while allowing for diverse implementations. This enforces a template pattern, promoting extensibility and specialization in complex systems .

Encapsulation helps manage complexity in Java applications by restricting access to certain components and maintaining a controlled interface for interaction. This encapsulation enhances maintainability, helps in error reduction, and protects the internal state of objects from unintended interference .

Abstract classes offer the advantage of providing shared code among subclasses, including constructors, fields, and implemented methods, which interfaces cannot do. They are also better suited for partially defining behavior, whereas interfaces serve to define only method signatures .

Protected access is important within classes as it provides access to class members only within its own package or subclasses, thus facilitating inheritance while preserving encapsulation. It allows subclass-specific implementations without exposing class members to the world outside the package .

Creating subclasses enhances interoperability and code reuse by allowing new classes to inherit properties and behaviors of existing classes, which reduces duplication and promotes flexible architecture design. This feature allows for easy maintenance and growth of Java programs as new requirements emerge .

Java implements multilevel inheritance by allowing a class to inherit from a derived class, creating a chain of inheritance. Benefits include organized code, reusability of classes, and the ability to build complex relationships. Challenges of multilevel inheritance can include increased complexity and potential ambiguity in the method hierarchy .

Marker interfaces in Java, like the MarkerInt, do not contain methods but convey metadata about a class. In practical terms, they are used to signal specific behaviors or characteristics to the JVM or frameworks, indicating how to process certain objects, as illustrated by classes like Serializable .

Constructors are fundamental in ensuring objects are initialized to a valid state. A parameterized constructor allows specific values to be passed at the time of object creation, ensuring precise initialization. In contrast, a default constructor initializes objects with default settings, offering no opportunity to assign specific initial states .

You might also like