When someone talks about physical objects, you quickly visualize their characters and features. e.g. when we think about Chair, we can answer quickly: what is it for (the purpose)? what are its dimensions and material used likewise? Similarly, in programming languages, we create classes which provide the blueprint of its features (properties/fields/variables it has) and capabilities (methods) what it has offered to do. All this comes under object-oriented concepts. Java OOP concepts are very important to understand and to become a great java developer.
Java is an object-oriented language. Though it is not pure object-oriented. Java OOP concepts as mentioned below:
JAVA OOP
1. Polymorphism
2. Encapsulation
3. Abstraction
4. Inheritance
5. Classes
6. Objects
7. Methods etc.
To understand, java OOP concepts we will dive deep with Classes and Object in java.
CLASS
It is a blueprint object. It is the definition of its object like what are their properties? what is it for? what can you do with it?. A class definition can have any number of properties and methods. Though it is advisable to keep the class as simple as possible for the same of maintaining and reducing complexities. A class can have
- Local variables (specific to control block defining it, e.g. variables defined inside methods), These variables can not be used or accessed outside of the block defined them. The local variable is created for local computation purpose only and has no importance outside methods.
- Class variable (applies to all its objects. We define such variable through static keyword.): The class variable has common memory allocation for all objects and in fact, they created even before any object is created and to access them you are not required to create an object of that class. You can access them using the "dot operator" like ClassName.staticVariableName. One should pay attention to choosing a variable to be class level as it comes with challenges too and inappropriate use can lead to complicated situations and problems.
- Instance variable (specific to its object): these variables are defined in class definitions. These variables hold properties specific to each instance of that class. Changing these variables will only change the properties of that instance only. To access instance variable you are required to create an instance of that class first. These are used to define the state of an object.
- Methods: Class can have any number of methods. Through methods, other objects can communicate and process information/data of this class's object. You can write the logic in the methods and expose it.
EXAMPLE
To explain above concepts lets, take an example of class
public class Dog{
public String color; //instance variable
public int age;
private int height;
public static int MAXAGE=20; // class level variable
public Dog(){ }
public Dog(String color,int height, int age){
color = color;
height=height;
age=age;
}
public String getColor(){
return this.color;
}
public String getAge(){
return age;
}
}
Now Class Dog defines 3 object properties, color, age, height, and 1 class variable /property. All the properties/variables are instance variable. Now through methods getColor() and getAge() you can check each Dog's color and age. private and public keywords are access-modifiers.
Also, notice method "Dog()" this is a class constructor. Through constructors method, you can create an object of the class and initialize them. A class can have multiple constructors. It is through the constructor, you can control the behavior of object creation.
OBJECTS
In my view, the object is a class in memory just like "a process is a program in execution". What I mean is, the object of the class holds concrete state values and behavior. e.g.
Dog jacky = new Dog("black",5,15);
Now we are doing three things in one line:
- declaring object jacky of class Dog through "Dog jacky"
- instantiation: we are creating an object of class Dog through "new Dog(...)"
- initialization: now in the final step we are initializing object jacky.
ACCESSING INSTANCE VARIABLES AND METHOD
You can access instance variable using objects only like jacky.color would return the value of color instance variable.
You are not required to create an object to access the class variable. Dog.MAXAGE would return 20
The way variables are instance level and class level, we have methods which are also instance level and class level. you can access the method in the same way as jacky.getColor() would return the color of the object jacky.
Hope you guys liked the article. Please share and provide suggestion in the comment section


















