Programming
Java Basics
Contributors:
Hamid Hussain, Austin Benedicto
Hamid Hussain, Austin Benedicto
OOP also known as Object Orientated Programming is a core concept that we advantage of in our program and it carries many advantages to it. In a short sentence basically think of everything as an object and that those objects can do stuff.
This article will tell you everything that is crucial with understanding what OOP is, its advanced ideas, and how to make use of it. Even though OOP is a concept not language specific it will be taught in java
Remember if you want to understand this even better take the CS courses at Nichols.
Object Orientated programming is kind of a weird idea to first wrap your head around at first but eventually becomes easy. In Java you have classes which are blue prints for objects, methods which are verbs, and variables which are attributes. Now what does that actually mean.
public class Dog
{
int age;
double weight;
double height;
String name;
public void run(){}
public void jump(){}
public void eat(){}
}
Lets start with an example of a dog. A dog is an object and it can have methods like run, jump, play, eat and then the dog can have attributes like age, weight, height, name.
These are all traits that a dog can have and that they belong to the dog. you can just use these methods outside of the dog without calling what a dog is in the first place.
Now where this becomes super useful is if I want to have say like 10 dogs now I just take this class called dog and make 10 dogs because the class is just a blue print for an object NOT an actual object. But this code strip as of right now will create 10 dogs that are exactly the same because there is no way to differentate between them, so how do we do that?
We can differentiate them by adding a constructor. This is how we are able to change the attributes of the class.
public class Dog
{
int age;
double weight;
double height;
String name;
public Dog(int dogAge, double dogWeight, String dogName)
{
age = dogAge;
weight = dogWeight;
name = dogName;
}
public void run(){}
public void jump(){}
public void eat(){}
}
Here we have a code sample that uses all three of these datatypes. More specifically, this code declares three variables, one for each datatype.
The first one is an integer, and is named "age".
The second one is a double, and is named "mass".
The third one is a float, and is named "distance".
After declaring each variable, the code assigns a value to each. Here, it's easier to see the differences between the datatypes.