OOPs concepts in Java
- Object-oriented programming System(OOPs) is a programming paradigm based on the concept of “objects” that contain data and methods.
- The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs.
- Object oriented programming brings together data and its behaviour(methods) in a single location(object) makes it easier to understand how a program works.
What is an Object
Object: is a bundle of data and its behaviour(often known as methods).
Objects have two characteristics: They have states and behaviors.
Examples of states and behaviors
Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
class House {
String address;
String color;
double are;
void openDoor() {
//Write code here
}
void closeDoor() {
//Write code here
}
}
Characteristics of Objects:
Abstraction: Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
Encapsulation: Encapsulation simply means binding object state(fields) and behaviour(methods) together. If you are creating class, you are doing encapsulation.
Message passing
A single object by itself may not be very useful. An application contains many objects. One object interacts with another object by invoking methods on that object. It is also referred to as Method Invocation. See the diagram below.
What is a Class in OOPs Concepts
- A class can be considered as a blueprint using which you can create as many objects as you like.
- For example, here we have a class Website that has two data members (also known as fields, instance variables and object states).
- This is just a blueprint, it does not represent any website, however using this we can create Website objects (or instances) that represent the websites.
- We have created two objects, while creating objects we provided separate properties to the objects using constructor.
public class Website {
//fields (or instance variable)
String webName;
int webAge;
// constructor
Website(String name, int age){
this.webName = name;
this.webAge = age;
}
public static void main(String args[]){
//Creating objects
Website obj1 = new Website("beginnersbook", 5);
Website obj2 = new Website("google", 18);
//Accessing object data through reference
System.out.println(obj1.webName+" "+obj1.webAge);
System.out.println(obj2.webName+" "+obj2.webAge);
}
}
Output:
beginnersbook 5
google 18
Static Class, Block, Methods and Variables
- Static keywords can be used with class, variable, method and block. Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without an object.
Here we have a static method myMethod(), we can call this method without any object because when we make a member static it becomes class level. If we remove the static keyword and make it non-static then we must need to create an object of the class in order to call it.
Static members are common for all the instances(objects) of the class but non-static members are separate for each instance of class.
class SimpleStaticExample
{
// This is a static method
static void myMethod()
{
System.out.println("myMethod");
}
public static void main(String[] args)
{
/* You can see that we are calling this
* method without creating any object.
*/
myMethod(); }
}
Static Block
Static block is used for initializing the static variables.This block gets executed when the class is loaded in the memory.
A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
Multiple static block execute in the given order which means the first static block executes before second static block. That’s the reason, values initialized by first block are overwritten by second block.
class JavaExample2{
static int num;
static String mystr;
//First Static block
static{
System.out.println("Static Block 1");
num = 68;
mystr = "Block1";
}
//Second static block
static{
System.out.println("Static Block 2");
num = 98;
mystr = "Block2";
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
System.out.println("Value of mystr: "+mystr);
}
}
Output:
Static Block 1
Static Block 2
Value of num: 98
Value of mystr: Block2
Java Static Variables
A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of a static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.
Few Important Points:
- Static variables are also known as Class Variables.
- Unlike non-static variables, such variables can be accessed directly in static and non-static methods.
Java Static Methods
Static Methods can access class variables(static variables) without using object(instance) of the class, however non-static methods and non-static variables can only be accessed using objects.
Static methods can be accessed directly in static and non-static methods.
Syntax:
Static keyword followed by return type, followed by method name.
static return_type method_name();
Static Class
A class can be made static only if it is a nested class.
- Nested static class doesn’t need reference of Outer class
- A static class cannot access non-static members of the Outer class