Skip to content
APPCITORThe Tech Blog
Esc

Java OOP -Inheritance (Part I)

August 9, 2020 2 min read

Java OOP -Inheritance (Part I)

According to the Oracle, In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes is called Inheritance.

A class that is derived from another class is called a sub class (also a derived class, extended class, or child class). The class from which the subclass is derived is called a super class (also a base class or a parent class).

Real world example – Children inherits their qualities, attitudes and behaviors from their parents.

Example of program:

JFrame ——> Super Class

Calculator ——> Sub Class

Qualities of the JFrame is inheriting to the Calculator class.

Case I

When we extend a sub class from a super class, all the attributes and methods (members) of the super class could be access directly by the sub class.

Class B can access variable “a” and the method “printA()” directly from the class A as it is extended by the class B.

Case II

Without creating an object you can’t call the members of the super class by the sub class in the main method.

There is no object created from the class B. Therefore it will cause a runtime error when executing the program.

Case III

Attributes and the methods (members) of the both super class and the sub class are creating inside the same object.

You can call the all attributes and methods of the class A, because an object is created from the class B.

Case IV

When we extends sub class from a super class, the bite code of the super class is taking to the sub class. Therefore when you create an object from the sub class, memory of the super class also would be loaded.

static blocks are running to the order from top to bottom. That means static block of class A is running first and then class B. That memory would be loaded in the object you create from the class B. Therefore static block of class A is running after the object is created.

Case V

Static blocks are running from the super classes to the sub classes.

Static block of class A is running first. Then the static block of class B is running. Next the static block of class C is running and at last the static method of class C is running because it is called in the main method.

Case VI

Let’s assume there are 3 classes called most super class, super class and sub class. All the templates of those classes are allocating to the memory of the object. Then object will initialize from class to class. There blocks and the constructors are running according to the following order.

instance blocks ——-> constructors (from most super class to the super class and then to the sub class)

Methods are running at last if they are called in the main method.

If there are static blocks, Object is initializing after they are executed.

Some of the cases of inheritance are finished. Let’s learn the other cases from the next article. See you.

Thank you.

Share this article

Keep reading