Over the last couple of weeks, we have looked at Java Modifiers and Basic Java Keywords and syntax I and II. As you may already know, Java is an object oriented program (OOP). OOP (Object Oriented Programming) as a programming model involves the collection of interacting objects. An Object is a black box which contains code and data, it sends and receives messages and are used to represent real life concepts ranging from human beings (described by name, address, and so forth) to countries (whose properties can be described and managed) down to the little widgets on a computer desktop (such as buttons and scroll bars). Some of the concepts of OOP have been discussed in my earlier posts however, despite these discussions OOP will be incomplete without explaining the following concepts:
Encapsulation a.k.a Data Hiding: In my last JAVA post, I mentioned encapsulation as one of the OOP concept that aims at protecting valuable information so as to hide unnecessary details. Encapsulation is a concept whereby a variable of a class is hidden from other classes and can be accessed only through the mutator methods(getters and setters) of their class. Setters and getters are used to control the changes to a variable
- How to Achieve Encapsulation: To achieve this concept, a variable is declared as private, setters and getters are generated to modify and access the values of the variables.
- Function: It is used for security purpose by allowing access only to the functionality of the class but inability to alter the variable.
The code below shows variables gender,age and height encapsulated (created as private keyword) and public setter and getter methods created to control each variable.
Now, a class HumanCreator can access all variables in Human class as shown below :-
The example above shows that object man and woman were able to modify the attributes in class Human to suite their purpose without altering the attribute and affecting the next object. Hence, to a man object, gender is Male while to a Woman object gender is female.
INHERITANCE: Just like two parents come together and give birth to a child that succeeds them, carry on the family name and picks one or more of the parent attributes and behaviours, a class in Java can also give birth to a child class. This concept in Java is known as Inheritance. Inheritance is a situation whereby an object or class is derived from another class. The parent class is said to be the superclass while the child class is known as a subclass.
How to Achieve: This kind of relationship is known as Is-A relationship and it is achieved using the ‘extends’ keyword. E.g. we can say a class Nigeria is a Country
Why we use it: Inheritance is used to achieve code reusability by inheriting attributes and behaviours from parent class and avoiding code repetition.
The code below shows a class Country () which will be considered as our super class in this example.
Now, Nigeria in the next code snippet extends the class Country hence inheriting all the variables and methods within class Country. With this relationship, we can say Country is a parent to Nigeria hence Nigeria IS-A Country.
Things to note with Inheritance:
- In the same way a child can but doesn’t necessarily have to exhibit all the attributes and behaviours of a parent, a class in Java can choose to or not to exhibit a superclass’s attribute (variable) and behaviour (method).
- A subclass can choose to customize a superclass’s method by overriding it , this condition is called Method Overriding – A superclass method is said to be overridden if a subclass creates a method within itself specifying the same name, parameter list, and return type as the method being overridden.
- If a subclass is trying to access the member of a superclass that have the same name as the sub class, super keyword is used on the super class member to differentiate the subclass member from the superclass member.
- A class can only extend one(1) super class the same way a child can only have one father.
Next week, we will be wrapping up this lesson with Abstraction and Polymorphism. Till then, you can check them out in advance…