A Simple Explanation of OOP
You already know how you can declare variables and assign values to them.
For example, you can declare a variable called x
.
int x;
And you can assign the value 5 to the variable x
.
x = 5;
You can also declare a variable called y
.
String y;
And you can assign the value "hello world"
to the variable y
.
y = "hello world";
The values you assigned to these two variables were of different types.
The variable x
was assigned a value of 5
, which is of type int
.
The variable y
was assigned a value of hello world
, which is of type String
.
Both of these types are defined in the Java language.
But the Java language has a way for you to define types as well.
You can define the structure of values of type Car
by defining a class.
class Car {
int mileage;
String name;
}
You can then declare a variable of type Car
.
Car mySedan;
You can then assign a value to the mySedan
variable.
mySedan = new Car();
And that’s object-oriented programming (OOP) — creating classes that define a type of values.