[Go to site: main page, start]

0% found this document useful (0 votes)
6 views13 pages

OOP With Java Programs

The document contains multiple Java programs demonstrating key object-oriented programming concepts such as inheritance, polymorphism, method overloading, and the use of abstract classes and interfaces. Each program illustrates specific functionalities, including salary calculations for employees, managing scores, and calculating areas of shapes. The examples also showcase the use of constructors, arrays, and final variables in Java.

Uploaded by

Sehar Ejaz
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)
6 views13 pages

OOP With Java Programs

The document contains multiple Java programs demonstrating key object-oriented programming concepts such as inheritance, polymorphism, method overloading, and the use of abstract classes and interfaces. Each program illustrates specific functionalities, including salary calculations for employees, managing scores, and calculating areas of shapes. The examples also showcase the use of constructors, arrays, and final variables in Java.

Uploaded by

Sehar Ejaz
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

Write a Java program to demonstrate inheritance by creating a base class Employee and a

derived class Manager. Override a method in the child class and show runtime
polymorphism using a parent class reference.
class Employee {
void calculateSalary() {
[Link]("Employee salary calculation");
}
}

class Manager extends Employee {


@Override
void calculateSalary() {
[Link]("Manager salary includes bonus");
}
}

public class Main1 {


public static void main(String[] args) {
Employee e = new Manager(); // Polymorphism
[Link]();
}
}

Write a Java program that demonstrates the use of Object class reference to store
different types of objects.

class Book {

void read() {

[Link]("Reading a book");

public class Main7 {

public static void main(String[] args) {

Object obj = new Book();

Book b = (Book) obj; // Type casting

[Link]();

}
Create an abstract class Bank with an abstract method getInterestRate(). Implement this
method in child classes and demonstrate runtime polymorphism.

abstract class Bank {

abstract void getInterestRate();

class HBL extends Bank {

void getInterestRate() {

[Link]("HBL Interest Rate: 7%");

class UBL extends Bank {

void getInterestRate() {

[Link]("UBL Interest Rate: 6.5%");

public class Main5 {

public static void main(String[] args) {

Bank b;

b = new HBL();

[Link]();

b = new UBL();

[Link]();

}
Develop a Java program that uses constructors in inheritance to initialize parent class data
from the child class.

class Vehicle {

int speed;

Vehicle(int speed) {

[Link] = speed;

class Car extends Vehicle {

Car(int speed) {

super(speed);

void showSpeed() {

[Link]("Car speed: " + speed);

public class Main4 {

public static void main(String[] args) {

Car c = new Car(120);

[Link]();

}
Create a Java class named Result that stores subject scores in an integer array initialized
with three values. Increase the array size to add one more score, implement a method
calculateAverage() to compute and return the average of all scores, and display the
average in the main method.

import [Link];

class Result {

int[] scores = {60, 75, 85};

// Method to add new element to array

void addScore(int newScore) {

scores = [Link](scores, [Link] + 1);

scores[[Link] - 1] = newScore;

// Method to calculate average

double calculateAverage() {

int sum = 0;

for (int s : scores) {

sum += s;

return (double) sum / [Link];

public static void main(String[] args) {

Result r = new Result(); // Object creation

[Link](90); // Adding new element

double avg = [Link](); // Method call

[Link]("Average score: " + avg);

}
Create a Java class Calculator that demonstrates method overloading by defining two add()
[Link] method should add two integers and the other should add three integers.
class Calculator {

int add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

public static void main(String[] args) {


Calculator c = new Calculator();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
}
}

Create an interface Drawable containing a method draw().


Implement the interface in a class Line.
interface Drawable {
void draw();
}

class Line implements Drawable {


public void draw() {
[Link]("Drawing line");
}

public static void main(String[] args) {


Drawable d = new Line();
[Link]();
}
}
Create a Java program with a base class Shape having a method area(). Derive classes
Circle and Square from it and override area() to calculate their areas. In main(), use base
class references to create objects of Circle and Square, call the overridden methods, and
display the areas.

// Base class
class Shape {
void area() {
[Link]("Area not defined for generic shape");
}
}

// Derived class Circle


class Circle extends Shape {
double radius;

Circle(double r) {
radius = r;
}

@Override
void area() {
double circleArea = 3.14 * radius * radius;
[Link]("Area of Circle: " + circleArea);
}
}

// Derived class Square


class Square extends Shape {
double side;

Square(double s) {
side = s;
}

@Override
void area() {
double squareArea = side * side;
[Link]("Area of Square: " + squareArea);
}
}

// Main class
public class MainShape {
public static void main(String[] args) {
// Base class references pointing to derived objects
Shape s1 = new Circle(5); // radius = 5
Shape s2 = new Square(4); // side = 4

// Call overridden methods (Polymorphism)


[Link]();
[Link]();
}
}

Create a Java class Student and store multiple student objects in an array.
Display the details of each student.
class Student {
int id;
String name;

Student(int i, String n) {
id = i;
name = n;
}

public static void main(String[] args) {


Student[] arr = new Student[2];

arr[0] = new Student(1, "Ali");


arr[1] = new Student(2, "Sara");

for (int i = 0; i < [Link]; i++) {


[Link](arr[i].id + " " + arr[i].name);
}
}
}

Create a base class Transport with attributes name and speed.


Create derived classes Bus and [Link] a method showInfo() in Transport and override it
in the derived classes to display vehicle type along with name and [Link] main(), create
objects for Bus and Truck and call showInfo().
class Transport {
String name;
int speed;

Transport(String name, int speed) {


[Link] = name;
[Link] = speed;
}
void showInfo() {
[Link]("Transport Name: " + name + ", Speed: " + speed);
}
}

class Bus extends Transport {


Bus(String name, int speed) {
super(name, speed);
}

@Override
void showInfo() {
[Link]("Bus Name: " + name + ", Speed: " + speed);
}
}

class Truck extends Transport {


Truck(String name, int speed) {
super(name, speed);
}

@Override
void showInfo() {
[Link]("Truck Name: " + name + ", Speed: " + speed);
}
}

public class Solution1 {


public static void main(String[] args) {
Bus myBus = new Bus("Volvo", 80);
Truck myTruck = new Truck("Tata", 100);

[Link]();
[Link]();
}
}

Create a Java class Employee with data members id and name.


Initialize the data members using a parameterized constructor and display the values.
class Employee {
int id;
String name;

Employee(int i, String n) {
id = i;
name = n;
}

void display() {
[Link](id + " " + name);
}

public static void main(String[] args) {


Employee e = new Employee(101, "Ali");
[Link]();
}
}

Create a Java class that uses a final variable and displays its value.

class Speed {
final int maxSpeed = 120;

void display() {
[Link]("Max Speed: " + maxSpeed);
}

public static void main(String[] args) {


Speed s = new Speed();
[Link]();
}
}

You might also like