Java-Object Oriented Programming

Basic concept of object oriented programming

Tharuksi Gunasekara
3 min readApr 8, 2022

Hello friends, in this article, I am going to discuss with you about the inheritance.

Inheritance

What is the inheritance?

Inheritance is used to organize and create a reusable class from the existing one. As well as inheritance is another fundamental concept in object-oriented programming.

Furthermore…

Therefore we called the existing class as a parent class or super class or base class and derived class named as a child class or subclass. As well as child inherits the characteristics of the parent class. The programmer can add new variables or methods or can modify the inherited ones.

How to create inheritance?

EX:- we use an old phone for a parent class and a new phone for a child class.

In the new phone class(subclass), it gets substance from the old phone class(super class). Therefore we can show above the example in a UML class diagram.

In the UML diagram, with an open arrowhead pointing to the parent class.

Inheritance should create a is-a relationship, meaning the child is a more specific version of the parent

Various forms of inheritance

Not only the inheritance but also access modifiers are very important to learn with the inheritance. So let’s discuss the access modifiers.

What are the access modifiers?

Access modifiers in java specifies the accessibility or scope of the filed, method, constructor or class. As well as we can change the access level of fields, constructors, methods and class by applying on it.

There are 4 types of access modifiers.

  1. Private
  2. Default
  3. Protected
  4. Public

1. Private access modifier.

In this modifier, it can be accessed from outside of the class

2.Default access modifier.

In this modifier, the access level is only with the package. Can not be accessed from outside the package. Therefore when you do not define any access level, it will be the default.

3.Protected access modifier

The access level is within the package and package through the child class. Therefore if you do not make child class, you can not be accessed from the outside of the package.

4.Public access modifier

The access level is everywhere. It can access from within the class, outside of the class, within the package and outside of the package.

Here is the table, that you can understand the uses of access modifiers very easily.

As a summary of this lesson, I discussed with you about the inheritance and access modifiers. I think, you learned something new in this article.

--

--

Tharuksi Gunasekara

I am an undergraduate of faculty of Information technology in university of Moratuwa