Inheritance by VXL Computer Classes 2023-24
Inheritance by VXL Computer Classes 2023-24
1
3) What are the types of Inheritances (Whether Java supports or not) available in
Object-Oriented Programming Languages?
A) Single Inheritance
B) Multi-Level Inheritance, Hierarchical Inheritance
C) Multiple Inheritance, Hybrid Inheritance
D) All the above
5) When a Class inherits two superclasses (not in Java), it is called ____ inheritance.
A) Multilevel inheritance
B) Single Inheritance
C) Multiple Inheritance
D) None
8) To stop or block inheriting a given class, the ___ keyword is used before the class.
2
A) static
B) private
C) final
D) none of the above
10) What is the output of the below Java program with Constructors using Inheritance?
class Ant
{ Ant(String name)
{ System.out.print("Inside Ant(String) Constructor. ");}
}
class WildAnt extends Ant
{
WildAnt()
{ System.out.print("Inside WildAnt() Constructor. "); }}
public class Inheritance3
{
public static void main(String[] args)
{ WildAnt wa = new WildAnt(); } }
A) Inside WildAnt() Constructor.
B) Inside Ant(String) Constructor. Inside WildAnt() Constructor.
C) Inside WildAnt() Constructor. Inside WildAnt() Constructor.
D) Compiler error
3
14) What is the access modifier used to declare a member so that it is accessible only
within its own class and its subclasses?
A) private
B) protected
C) public
D) default
15) In Java, which keyword is used to prevent a method from being overridden in a
subclass?
A) final
B) static
C) abstract
D) private
18)In Java, which keyword is used to call the parent class constructor?
A) parent()
B) super()
C) base()
D) this()
4
Short Question
Note : Write comment in each program, write approx. 5 to 6 comments in each program
of 10 marks and 3 to 4 comments in 5 marks questions, no need to write variable
description.
Question 1. A class Demand contains the market demands of a product. Another class
Supply is derived from Demand which contains the production and supply details of the
product.
The details of the base class:
Class Name : Demand
Data member / instance variable:
PId : String to store the product id.
PName : String to store the product name.
PDemand : Integer to store the demand of the product.
Member functions / methods :
Demand(…) : parameterized constructor to initialize the data members
void display( ) : to display the member data.
The details of the derived class :
Class Name : Supply
Data member /instance variable:
PProduced : to store the amount of product produced.
Prate : to store the cost per unit.
Member functions / methods :
Supply(…) : parameterized constructor to initialize the data members of both the classes.
int amount( ) : returns the difference between the amount of demand [rate x demand] and
amount of produced [rate x produced].
void display( ) : to display all the details of a product with proper messages
(including the difference in amount) (apply method overriding).
5
Assume that the super class Demand has been defined. Using the concept of inheritance,
specify the class Supply giving details of the constructor, int amount( ) and void display( ). The
super class, main function and algorithm need NOT be written.
Answer.
Question 2. A super class VEHICLE stores the details of a vehicle. Another class CAR is
derived from VEHICLE which performs certain operations.
The details of the base class :
Class Name : VEHICLE
Data member/ instance variable :
String regnum : To store the registration number [alphanumeric].
char Permit : The national permit category [either N, S or L].
Member functions/ methods :
VEHICLE(…) : parameterized constructor to initialize the data members.
void Print( ) : to display the member data.
The details of the derived class :
Class Name : CAR
Data member/ instance variable :
long Price : to store the price of the car.
double Exc : to store the excise to be levied on the car.
Member functions/ methods :
CAR(…) : parameterized constructor to initialize the data members of the base and current
class. The excise amount (Exc) is initialized to null.
6
void CalEx( ) : calculates the excise amount as per the given chart.
Permit Excise Amount
N 22 % of Price
S 11% of Price
L 5% of Price
void Print( ) : to display all the details of both the classes with proper
messages (apply method overriding).
Specify the class CAR giving the details of its mentioned methods. Assume that class
VEHICLE is already present. The super class, main function and algorithm need NOT be
written.
Answer
Question 3.
Create two classes named Library and Issue. The class library will have the following
structure to store detail of the books. Another class Issue will inherit class Library purchase
that will store the number of days late in returning the book and fine calculated.
Class name : Library
Member data:
bookno : as Integer to store book number.
Bookname : as String to store the name of the book.
Studentname : as String to store the name of the student.
Member function:
Library(….) : parameterized constructor to give initial values to data members.
7
void display( ) : to display the member data
Class name : Issue
Member data :
daysLate : as Integer to store the number of days late to return the book.
fine : as Double to store the fine calculated.
Member function:
Issue(….) :parameterized constructor to give initial values to the Super class data members
void return_book( ): to receive date of return of the book from student and compute the fine if
no. of days are late according to the given rule otherwise no fine. Number of days late * 0.5
void display( ) : to display the detail of the student issued the book along with total amount as
fine (if applicable)
Assume that the super class Library has been defined. Using the concept of inheritance,
specify the class Issue giving details of the constructor, void return_book( ) and void display( ).
The super class, main function and algorithm need NOT be written.
Answer
Question 4.
A class Employee contains employee details and another class Salary calculates the
employee‟s net salary.
The details of the two classes are given below:
Class name : Employee
Data members :
empNo : stores the employee number.
empName : stores the employee name
empDesig : stores the employee‟s designation.
Member functions:
Employee(…) : parameterized constructor to assign values to data members.
8
void display( ) : display the employee details.
Class name : Salary Data members:
basic : float variable to store the basic pay.
Member functions:
Salary(…) : parameterized constructor to assign values to data members.
void calculate() : calculates the employee‟s net salary according to the following rules:
DA = 10% of basic
HRA = 15% of basic
Salary = basic + DA +HRA PF= 8 % of Salary
Net Salary = Salary –PF
Display the employee details and the Net salary.
Specify the class Employee giving details of the constructors and member function void
display(). Using the concept of inheritance specify the class Salary giving details of the
constructor and the member function void calculate (). The main function need not be written.
Question 5.
A super class Worker has been defined to store the details of a worker. Define a subclass
Wages to compute the monthly wages for the worker. The details/specifications of both the
classes are given below:
Class name : Worker
Data Members/instance variables:
Name : to store the name of the worker
Basic : to store the basic pay in decimals Member functions:
Worker (…) : Parameterized constructor to assign values to the instance variables.
void display ( ) : display the worker‟s details
Class name : Wages
Data Members/instance variables:
hrs : stores the hours worked
rate : stores rate per hour
wage : stores the overall wage of the worker
Member functions:
Wages (…) : Parameterized constructor to assign values to the instance variables of both the
classes
double overtime ( ) : Calculates and returns the overtime amount as (hours*rate)
void display ( ) : Calculates the wage using the formula
wage = overtime amount + Basic pay and displays it along with the other details.
Specify the class Worker giving details of the constructor () and void display ( ). Using the
concept of inheritance, specify the class Wages giving details of constructor ( ), double-
overtime () and void display (). The main () function need not be written.
INTERFACE
1. What is an interface in Java?
a. A class that cannot be instantiated
b. A blueprint for a class
c. A collection of methods without implementation
d. A keyword to define constants
10
11. Which of the following is a valid interface method declaration?
a. void method( );
b. private void method();
c. final void method();
d. static void method();
11