Showing posts with label subclasses. Show all posts
Showing posts with label subclasses. Show all posts

Friday, May 19, 2017

Instantiating a Python subclass

In this screenshot we instantiate the project manager subclass. The project manager subclass derives from the employee details parent class. Note that there are two cases: one where we input the employee id, name, etc, and the another where we do not supply any arguments to the constructor. In this case the default values in the constructor declaration supply the values.

 Instantiating a Python subclass
Click to enlarge
This concludes our screenshot series for this course. We will be offering it in our studio in the coming weeks.


Thursday, May 18, 2017

Python Subclasses

As with  any typical object-oriented language it is possible in Python to create a subclass that inherits properties and methods from a parent or superclass. This is shown in our attached screenshots. The subclass (or child class) can inherit the methods as is or it can override them. This means they are redefined in the subclass. When an object is instantiated from the child class the methods not overridden will work just as they do in the parent class. The overridden methods will work as defined in the subclass.

Our little example shows a project manager subclass created based on the parent employee class. It overrides the salary computation to give a bonus rather than deduct an allowance. It also extends the class by adding a method to return the number of subordinates. Note also the use of the keyword super in the subclass to invoke a method in the superclass.

Subclasses in Java and Python
Click to enlarge view

Friday, April 14, 2017

Instantiate a subclass in Java

The following code actually instantiates the project manager subclass of the employee detail superclass. It inputs the manager's id, name, etc. and then instantiates a project manager passing the variables just input to the subclass constructor which in turn invokes the superclass constructor.

This concludes the initial version of the programs and screenshots for the JVA101 Introduction to coding class. There is another lesson dealing with error handling (exceptions) which we may add in the future.


 View expanded image on box.com
 Click to view expanded image on box.com





Java subclasses

A class can inherit from a parent or superclass. This is done in Java with the extends keyword. The following snippets create a ProjectManager subclass from the EmployeeDetails superclass. Note that the subclass invokes code from the superclass using the super keyword. It also overrides the compute salary method.

 View expanded image on box.com
Click image to view expanded image on box.com