First time here? Please, start at the beginning.

Sunday, August 22

Part 1.1: Basic Java/Programming Concepts

I won't bore you with the distinction between machine language and high-level code. Instead I'll hit some key terms (in no particular order) with a few examples.

Class: A class is basically the same as a "type". It defines a type of object, what it knows (variables) and what it can do (methods). You don't really use a class directly, you create objects from a class and uses those. There are exceptions to this rule, I'll go into a bit more detail later.

Object: An instance of a class. These can be created, destroyed, passed around, etc. If Dog is a class, then lassie is an object of class Dog. Each object has it's own independent copy of the variables defined in it's class. lassie's weight is different from toto's weight, but since the variable weight is defined in Dog you know that they both have a weight.

Function: a named block of code that can be called repeatedly. very similar to a method. in fact, there are no functions in Java, only methods.

Method: a function that is declared in a class, and runs within the context of an object. Example: Dog defines getWeight() (an accessor method, see Encapsulation), therefore you can ask and instance of Dog for its weight. since the method runs in the context of its object, it can return the appropriate weight value.

Encapsulation: the practice of hiding as much of an object's internal data as possible, exposing only what's needed via accessors ("get" methods) and mutators ("set" methods). this allows for validity checking, among other things.

Subclass: a class that extends another class, adding and/or changing some functionality. For example, you have a generic Vehicle class, and subclasses Car, Truck and maybe even Segway. They all have certain things in common, and these common properties can be defined in the "superclass" Vehicle. This is the basis of inheritance.

A quick break from terms to talk about variable types, classes, and objects. The type that a variable is declared as determines what objects it can contain. A variable declared with public Vehicle myRide; can hold an object of type Vehicle or any subclass of Vehicle. This is a very important concept, and allows for...

Polymorphism: Long word for a simple concept: using a subclass as it's superclass. Understand that? Not so much? Here's an example, let's say in your Vehicle class you defined a start() method. Thanks to inheritance, this means that you can tell any Vehicle (even if it's really a Car) to start. Polymorphism is when a subclass changes what is actually does to accomplish the task. How a car starts will be different from how a segway starts, but thanks to polymorphism, you don't have to worry about the details.

Wow that's a lot of stuff... and not too many examples. Check back next post for a nice example of most of these concepts.

No comments:

Post a Comment