In this post, we will deal with the basic OOPs Concepts in java (Object-Oriented Programming system). OOPs is a programming paradigm which is based on objects. It deals with other concepts such as data binding, polymorphism, and inheritance.
The first object-oriented programming language is Simula but the first truly programming language to be fully based on an object is regarded as Smalltalk. The main concept of OOP is that everything in the language is represented as an object. Some of the other popular object-oriented programing languages are C#, Python, PHP, and C++ and Java.
The main goal of OOPs concepts is to implement real-world entities such as classes, inheritance, abstraction, polymorphism, and objects.
Object-oriented Programming System (OOPs Concepts)
An object can be simply defined as any real-world entity such as pencil, paper, clothe, and so on. Object-oriented programming helps to design a program with the use of objects. This makes the development and maintenance of software easier. Some of the concepts that OOP deals with are:
Inheritance is known as the phenomena in which an object gains properties of the parent object. It helps to use the code again and again and achieve runtime polymorphism.
Here, the superclass is those classes whose properties are inherited and subclasses are those classes that inherit the properties of the superclass. With the use of inheritance, we can easily reuse the field and methods of the existing class to create a new class.
This phenomenon is known as reusability. Inheritance helps to manage the information in a hierarchical order. There are five types of inheritance in Java and they are, Single Inheritance, Multilevel Inheritance, Hierarchical Inheritance, Multiple Inheritance, and Hybrid Inheritance.
Polymorphism is the process of performing a task in various different methods. Method overloading and overriding are generally used in Java to achieve polymorphism.
Polymorphism can be defined as the ability of an object that helps them take various different forms. It is mostly used in OOP when a child class object is referenced by using a parent class.
Any object that can pass more than a single IS-A test is known to be polymorphic. All objects in Java pass this test so they are all polymorphic.
The reference variable is the only method to access an object and the reference variable can only be of a single type. The kind of reference variable cannot be changed once it is declared.
Abstraction is the process of keeping the internal details a secret and only making the functionality visible. It is the quality of dealing with only ideas, not the events.
In other words, it can be defined as a process to keep the implementation details a secret and only make the information about the functionality available.
It helps the user to understand the function of the object rather than how the object performs its function. Abstract class and interface are used in Java to achieve abstraction.
Encapsulation is the process of wrapping all the code and data and making a single unit from them. In it, the variable of a class will only be accessible through the methods of their current class and are hidden from other classes.
It works as a kind of a shield to protect data from being accessed by the code outside. It is also known as data-hiding as it hides the data in a class from other classes.
It is achieved by declaring all the variables in the class as private and writing public methods to find out the values of the variables. Java bean is an example of such encapsulation as all data members are private here.
Object and Classe in OOPs concepts:
In simple words, an object is any type of physical and logical entity that has state and behaviors. Car, house, computer, mobile etc. are some common examples of objects in the real world. The objects in the real world and the software objects have the same characters, that is, state, behaviors, and identity.
State of an object is represented through attributes and reflects the features of an object. On the other hand, a behavior of an object is represented through methods and reflects the response of an object with other objects.
Similarly, identity refers to the unique name of the object that differentiates it from other objects and helps in the interaction between objects. An object resides in memory and has an address. They are an instance of a class that can communicate without the knowledge of data or code of each other.
A class is a logical entity which has more than one object. Class doesn’t take up any space and are the collection of objects. They are the blueprints or prototypes which are used to make a single object. The features or methods that are common in a type of objects are represented by a class.
A class has default access and its name begins with an initial letter. A class contains three types of variables and they are, Local variables, Instance variable, and Class variables. Local variables are those variables that are inside methods and they are destroyed when the method gets completed.
Instance variables are variables that are outside a method but are accessible from inside any method. Finally, class variables are variables that have static keyword and are outside a method.
# Advantages of OOP over Procedure-oriented programming language
- OOPs makes it easier to develop and maintain a software, unlike Procedure-oriented programming language in which it becomes difficult to manage the software as the number of code increases.
- In OOPs, data can be hidden but in the Procedure-oriented programming language, the data can be accessed from anywhere.
- OOPs provide solutions to real-world problems and it has the ability to simulate real-world events.
# Dissimilarities between an object-oriented programming language and an object-based programming language
# Java Naming Conventions
Java naming convention is the process of providing the identifiers unique names such as variable, package, class, method, and constant. It is not compulsory to name the identifiers so it is not called rule and is known as conventions.
The names of all the interfaces, methods, classes, fields, and packages of Java programming language are decided on the basis of the Java naming convention.
# Advantages of naming conventions in java
Java naming conventions make it easier to read the code. It helps to save programmers time while reading the code and figuring out the functions of each code.
Guidelines while using naming conventions:
Class name: A class name should always be a noun and should start with an uppercase letter.
Interface name: An interface name should be always an adjective and should also start with an uppercase letter.
Method name: A method name should always be a verb and should start with a lowercase letter.
Variable name: A variable name should start with a lowercase letter.
Package name: Like the variable name, a package name should also start with a lowercase letter.
Constant name: A constant name should have all uppercase letters.
# camelCase in Java Naming Conventions
In Java, camelcase syntax is used to name class, method, interface, and variable. In this syntax, if a name is composed of two different words then the second word starts with an uppercase letter.