Tip:
Highlight text to annotate it
X
Hi, I'm Cristian. Welcome to the laboratory number ten of the module "Development of Computer
System Functions".
In this lab you will practice the concept of inheritance in Java.
Open Eclipse by double clicking on its icon. To do this lab you will need to create three
classes. As usual, in the first step you will create a new project. To do this, choose Archivo
? Nuevo ? Proyecto Java. In the wizard that it will be appeared put "Lab10" in the "Nombre
de proyecto" textbox and click on “Finalizar”.
Import files with the source code of previous lab. Deploy the project by double clicking
on its name and click the right mouse button on the icon src. Select Importar. In the wizard
that it will be appeared, choose General ? Sistema de archivos. Look for files with
the source code of the Alumno and TestAlumno classes.
Mark them and then click on Finalizar.
Once your project contains two classes, create a new class called AlumnoCiclos, that inherits
from the Alumno class. To do this, simply right-click on the src icon. Select Nueva
? Clase. In the wizard that it will be appeared, put “AlumnoCiclos” in the “Nombre”
textbox,
replace java.lang.Object by Alumno in the “Superclase” textbox. Then click on Finalizar.
Within AlumnoCiclos class define its own attribute, which is:
Create the signature of the constructor of the class. It should receive the values of
attributes and those inherited as parameters. That is:
Now create the code for the constructor. You must call the base constructor class sending
inherited attributes as parameters. That is:
And assign the parameter with the value of the attribute itself:
Import GregorianCalendar class from the java.util package to work with dates.
Create setter and getter methods of this new attribute:
Finally, create toString() method that returns a String with the values of the own attributes
and inherited attributes.
Open TestAlumno class with a double click on the file with the source code. Create an
object of the GregorianCalendar class called fechaNacimientoLaura and call the constructor
of that class that accepts year, month and birthday as parameters. That is:
Create an object of the AlumnoCiclos class called alumnaLaura and call the constructor
of the class with the information of that student:
Now you can see how AlumnoCiclos constructor is called with their attributes, but object
belongs to Alumno class.
Now, call toString() method of the alumnoLaura and display its contents, that is:
Run the code and see how it is called toString() method from AlumnoCiclos class and not toString()
method from its parent class, that is, Alumno class.
You can see the differences between these two methods because Juan is really calling
toString() method from Alumno.
And so this laboratory number 10 is ended.
Thank you very much for your attention. See you in the lab number 11.