Object Oriented Programming System java
OOPs (Object Oriented Programming System):
Object means a real-world entity. Object-Oriented Programming is a methodology or paradigm used to design a program using classes and objects. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs
It simplifies the software development and maintenance by providing some concepts like
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Object-Oriented Programming features:
First let us discuss what is class and Object before going to oops features
Object: Any entity that has state and behavior is known as an object.It is a bundle of data and its behaviour(often known as methods).
Examples of states and behaviors:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
So if I had to write a class based on states and behaviors of House. I can do it like this: States can be represented as instance variables and behaviors as methods of the class. We will see how to create classes in the next section of this guide.
Class: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 Machine that has two data members (also known as fields, instance variables and object states). This is just a blueprint, it does not represent any Machine, however using this we can create Machine objects (or instances) that represents the Machine activities.
Example to understand class and object
public class Machine {
//fields (or instance variable)
String machineName;
int machineAge;
// constructor
Machine(String name, int age){
this.machineName = name;
this.machineAge = age;
}
public static void main(String args[]){
//Creating objects
Machine obj1 = new Machine("Crusher", 5);
Machine obj2 = new Machine("Mixer", 18);
//Accessing object data through reference
System.out.println(obj1.machineName+" "+obj1.machineAge);
System.out.println(obj2.machineName+" "+obj2.machineAge);
}
}
//fields (or instance variable)
String machineName;
int machineAge;
// constructor
Machine(String name, int age){
this.machineName = name;
this.machineAge = age;
}
public static void main(String args[]){
//Creating objects
Machine obj1 = new Machine("Crusher", 5);
Machine obj2 = new Machine("Mixer", 18);
//Accessing object data through reference
System.out.println(obj1.machineName+" "+obj1.machineAge);
System.out.println(obj2.machineName+" "+obj2.machineAge);
}
}
Output: Crusher 5
Mixer 18
Mixer 18
No comments