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

Lab examples

Uploaded by

Fred Neilly
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)
6 views4 pages

Lab examples

Uploaded by

Fred Neilly
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

Example 1: Shape Hierarchy (Understanding Inheritance Relationships)

This example demonstrates inheritance for creating a hierarchy of shapes.

Classes:

1. Shape (Base Class):


o Define a base class Shape with basic properties like area (can be a double) but
without a specific implementation for calculating it.
o Include an abstract method calculateArea() to enforce subclasses to implement
their own area calculation logic.
2. Rectangle (Subclass):
o Create a subclass Rectangle inheriting from Shape.
o Introduce private fields for length and width (both doubles).
o Implement the calculateArea() method to return length * width.
3. Circle (Subclass):
o Create another subclass Circle inheriting from Shape.
o Include a private field for radius (double).
o Implement the calculateArea() method to return Math.PI * radius *
radius.
4. Usage in Main Method:
o In the main method, demonstrate creating objects of Rectangle and Circle.
o Call their respective calculateArea() methods and print the results.

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 showcases the "is-a" relationship in inheritance. A Rectangle is a type of


Shape, and a Circle is also a type of Shape.
 It highlights abstract methods that enforce subclasses to provide their own
implementation for specific functionalities.

Example 2: Library Management System (Demonstrating Polymorphism)

This example simulates a library system with different types of library items.

Classes:

1. LibraryItem (Base Class):


o Define a base class LibraryItem with common properties like title (String),
author (String), and checkout status (boolean).
o Include getter methods for these properties.

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).

1. // LibraryItem.java (Base Class)


2. public class LibraryItem {
3. private String title;
4. private String author;
5. private boolean checkedOut;
6.
7. public LibraryItem(String title, String author) {
8. this.title = title;
9. this.author = author;
10. this.checkedOut = false;
11. }
12.
13. public String getTitle() {
14. return title;
15. }
16.
17. public String getAuthor() {
18. return author;
19. }
20.
21. public boolean isCheckedOut() {
22. return checkedOut;
23. }
24.
25. public void setCheckedOut(boolean checkedOut) {
26. this.checkedOut = checkedOut;
27. }
28.
29. @Override
30. public String toString() {
31. return "Title: " + title + ", Author: " + author + ", Checked
Out: " + checkedOut;
32. }
33. }
34.
35. // Book.java (Subclass)

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

You might also like