OOPS( Object-Oriented Programming ) Concept in Java :
In above class , we have define 2 numbers one is holding 10 and another is holding 20 and a method to add 2 numbers . So we can say this class has 2 properties and one behaviour which can be accessed only though Object of class FirstJavaProgram , as I already mentioned class is just blueprint representation of properties and behaviours of its own Object, properties and behaviours of any class can only be accessed after creating object of the class except static properties and behaviours which I already explained in my previous post BASIC OVERVIEW OF JAVA (SESSION 1).
Memory management and garbage collector we will understand in deep once we will start JVM memory series. Just think that everything created in Heap memory while stack memory keep the reference and call stack only.
As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.
So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post BASIC OVERVIEW OF JAVA (SESSION 1)
Let's understand through an example :
public class FirstJavaProgram {
int firstNumber=10;
int secondNumber=20;
public int sum(int fNum, int sNum){
return fNum+sNum;
}
public static void main(String[] args) {
//our logics
}
int firstNumber=10;
int secondNumber=20;
public int sum(int fNum, int sNum){
return fNum+sNum;
}
public static void main(String[] args) {
//our logics
}
}
Now let's understand how Java internally creates object of any class and where its stored and how we are able to access these properties and behaviours via Object.
To understand this, just consider that you have a book and and every book has index page denotes actually content of each chapter via page number .
In Java, we have JVM(Java Virtual Machine) which has 2 type of memory to handle Object and its properties and Behaviours.
- STACK MEMORY
- HEAP MEMORY
There are also some other memory section of JVM which we will discuss later but for now focus on this 2 memory(STACK and HEAP).
As I explained that Java program starts from main method, so internally JVM creates a main Thread and one STACK memory for main thread (Threading concept will learn in details once we will start Thread series).
Now consider we have created an Object of our class FirstJavaProgram, so object and its contents and all other stuff mentioned inside a class will be created in HEAP memory while STACK memory will have only reference to these properties and behaviours just like below .
Although there is slight difference in case of primitive but for simplicity i have explained the way actually object, properties and behaviours got created in JVM.
So as per above digram our program should be as below.
public class FirstJavaProgram {
int firstNumber=10;
int secondNumber=20;
public int sum(int fNum, int sNum){
return fNum+sNum;
}
public static void main(String[] args) {
FirstJavaProgram obj=new FirstJavaProgram();
int firstNumber=10;
int secondNumber=20;
public int sum(int fNum, int sNum){
return fNum+sNum;
}
public static void main(String[] args) {
FirstJavaProgram obj=new FirstJavaProgram();
int result=obj.sum(obj.firstNumber, obj.secondNumber);
}
}
Finally 30 will be written to the result variable. This is all about how JAVA and JVM processes our objects and its properties and Behaviours.
Need of OOPS concept :
Now to make use of properties and Behaviours of an object inside other Object we have OOPS concepts called INHERITANCE ( via extends )
To make use of properties and Behaviours of an object restricted, we have OOPS concepts called ABSTRACTION (( Interface is for 100% abstraction and Abstract class is for partial abstraction ))
To make use of properties and Behaviours of an object behaved differently based on input param, sequence of param, method signature etc we have OOPS concepts called POLYMORPHISM (like Method Overloading and Method Overriding ).
To make use of properties and confidential Behaviours of an object private and access via only common public method is called ENCAPSULATION ( wrapping all the required things of a class in single unit )
As mentioned above 4 OOPS concepts we will explained each OOPS concepts in details in our coming post. If you have any doubt or confusion please send us your queries via submit your queries.
OOPS CONCEPT :
We will also insist you to go through our basic java overview concept before OOPS concepts...
- BASIC OVERVIEW OF JAVA (SESSION 1)
- PRIMITIVE TYPES AND WRAPPER CLASS (SESSION 2)
- JAVA OPERATORS (SESSION 3)
- LOOPS IN JAVA (SESSION 4)
- NAMING CONVENTION WITH FACTORIAL PROGRAM (SESSION 5)
Comments
Post a Comment