Lab examples
Lab examples
Classes:
Java
1. // Shape.java (Base Class)
2. public abstract class Shape {
3. private double area;
4.
5. public abstract double calculateArea();
6.
7. public double getArea() {
8. return area;
9. }
10.
11. public void setArea(double area) {
12. this.area = area;
13. }
14. }
15.
16. // Rectangle.java (Subclass)
17. public class Rectangle extends Shape {
18. private double length;
19. private double width;
20.
21. public Rectangle(double length, double width) {
22. this.length = length;
23. this.width = width;
24. }
25.
26. @Override
1|Page
27. public double calculateArea() {
28. return length * width;
29. }
30. }
31.
32. // Circle.java (Subclass)
33. public class Circle extends Shape {
34. private double radius;
35.
36. public Circle(double radius) {
37. this.radius = radius;
38. }
39.
40. @Override
41. public double calculateArea() {
42. return Math.PI * radius * radius;
43. }
44. }
45.
46. // Main.java
47. public class Main {
48. public static void main(String[] args) {
49. Rectangle rectangle = new Rectangle(5, 3);
50. Circle circle = new Circle(2);
51.
52. System.out.println("Rectangle Area: " +
rectangle.calculateArea());
53. System.out.println("Circle Area: " + circle.calculateArea());
54. }
55. }
Explanation:
This example simulates a library system with different types of library items.
Classes:
2|Page
2. Book (Subclass):
o Create a subclass Book inheriting from LibraryItem.
o Introduce a private field for number of pages (int).
o Override the toString() method to provide a detailed representation of a book
(including title, author, number of pages, and checkout status).
3. Movie (Subclass):
o Create another subclass Movie inheriting from LibraryItem.
o Introduce private fields for director (String) and running time (int in minutes).
o Override the toString() method to provide a detailed representation of a movie
(including title, director, running time, and checkout status).
4. Usage in Main Method:
o In the main method, create an array of LibraryItem references and populate it
with objects of Book and Movie types.
o Loop through the array and call the toString() method on each item
(demonstrating polymorphism).
3|Page
36. public class Book extends LibraryItem {
37. private int numPages;
38.
39. public Book(String title, String author, int numPages) {
40. super(title, author);
41. this.numPages = numPages;
42. }
43.
44. @Override
45. public String toString() {
46. return super.toString() + ", Number of Pages: " + numPages;
47. }
48. }
49.
50. // Movie.java (Subclass)
51. public class Movie extends LibraryItem {
52. private String director;
53. private int runningTime; // Minutes
54.
55. public Movie(String title, String director, int runningTime) {
56. super(title, ""); // Assume movies don't have authors
57. this.director = director;
58. this.runningTime = runningTime;
59. }
60.
61. @Override
62. public String toString() {
63. return super.toString() + ", Director: " + director + ",
Running Time: " + runningTime + " minutes";
64. }
65. }
66.
67. // Main.java
68. public class Main {
69. public static void main(String[] args) {
70. LibraryItem[] items = new LibraryItem[3];
71. items[0] = new Book("The Lord of the Rings", "J.R.R. Tolkien", 1178);
72. items[1] = new Movie("The Shawshank Redemption", "Frank Darabont", 142);
73. items[2] = new Book("Pride and Prejudice", "Jane Austen", 225);
74.
75. for (LibraryItem item : items) {
76. System.out.println(item);
77. }
Explanation:
This example showcases polymorphism, where the same method ( toString()) behaves
differently based on the actual object type at runtime (Book vs. Movie).
It highlights the concept of overridden methods that provide subclass-specific
functionality.
4|Page