Object-Oriented Programming Concepts in JAVA-Part 2

Mrigank Singh
CodeX
Published in
4 min readApr 5, 2022

Static Members

The data members you want to share with all the class objects are static. Static members can be accessed by using the class name to call them, and no object needs to be created to call them though static members can be called with objects also. The static methods are mainly used to modify or return the private static members. Some key points about static methods are:

  • Static methods cannot access non-static methods and data members, while the other hand, non-static methods can access static members and methods.
  • As objects don’t need to be created to access static members, this reference is not passed to static methods; this reference is only passed to non-static methods in the program.

Constructors

The constructors are called whenever objects of a class are created. The constructor is similar to a method with no return type and name of the class. There are two types of constructors parameterized constructor and default constructor.

Parameterized Constructor

In case of no constructor is created inside the class, a default constructor is created, which initializes members of all primitive types as their default values and non-primitive types as null.

Note: JAVA will create no default constructor if we create any constructor inside the class.

Encapsulation

In JAVA, documentation encapsulation is defined as the ability of an object to hide its data and methods from the rest of the world. It is one of the fundamental principles of object-oriented programming. In Java, a class encapsulates the fields, which hold the state of an object, and the methods, which define the actions of the object. The encapsulation is attained with the help of access modifiers public, private, default, and protected. For example, while developing an application in JAVA, a common practice is to set the data members access modifier as private and then access it with the help of getters and setters.

Inheritance

When a class inherits the public methods and data members of a class, it is called inheritance. The class inheriting the methods and members is called a child class or subclass and the class whose methods and members are getting inherited is known as a superclass or base or parent class. The keyword super refers to the superclass inside the child class. With the help of super, we can call base class all methods and members. The keyword extends is used in the child class to inherit the base class. The child class can have methods and members exclusive to the child class only. Let’s look at all the theories we discussed above in a code.

Inheritance Program
Output

Types of Inheritance in JAVA

Note: In JAVA, there cannot be multiple parent classes for a child class. So in the above figure, Multiple Inheritance and Hybrid Inheritance are not allowed in JAVA. However, Multiple Inheritance and Hybrid Inheritance are possible by using interfaces in JAVA.

A child class inherits all the public and protected members of the parent class whether the child class is in the same package or not in the same package as that of the parent class. The child also inherits all the package-private or default parent class members if both the parent and child class are in the same package.

Some features of inheritance are:

  • The child class can directly use the inherited fields and methods.
  • We can declare a variable in the child class as the same name as in the parent class. This is called data hiding. This can be done with the static members of the parent class also.
  • We can declare a method in the subclass with the same signature as the parent class. This is called method overriding. This can be done with the static members also.
  • We can declare new fields and methods in the subclass which are not a part of the superclass.
  • We can write a subclass constructor that invokes the superclass constructor either implicitly or by using the super keyword.
  • We can initialize the superclass with the child class object.

Inheritance vs. Composition

Inheritance is an is-a relationship. For example, Engineering students, Medical students, and students. Engineering Student is a Student.

Composition is a has-a relationship. For example, House is made up of bricks and cement.

Whenever there is a clash between inheritance and composition, it is always advisable to use composition. In composition, we do not use extends keyword we use objects of the base classes inside the child class. Let us take the example of a house, cement, and bricks to demonstrate composition with the help of code.

Composition

--

--