The basic three principles of JAVA are inheritance, polymorphism and encapsulation. All the concepts of JAVA are based on classes and its objects. An object is a real-world entity that has some attributes or properties. Class is a prototype, blueprint or template on the basis of which objects are created. An object cannot exist without a class.
“Inheritance in object-oriented system means defining one class in terms of another. The new class defined is known as the child class or subclass or derived class. The class in terms of which the new class has been defined is known as the parent class or super class or base class. Each subclass inherits state and behavior from the super class. However, subclasses are not limited to the state and behaviors provided to them by their super class. Subclasses can add attributes and methods to the ones they inherit from the super class. Subclasses can also override inherited methods and provide specialized implementations for those methods.” (A. Rajesh, 2006)
In the OOP concept in Java inheritance can be divided into major 4 types. Such as
- Single level single inheritance: – In single level single inheritance there will be a one child class for one parent class.
- Single level multi inheritance: – In single level multi inheritance there will be more than one child class for the parent class.
- Multi-level single inheritance: – In multi-level single inheritance there can be one child class for every parent class in different levels.
- Multi-level multi inheritance: – In multi-level multi inheritance there can be more than one child class for every parent class in different levels.
Characteristics of inheritance
- Will inherit the sub class from the super class or child class from the parent class.
- Sub class can have the common attributes and methods of the super class.
- Can have different levels of inheritance.
- Sub classes will access the super class according to the access modifier which was set to it.
- This will form the generalization and specialization in common state and behaviors.
Purpose of using inheritance
- Used to create super class and sub classes into the program.
- It will allow the programmer to reuse the code from the base class.
- Easy to make changes to the global classes by changing the base class.
Example of inheritance
Forming of sub classes from super class is considered as the inheritance. Here the super class and the sub class will have the common attributes and also can have the unique attributes. When taking the persons as the super class the person can be divided into students and employees which can be derived from the person class. Here the both student and employee will have common states of the human being and unique states as student ID and employee ID and also the common and unique behaviors.
“It is a feature, which lets us create functions with same name but different arguments, which will perform differently. That is function with same name, functioning in different way.” (studytonight.com, 2017)
Characteristics of polymorphism
- The program will have the same method names within it.
- Different types of parameters can be passed within the methods.
- It will have different method signatures within the program.
- This will form the method overloading mechanism within the system.
Purpose of using polymorphism
- Used to create method overloading function in the system.
- Used to create the methods with same method name by using the different method signature.
- Used to make the parameter passing concept into the program.
Example of polymorphism
Having same method name in a system is considered as polymorphism and it will be called as the method over loading. When taking the dog as the class and the dog will make sound woof in normal condition and it will make sound whimper when injured the polymorphism concept can be used as follows.
“Encapsulation is the process of binding both attributes and methods together within a class. Through encapsulation, the internal details of a class can be hidden from outside. The class has methods that provide user interfaces by which the services provided by the class may be used.” (tutorialspoint.com, 2017)
Characteristics of encapsulation
- It will hide the internal functions of the system from the users.
- The user will feel the outside view of the system and can’t find out the internal mechanism of the program.
- Can be the blue print of the system.
- It will reduce the complexity of the program to the users.
- It will be worked as like as the capsule tablet in programming.
- Used to protect the coding of the system from the external users.
Purpose of using encapsulation
- Used to hide the codes from the users.
- Used to reduce the complexity of the program.
- It will only display the outer functions of the system to the users.
- It will be worked as the blue print of the devices.
Example of encapsulation
When taking the same example car and the ATM machine the hiding of the internal function is considered as encapsulation and the outer look for the users will be considered as abstraction.
Inheritance, polymorphism and encapsulation are the key principles of JAVA that are the foundation of the language. There are some other principles such as abstraction, data hiding that are less important. JAVA revolves mainly around these three basic principles.
“Abstraction is a process of exposing essential feature of an entity while hiding other irrelevant detail.” (Navneet Sinha, 2016)
Characteristics of abstraction
- It can be concluded as the different view sight of different users for a same thing.
- This will hide the unwanted data to the users according to their user types.
- This will be used to assign different user accounts in a program.
- It will only show the necessary features to the users.
Purpose of using abstraction
- It will be used to set the access modifier to the system.
- It will be used to allocate different user accounts to the system.
- Used to hide the unwanted parts from the users.
- Used to reduce the complexity of the system for the users.
Example for abstraction
In real life example abstraction can be done to hide the unwanted details to the users. When taking the same car as the example only the users know how to ride it and its external features and they didn’t know how it is function. When taking the ATM machine as the another example the users only know it can be used to withdraw, deposit money and to receive the receipt and they didn’t know how it is function.