PSYCHOCODES

Explaining the Object Oriented Programming Concepts in Java

Procedures......It's a word which defines a Procedural Programming Language like C where we have to make procedures for each and everything and divide our whole code into separate procedures which is quite a hard stuff to do but the introduction of OOP has made things a lot easier for programmers nowadays.

Explaining OOP in java

In object oriented programming there is no need to define separate procedures, in OOP we uses objects (No need to worry we will discuss objects later in this article) which have all the properties of a Class, In OOP programmers not just define the data type of a data structure but also the operations that are going to be implemented on those data types. Now let us see what Classes and Objects are:

Class 

A class combines the data members or instances and the member functions or methods which are going to be operated on to the given data members under one roof and acts as a blueprint to an object. Let us see an example based on class.

class dog{
int height;
String name;
void display(){
System.out.println("Name: "+name+"\n"+"Height: "+height);
}
}

In the above example the class dog has data members height and name and to display the data it has a member function called display().

Objects

Objects are used to access the members of a class, an object contains all the property of its respective class. Let us see an example based on objects

class dog{
int height;
String name;
void display(){
System.out.println("Name: "+name+"\n"+"Height: "+height);
}
}
public Sampleprogram(){

public static void main(String[] args){
dog s1 = new dog();   //new object created of class dog
s1.height = 3; //calling instance
s1.name = "Tyson";
s1.display();    // calling method using objects
}
} 

Now we will see some of the major principles of OOP

1.Encapsulation

Encapsulation is the binding of data and it's operations under a single roof ,Encapsulation does not give direct access to the data members to the outside world which makes the data secure ,For example, Classes follow the principle of Encapsulation.

2.Data Abstraction

Data abstraction refers to the act of representing the essential features without showing the background details. For example, we want to withdraw some amount from an ATM machine, what we do is we insert our card follow the steps on the screen and get the money but did the atm machine shows you the background processing of the data you enter on the ATM machine screen? this is known as Data Abstraction.

3.Polymorphism

 Polymorphism means that an instance or method can behave in different forms according to the behaviour of the calling method, see the code snippet below for better understanding.

class dog{
void dogSound(){
System.out.println("Woof");
}
void dogSound(bool injured){
if(injured)
System.out.println("Weep");
}
else{
System.out.println("Woof");
}
}

 In the above example, we can see how the methods with the same name can act according to the condition, this is known as Polymorphism.

4.Inheritance

Inheritance is the property by which an object can inherit all the properties of an another object or one class can inherit the properties of another class.

So these were the Major Principles of Object Oriented Programming, Thanks for reading if you like this article the please share it and help us grow.

If you have any queries then comment them down below or you can send your queries to us on our twitter handle @PsychoCodes or on our Facebook page and do not forget to subscribe to our newslettersmile.

Share your thoughts