AZ OOP 4
AZ OOP 4
No URK23CO2018
Aim
and build java application using Java’s Inheritance Concepts such as Super class, Sub classes,
Description
Inheritance in java is a mechanism in which one object acquires all the properties and
Syntax:
Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static voidmain(Stringargs[]){
Dog d=newDog();
d.bark();
d.eat();
}}}
1
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
Method Overriding
If subclass(child class) has the same method as declared in the parent class,it is known
Example
class Bank{
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
class Test2{
public static void main(String args[]){
SBI s=new
SBI(); ICICI
i=new ICICI();
AXIS a=new
AXIS();
System.out.println("URK23CO2018 SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
2
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
Program
1. Develop a java application using Inheritance concept to automate the salary calculation of
employee in an organization as per the salary band given below. Create a super class called
Employee and derive sub classes as per the given table. Apply method overriding (Run time
Polymorphism) to implement the following services via menu driven interface.
c) Calculate Tax
import java.util.Scanner;
class Employee {
double Basic_salary;
double DA ;
double hra;
double tax;
double EPF;
double gross;
double net;
Employee(double basic,int da,int Hra,int TAX,int epf){
this.Basic_salary = basic;
this.DA = Basic_salary * da/100;
this.hra = Basic_salary * Hra/100;
this.tax = Basic_salary * TAX/100;
this.EPF = epf;
this.gross = Basic_salary + DA + hra;
this.net = gross - tax - EPF ;
}
void print() {
System.out.println("Salary details");
System.out.println("Basic pay"+ Basic_salary);
System.out.println("Dearance allowance"+DA);
System.out.println("House rent allowance"+hra);
System.out.println("TAX"+tax);
System.out.println("EPF"+EPF);
System.out.println("Gross Salary"+gross);
System.out.println("Net Salary"+ net);
3
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
}
}
class Manager extends Employee{
Manager(){
super(100000,95,20,25,3500);
}
}
class Engineer extends Employee{
Engineer(){
super(50000,80,15,15,2500);}
}
public class Employer {
public static void main(String []args) {
Manager m = new Manager();
Engineer b = new Engineer();
Scanner s = new Scanner(System.in);
System.out.println(" URK23CO2018 Calculate the salary of \n 1. Manager \n 2. Engineer
\n Choose your option");
int a = s.nextInt();
if (a == 1) {
m.print();}
else {
b.print();}
s.close();}
}
Output Screenshot
4
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
4. Develop a java application using Inheritance as per the following. Create a super class
called Train which has attributes like name, trainno, source, destination, no. of tickets,
cost. Add methods in super class such as Check_SeatAvailablity, BookTicket. Derive two
sub classes such as ChennaiExpress and CoimbatoreExpress. Override all the super class
functions in the derived classes. Implement the services such as check seat availability and
booking ticket using run time polymorpshim with menu driven options. Initialize few train
details using array of objects to make it those functions work for any source and
Program
import java.util.Scanner;
class Train {
String name;
int tno;
String source;
String destination;
int nt;
int available;
int cost;
Train(String name, int tno, String source, String destination, int nt, int cost) {
this.name = name;
this.tno = tno;
this.source = source;
this.destination = destination;
this.nt = nt;
this.available = nt;
this.cost = cost;
}
void CheckSeatAvailable() {
System.out.println("Seats Available: " + available);
}
void Bookticket(int n) {
5
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
if (available >= n) {
available -= n;
System.out.println("Ticket(s) Booked Successfully");
} else {
System.out.println("Not Enough Tickets Available. Availability: " + available);
}
}
void print() {
System.out.printf("Train Name: %s%n", name);
System.out.printf("Train Number: %d%n", tno);
System.out.printf("Starting Location: %s%n", source);
System.out.printf("Destination Location: %s%n", destination);
System.out.printf("Number of Tickets Available: %d%n", available);
System.out.printf("Train Cost: %d%n", cost);
}
}
System.out.println("URK23CO2018");
System.out.println("101. Chennai Express (Chennai-Kanyakumari)");
6
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
if (selectedTrain != null) {
System.out.println("1. Check Seat Availability");
System.out.println("2. Book Ticket");
System.out.println("3. Print Ticket Details");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = s.nextInt();
switch (choice) {
case 1:
selectedTrain.CheckSeatAvailable();
break;
case 2:
System.out.print("Enter number of tickets to book: ");
int tickets = s.nextInt();
selectedTrain.Bookticket(tickets);
break;
case 3:
selectedTrain.print();
break;
default:
System.out.println("Invalid choice");
}
} else
{
System.out.println("Train not found.");
}
7
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
s.close();
}
}
Output Screenshot
8
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
8. Write a menu driven application for Library Management using Java to demonstrate the
concept of Inheritance. Your application must contain the following functionalities along
i. bookTitle
ii. bookAuthor
iii. bookNoOfCopies
iv. bookAvailability
v. bookEdition
vi. bookPublisher
Program
import java.util.Scanner;
class Book{
String bookTitle;
String bookAuthor;
int bookNoOfCopies;
boolean bookAvailability;
int bookEdition;
String bookPublisher;
9
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
}
void print(){
System.out.println("bookTitle:"+bookTitle);
System.out.println("bookAuthor:"+bookAuthor);
System.out.println("bookNoOfCopies:"+bookNoOfCopies);
System.out.println("bookAvailability:"+bookAvailability);
System.out.println("bookEdition:"+bookEdition);
System.out.println("bookPublisher:"+bookPublisher);
}
void borrowBook(){
if (this.bookAvailability){
this.bookNoOfCopies--;
System.out.println("The book has been successfully delivered");
}
else{
System.out.println("Book not available");
}
}
void ReturnBook(){
this.bookNoOfCopies +=1;
this.bookAvailability = true;
System.out.println("The booked has been returned successfully");
}
}
10
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
switch(choice){
case 1:
l.borrowBook();
break;
case 2:
l.ReturnBook();
break;
case 3:
l.print();
case 4:
break;
default:
System.out.println("Wrong input");
}s.close();
}
11
23DC2002 - Object Oriented Programming Lab Reg.No URK23CO2018
Output Screenshot
Result
Successfully implemented and executed the creation and manipulation of classes and objects in
Java to solve real-world problems using inheritance, method overriding, and runtime
polymorphism
12