SEARCH W7R

Sunday, February 05, 2012

Objects and Classes in Java: The Complex Data Type

The idea and use of CLASSES, and OBJECTS are frequently over-complicated. In this Java post, I will show you how simple, but important the concept of objects and classes is. We will explore the ideas behind O.O.P. (Object Oriented Programming), also called O.O.D.  (Object Oriented Design) and expose one of the most important concepts of modern day programming.

OBJECT ORIENTED DESIGN (O.O.D.)
~PROGRAMMING in Java
Everything starts with a problem. How does a programmer create variables other than the primitive types (integers, characters, doubles, booleans, longs, ect.)? The answer is surprisingly simple: Combine and mix any number or types of primitive variables and place them all in a container. This container would be the OBJECT, while the contents would be the OBJECTS' LOCAL VARIABLES. In Java programs, object variables are called CLASS VARIABLES! So, class variables are the same thing as object local variables. Knowing that class variables are the contents of an object variable in Java, you may already have an idea of what an object in Java programs is: CLASSES. 


A drawing of what a real Dinosaur would look like (Triceratops)

public class Dinosaur{
//What variables does a dinosaur need?
}

public class Dinosaur{
//here's a few for starters.
public int length, height, weight;
}

As you can see (above) my Dinosaur class has 3 class variables, in which any of the 3 can be thought of as a trait that better describes a Dinosaur. The more class variables a class contains, the closer it can emulate an actual dinosaur of the Jurassic period. Of course, these class variables need to be appropriate for the subject at hand; like, I would not include a variable such as a String named bestMagicTrick. That'd be a new skill of dinosaurs... that's for sure.

All classes of a Java program extend a class called Object. So, every class variable can also be referred to as a object, but more often an instance of a class. 

The primitive variables have a class version for their uses such as changing a integer into a String. The int keyword is equivalent to an instance of the Integer class (Integer.java). To create an integer from a string you use the parseInt(String s) method of the Integer class.

Integer.parseInt("564") = = 564 should be TRUE! Check it out yourself.

Back to the thought of emulating real life into computer programs, our Dinosaur class could use some ACTIONS. These actions we will call methods (sound familiar?) For example, a dinosaur would have an eat method, a move method, a roar method.



public class Dinosaur{
//here's a few for starters.
public int length, height, weight;


public void roar(){
//roar method contents
}
public void move(String direction, double length){
//move method contents
}
 public void eat(){
//eat method contents.
}
}


Our dinosaur object is more of a dinosaur than it has ever been. To create an instance of a Dinosaur class we simply write Dinosaur dino1 = new Dinosaur();

I am eager to go further into this topic of Objects and Classes, but I think this is enough for one post. I hope you enjoyed the lesson!
 -Brian

MORE RESOURCES ON OOP/OOD in JAVA

No comments: