OOP - Objects and Classes
Classes
Classes and objects are different terms
Classes are an object type.
You can create as many objects as you want of a class
For example if a class has many fields and one of those is country, then an object would be Australia.
In summary Objects are to Classes what Variables are to Data types.
For example
class Pokemon {
String name; //Field
String type; //Field
int health; //Field
boolean dodge(){ //Methods
return Math.random()>0.5;
}
}
In Java each class should be created in its own file and have the extension ".java"
For examples classes might be vehicle.java or map.java
________________________________________
The main method:
This is where the program starts
Inside it you can create objects and call methods to run other parts of your code.
As defined by :
public static void main (String [] args){
//start program here
}
//public: means you can run this method from anywhere
//static: means it does not need an object to run
//void: means that this method does not return anything
//String [] args: is the input parameter(array of strings)
__________________________________________
Constructors
These are special type of method that are responsible for creating and initializing an object of that class.
When creating a constructor it is much like a method but:
1. They do not have return types
2. They have the same name as the class itself
However they my take input parameters like a normal method and you are allowed to create multiple constructors with different input parameters.
An example of a constructor is as follows:
class Game{
...
//Constructor
Game(){
//initialization code here
}
....
}
When referring to a default constructor it is a constructor that does not take any input parameters.
Classes and objects are different terms
Classes are an object type.
You can create as many objects as you want of a class
For example if a class has many fields and one of those is country, then an object would be Australia.
In summary Objects are to Classes what Variables are to Data types.
For example
class Pokemon {
String name; //Field
String type; //Field
int health; //Field
boolean dodge(){ //Methods
return Math.random()>0.5;
}
}
In Java each class should be created in its own file and have the extension ".java"
For examples classes might be vehicle.java or map.java
________________________________________
The main method:
This is where the program starts
Inside it you can create objects and call methods to run other parts of your code.
As defined by :
public static void main (String [] args){
//start program here
}
//public: means you can run this method from anywhere
//static: means it does not need an object to run
//void: means that this method does not return anything
//String [] args: is the input parameter(array of strings)
__________________________________________
Constructors
These are special type of method that are responsible for creating and initializing an object of that class.
When creating a constructor it is much like a method but:
1. They do not have return types
2. They have the same name as the class itself
However they my take input parameters like a normal method and you are allowed to create multiple constructors with different input parameters.
An example of a constructor is as follows:
class Game{
...
//Constructor
Game(){
//initialization code here
}
....
}
When referring to a default constructor it is a constructor that does not take any input parameters.
Comments
Post a Comment