[go: up one dir, main page]

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

lab6

The document contains three Java code examples demonstrating object-oriented programming concepts. The first example illustrates inheritance with Vehicle, Car, and Bike classes, showcasing method overriding and specific functionalities. The second example features Shape, Circle, and Rectangle classes, emphasizing polymorphism, while the third example presents a Library class managing a collection of Book objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

lab6

The document contains three Java code examples demonstrating object-oriented programming concepts. The first example illustrates inheritance with Vehicle, Car, and Bike classes, showcasing method overriding and specific functionalities. The second example features Shape, Circle, and Rectangle classes, emphasizing polymorphism, while the third example presents a Library class managing a collection of Book objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1:

class Vehicle {
private String brand;

public Vehicle(String brand) {


this.brand = brand;
}

public void start() {


System.out.println("Vehicle is starting");
}

public void stop() {


System.out.println("Vehicle is stopping");
}

public String getBrand() {


return brand;
}
}

class Car extends Vehicle {


public Car(String brand) {
super(brand);
}

@Override
public void start() {
System.out.println("Car is starting");
}

public void drive() {


System.out.println("Car is driving");
}
}

class Bike extends Vehicle {


public Bike(String brand) {
super(brand);
}

@Override
public void start() {
System.out.println("Bike is starting");
}

public void ride() {


System.out.println("Bike is riding");
}
}

public class TestVehicles {


public static void main(String[] args) {
Car car = new Car("Toyota");
Bike bike = new Bike("Honda");

car.start();
car.stop();
car.drive();

bike.start();
bike.stop();
bike.ride();
}
}
Q2:
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}

public void erase() {


System.out.println("Erasing a shape");
}
}

class Circle extends Shape {


@Override
public void draw() {
System.out.println("Drawing a circle");
}

@Override
public void erase() {
System.out.println("Erasing a circle");
}
}

class Rectangle extends Shape {


@Override
public void draw() {
System.out.println("Drawing a rectangle");
}

@Override
public void erase() {
System.out.println("Erasing a rectangle");
}
}

public class TestShapes {


public static void main(String[] args) {
Shape[] shapes = {new Circle(), new Rectangle()};

for (Shape shape : shapes) {


shape.draw();
shape.erase();
}
}
}
Q3:
import java.util.ArrayList;

class Book {
private String title;

public Book(String title) {


this.title = title;
}

public String getTitle() {


return title;
}
}

class Library {
private ArrayList<Object> items;

public Library() {
items = new ArrayList<>();
}

public void addBook(Book book) {


items.add(book);
}

public void printBooks() {


for (Object item : items) {
if (item instanceof Book) {
System.out.println(((Book) item).getTitle());
}
}
}
}

public class TestLibrary {


public static void main(String[] args) {
Library library = new Library();
library.addBook(new Book("Java Programming"));
library.addBook(new Book("Data Structures"));
library.printBooks();
}
}

You might also like