Assignment
Assignment
class Book {
String title;
String author;
double price;
void display() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Price: " + price);
}
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;
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Roll Number: " + rollNumber);
System.out.println("Course: " + course);
}
3. Create a class Shape with a method area(). Derive two classes Circle and Rectangle from it,
Circle(double radius) {
this.radius = radius;
}
void area() {
System.out.println("Circle Area: " + (Math.PI * radius * radius));
}
}
void area() {
System.out.println("Rectangle Area: " + (length * breadth));
}
}
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;
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();
}
v1.start();
v1.stop();
v2.start();
v2.stop();
}
}