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

Assignment

The document contains Java code examples demonstrating various object-oriented programming concepts. It includes the creation of classes such as Book, Person, Shape, BankAccount, and Vehicle, showcasing features like encapsulation, inheritance, polymorphism, and abstraction. Each example includes a main method to create instances and display relevant information or behaviors.

Uploaded by

ayushverma593773
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 views4 pages

Assignment

The document contains Java code examples demonstrating various object-oriented programming concepts. It includes the creation of classes such as Book, Person, Shape, BankAccount, and Vehicle, showcasing features like encapsulation, inheritance, polymorphism, and abstraction. Each example includes a main method to create instances and display relevant information or behaviors.

Uploaded by

ayushverma593773
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
You are on page 1/ 4

1. Create a class Book with attributes: title, author, and price.

Write a program to create and display

details of multiple book objects.

class Book {
String title;
String author;
double price;

Book(String title, String author, double price) {


this.title = title;
this.author = author;
this.price = price;
}

void display() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Price: " + price);
}

public static void main(String[] args) {


Book[] books = {
new Book("Java Basics", "John Doe", 299.99),
new Book("Advanced Java", "Jane Smith", 399.99)
};
for (Book book : books) {
book.display();
System.out.println();
}
}
}

2. Design a base class Person with attributes name and age. Create a derived class Student that

adds roll number and course. Demonstrate inheritance using these classes.

class Person {
String name;
int age;

Person(String name, int age) {


this.name = name;
this.age = age;
}
}

class Student extends Person {


int rollNumber;
String course;

Student(String name, int age, int rollNumber, String course) {


super(name, age);
this.rollNumber = rollNumber;
this.course = course;
}

void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Roll Number: " + rollNumber);
System.out.println("Course: " + course);
}

public static void main(String[] args) {


Student s = new Student("Alice", 20, 101, "Computer Science");
s.display();
}
}

3. Create a class Shape with a method area(). Derive two classes Circle and Rectangle from it,

override the area() method, and demonstrate runtime polymorphism.

abstract class Shape {


abstract void area();
}

class Circle extends Shape {


double radius;

Circle(double radius) {
this.radius = radius;
}

void area() {
System.out.println("Circle Area: " + (Math.PI * radius * radius));
}
}

class Rectangle extends Shape {


double length, breadth;

Rectangle(double length, double breadth) {


this.length = length;
this.breadth = breadth;
}

void area() {
System.out.println("Rectangle Area: " + (length * breadth));
}
}

public class TestShape {


public static void main(String[] args) {
Shape s1 = new Circle(5);
Shape s2 = new Rectangle(4, 6);

s1.area();
s2.area();
}
}

4. Create a class BankAccount with private fields: account number and balance. Provide public

getter and setter methods to access these fields. Demonstrate encapsulation in your program.

class BankAccount {
private String accountNumber;
private double balance;

public String getAccountNumber() {


return accountNumber;
}

public void setAccountNumber(String accountNumber) {


this.accountNumber = accountNumber;
}

public double getBalance() {


return balance;
}

public void setBalance(double balance) {


this.balance = balance;
}

public static void main(String[] args) {


BankAccount account = new BankAccount();
account.setAccountNumber("123456789");
account.setBalance(5000.0);

System.out.println("Account Number: " + account.getAccountNumber());


System.out.println("Balance: " + account.getBalance());
}
}

5. Define an interface Vehicle with methods start() and stop(). Implement this interface in two

classes Car and Bike and demonstrate abstraction through interface implementation.

interface Vehicle {
void start();
void stop();
}

class Car implements Vehicle {


public void start() {
System.out.println("Car started");
}

public void stop() {


System.out.println("Car stopped");
}
}

class Bike implements Vehicle {


public void start() {
System.out.println("Bike started");
}

public void stop() {


System.out.println("Bike stopped");
}
}

public class TestVehicle {


public static void main(String[] args) {
Vehicle v1 = new Car();
Vehicle v2 = new Bike();

v1.start();
v1.stop();
v2.start();
v2.stop();
}
}

You might also like