An object in Java represents an entity, a actual item. Objects have methods and attributes that can be accessed. It is the instance of a class.
A object consists of 3 main things:
State: It is essentaially the attribiutes of an object.
Behavior: The behavior of an object is represented by the methods within it, what does the object do?
Identity: The name of the object (HAS TO BE UNIQUE), the profle of the object.
KEY EXAMPLE:
Think of a cat: it has attributes, behaviors and identity, we can create a Cat object like:
STATE: Color, Breed, Age, BEHAVIORS: Meow, Eat, Jump, IDENTITY: The Name Of A Cat, maybe George?
A class is something in Java where you can actually create that object as well. It's essentially a blueprint or template of an object that is waiting to be manipulated/used. A class will initialize the methods the object can do and the attributes that accompany it.
Always declare your class first before doing anything!!
To declare your class the syntax is:
public class "className"
{
}
Private instance variables (or PIV's) are private as they can only be accessed by the class. It cannot be accessed "freely" outside of the class, the only ways to access it is adjust the class or use a class method that accesses it. To go to the PIV's you gotta get through the class. PIV's itself are basically the first thing taht make sup an object: the state or mroe specifically tehe attributes. This is where you will declare the attributes of the object, like a feature or quality of an object.
The syntax would be: private DATA TYPE "attribute name" ;
Examples can include private int cost;
A constructor is a unique method that intializes the actual object itself with all or none of the attributes within it.
Default constructors intialize default values for the PIV's/attributes, so if a person creates an object without initializing the values for the PIV's, it would create a default value for all the attributes.
The syntax for it is:
public CLASS NAME()
{
PIV = Your default value. //this will intialize your value
}
Regular constructors intializes values for the PIV's/attributes in terms of parameters. This would allow the user to create their own values for each of the PIV's/attributes. So if a person creates an object, they would have to initialize for each PIV to create it.
The syntax for it is:
public CLASS NAME(parameters (e.g int y))
{
PIV = A parameter from up top. //sets to the suer's intialized value
}
COOL TIP!
If you want to make your parameter the same name as your PIV, just do this PIV = PIV;
Mutator methods: These are usually called setters as the method allows you to change a PIV's value. To create, you have to have a void method with a single parameter, make sure it's the same data type as your chosen PIV. Make a statement intializing the PIV to that same parameter.
The syntax for it is:
public void setPIVName (your parameter)
{
PIV = parameter; //this will intialize a NEW value.
}
Accessor Methods: These are usually called getters as this method allows you to access a PIV's method, this is helpful if youa re printing a PIV. TO CREATE, you have to have a non-void method specified to the PIV's data type, in Methods more explanantion is how about non-void methods, and within the code you will have to have a statemnt returning the actual PIV, so just retruning it as it is.
public void getPIVName
{
return PIV; (this will return the actual value of the PIV in the client)
}
The mthods within the class usually acts as the behaviors/functions of the object itself. Whether it's a robot moving, or a cat meowing, a method can have the object do soemthing, or even manipulate itself. There are no set way to creating methods in classes and objects. But, here are some examples of methods.
Here it checks if the status int PIV is at a specific threshold. Whatever threshold it lands within is WHAT IT WILL PRINT.
A toString() is basically a method that is what sprouts out when you return the the whole class (or the whole class). Inside the ToString is exactly what it'll return to.
Polymorphism is when one class can become another. "Poly" means many and morphism means"form". Basically it means a class can come into many forms. For examplle, take Class A for example,