SLC S22 Week3 || Inheritance and Polymorphism in JAVA
Hi friends, after reading around I am interested in participating in an amazing challenge this week. The challenge is titled: "Inheritance and Polymorphism in JAVA" by Mr @kouba01 following the contest link.
Previously I also invited other friends such as Mr. @irawandedy, Mr @muzack1 , and Mr @pelon53. |
---|
Task 1
In the first task, we will create a Java program which will use 3 classes namely Person, Student and Main where there will be name and age attributes. The Student class will inherit from Person and add the IdStudent attribute.
First I created a new Java Project in Eclipse, then I named the project Warisan. Then I also created a new package with the name Warisan as well. After that I created 3 new classes in the Warisan project, each of which I named : Person Class, Student Class, and Main Class. Here is the code that is included in each of these classes:
- Class Person.
The Person class consists of 2 attributes, namely name and age. While the constructor there serves to initialize these attributes. As for the coding above, there is also a displayDetails method which functions to display information from this Person class.
Student Class
In this class will inherit Person utilizing the extends keyword. There I also added a new attribute, IdStudent. Not forgetting to use super to call the constructor that comes from the parent class. Next, override it by utilizing the displayDetails method so that it can add Student Id information.Main Class
In this class, the purpose is to create objects from the Person and Student classes. After that we will call the displayDetails method in order to display the details of each object.The result:
Task 2
In this second task, we will create a Shape class that will have an overloaded area() method in order to calculate the area of various shapes (circle, rectangle, and triangle) based on the parameters provided.
First we create a new java project and I name it Overloading, then I create a package with the same name Overloading. Next I create two classes each with the name : Shape and Main. Here are the details of the two classes:
- Shape class:
In the coding of the Shape class that I have created above, it contains an overloaded area() method, there is already a formula for calculating the Area of a Circle which utilizes the radius parameter. In addition to the circle area formula, the second one already has a Rectangle Area formula that utilizes the length and width parameters. And thirdly, there is already a Triangle Area formula by utilizing the base, height, and isTriangle flag parameters with the intention of being able to distinguish cases.
Main Class:
In the main class that I have created, there will be a function to display the choice of shape that you want to calculate the area, where there are already options for Area Calculation 1. Circle, 2. Rectangle, 3. Triangle. By asking for input from someone so that they can enter each shape by selecting 1, 2 or 3. To call it also uses the corresponding area() method in the Shape class above.Result :
Task 3
In this 3rd assignment, we will create a java code about banks, where we will create a Bank class where we utilize the getInterestRate() method. Next, we create two other classes such as SavingsBank class and CurrentBank class which will replace getInterestRate() and utilize the method to return a specific interest rate. In the program we create, it will certainly display interest rates for both types of banks.
First we create a new java project and name it Bank, then I also create a package with the same name Bank. Then I then created 4 classes, including the Bank Class, SavingsBank Class, CurrentBank Class, and Main Class. Here is each of the code:
Bank Class
The Bank class contains a getInterestRate() method with the purpose of returning the default interest rate of 0.0%. And please note that this class is the parent class.SavingsBank Class
This SavingsBank class will also inherit what we have created in the Bank class above. And override it by utilizing the getInterestRate() method in order to return an interest rate of 5.5%.
- CurrentBank Class
We need to know that the CurrentBank class will also inherit from the Bank class, and override it by utilizing the getInterestRate() method so that it can return an interest rate of 1.0%.
Main Class
While the Main class will certainly create objects from the SavingsBank class and also what is in the CurrentBank class. The last is to be able to display the interest rate of each bank by utilizing the polymorphism method.The result:
Task 4
In task 4, we will create a java program again, but this time about a vehicle engine where there is a Vehicle base class, then there are 2 derived classes namely Bike and Truck classes. We are also encouraged to use the startEngine() method in the base class and override it in the child classes to display some specific messages. By utilizing polymorphism, we can call and utilize a method through a base class reference.
First, we will create a new java project with a name that is up to us, here I give it Sepedamotor. Then I create a package with the name Motorcycle too, then just create 4 classes with each name, namely Vehicle, Bike, Truck and Main. Here are the details of the four classes:
- Vehicle Class
This class is one of the base classes utilizing the startEngine() method which displays a general message.
- Bike class
The bike class will inherit the Vehicle class and will override it by utilizing the startEngine() method in order to provide motorcycle-specific messages.
- Truck Class
This class also inherits the Vehicle class and overrides it by utilizing the startEngine() method to provide truck-specific messages.
- Main Class
In this class we will create Bike and Truck objects by utilizing the Vehicle reference (polymorphism) and then we will call by utilizing the startEngine() method in order to display the message according to the vehicle type.
- The result
Task 5
In this 5th assignment we will implement a Library Management System where we will utilize the concepts of inheritance and polymorphism. There is a base class that we will name Item, then there are some basic attributes and also use methods that are implemented or overridden in the Book and Magazine derived classes.
First we will create a java project in eclipse, the name is up to us. Here I will name it SistemManagementPerpustakaan. Then I create a package with the same name. Next, I created 4 classes with each of their names, namely : Item class, Book class, Magazine class, and Main class. Here are the class details:
Item Class
This Item base class has several attributes including: id, title, and isAvailable. Here also uses several methods including : borrowItem() with the intention of being able to borrow items if available, there is also a returnItem() method with the intention of being able to return items if they are being borrowed and the last is the displayDetails() method which will display general details.Book Class
The Book derived class will add the author attribute and also override the displayDetails() method in order to display the author.Magazine Class
In this Magazine derived class, of course, we will be in charge of adding the issue attribute and we also override it by utilizing the displayDetails() method in order to display the edition.
Main class
In this last class, the Main class, we will create objects from the Book class and Magazine class by utilizing the Item base class reference. We will also use polymorphism to call the borrowItem(), returnItem(), and displayDetails() methods.The result
Task 6
In this 6th assignment we will implement an Employee Management System by utilizing the concepts of inheritance and polymorphism. In the coding, we will create several classes in which there is an Employee base class consisting of basic attributes and methods that are overridden in derived classes.
First, I created a new java project that I named SistemManagementKaryawan. Followed by creating a package with the same name. Then I created several classes including : Employee class, HourlyWorker class, ContractualEmployee class, PermanentEmployee class, and Main class. Here are the details:
- Employee Class
In this Employee class, there are several attributes including: name, CIN, address, and salary. Not only that, we also use several methods such as calculateSalary() which is an abstract that will be implemented in other derived classes. and the display() method which is useful for displaying general employee details.
Meanwhile, other derived classes such as the HourlyWorker class will be useful for adding the hourlyRate and hoursWorked attributes. The salary is calculated based on the hourly rate and then multiplied by the number of hours worked. As for the ContractualEmployee class, it will be useful to add the fixedMonthlySalary attribute. Which is a fixed salary every month. And in the PermanentEmployee class it is useful to add the baseSalary and performanceBonus attributes. where salary is calculated as the sum of base salary and bonus.
- HourlyWorker Class
- ContractualEmployee class
- PermanentEmployee class
- Main class
- The result
Task 7
Let's analyze each instruction contained in the code based on the concept of inheritance in Java. Then the following is my version:
We need to know that the Inheritance structure is composed of parent classes, so from the problem we can understand that :
C1 is a superclass (parent class), while C11 is one of its descendants or subclasses of C1. C111 is another descendant or subclass of C11.
Therefore, these subclasses can be cast to the parent class without explicit casting (upcasting), but we need to know that casting from the parent class to its descendants (downcasting) requires an explicit cast. This downcasting will fail at runtime if the actual object is not of the specified subclass type.
Let's analyze it first:
a. o1 = o2; we can consider it valid, because o2 is a reference to object C11, which is a subclass of C1. And the upcasting may appear implicitly.
b. o1 = o3; is also valid, because o3 has a reference to object C111, which is a subclass of C1. also the upcasting appears implicitly.
c. o3 = o1; this is invalid, the reason is because in o1 it is a reference to type C1, while o3 really needs type C111. So explicit downcasting is needed.
d. o4 = o5; Valid. with the main reason that o5 has a reference to object C111, and o4 has type C11. So that upcasting appears implicitly.
e. o3 = (C111)o1; also Valid. But explicit downcasting is done. If only o1 actually refers through an object of type C111, then there would be no error. If it is not through an object of type C111, then there will certainly be a ClassCastException at runtime.
f. o4 = (C11) o5; is also Valid, with the main reason being that o5 is also considered a reference to a C111 object, and can be cast to C11 which at that time C111 is a derived class or subclass of C11.
g. o4 = (C111)o2; could also be valid but a potential runtime error, because explicit downcasting is executed. If o2 was definitely referring to an object of type C111, the result would be no error. If it's not, it's likely a ClassCastException at runtime.
h. o3 = (C11)o5; now this is invalid, for the reason that o5 references type C111, but o3 must also require type C111. Although explicit casting to C11 is executed, the final type does not correspond to C111.
Task 8
In the 8th task, which is the last task, here we will create a Point class where there are several attributes, namely x and y, then we will create a Rectangle class that inherits Point and adds length and width attributes, then we will also create a Parallelogram class that inherits Rectangle and there we will add a height attribute, equipped with a constructor, getter, setter, area(), volume(), and toString() methods, as well as a test class to check the functionality.
First we will open Eclipse first and create a new java project by going File ---> New --->Java Project. Then I give the project name GeometricShapesWalictd. Then I create a new package with the same name too. Then just create several classes including :
Class Point
This Point class will certainly be the class that will represent 2D points with x and y coordinates.Rectangle class.
In this one class, of course, it will be a class that inherits the Point class, and will also add length and width attributes.
Parallelogram Class
The Parallelogram Class is an instance that will inherit from the Rectangle and will add attributes such as height and volume() method.GeometricTest class
This is where all the classes will be tested and will print results such as the area and volume of the parallelogram.The result:
Thank you for your attention and apologize if there are any mistakes.
@walictd
https://x.com/walictd/status/1874758058234225122