You are on page 1of 2

INHERITANCE

Inheritance is a mechanism where it derives a new class from the old one in which one class
acquires the property of another class. This is when a child class inherits all the variables and
methods of the parent classes (Java Inheritance (Subclass and Superclass), 2020). Shared features
are grouped in a single class known as the parent class and the unique features are separated into
subclasses.

Inheritance plays an important role in object oriented programming. By using inheritance, we do


not have to repeat the code because we can reuse the fields and methods of the existing class. It
promotes reusability which is a very important aspect of coding to avoid repetition. Inheritance
in java can easily be used with declaring “extend” keyword in the public class method.

4.2 ENCAPSULATION

Encapsulation is a mechanism of putting the data, variables and code acting on the data together
as a single unit. By using encapsulation, the variables of a class will be hidden from other classes
and can only be accessed through the methods of their current class. It is a technique of making
the variables in a class private and allowing access to the variables through public methods.

There are two ways of declaring encapsulation in Java, the first way is declaring the variables of
a class private then secondly providing a public setter and getter methods to view the variables
(Encapsulation in Java - GeeksforGeeks, 2020). The importance of encapsulation is that it
protects an object from unwanted access and allows access to a level without revealing very
complex details.
4.2.1 Encapsulation used in code

Figure 6

As we can see from the figure 6 above, the variables of class are declared as priva

You might also like