Abstractions, Class and Object. What relation?

Suppose you were asked to describe your lovely car you just bought. You would certainly mention the most important properties according to you.
You would, for instance, stress out the high speed it can reach if you are one of the speedy-cars geeks. Or how spacious it is if you are blessed with one of those large families. Just out-of-university engineer, you would talk about every piece its motor contains and how aerodynamics rules would have been better applied to gain more speed. Or greeny activist, you would just mention the electrical-hybrid motor it contains and its befits for your backyard garden.

you got it :)

Discarding unimportant properties or parts while describing your car, is called abstractions. So, for this simple car example, many abstractions may be done depending on your mechanical skills, visual importance, financial side and many other considerations. Abstraction of the same car differ, hence solving the same problem would practically lead to different solutions.
Abstraction is a tricky and diversion-prone step when developing software.

Java is an Object oriented Programming language(OOP), that means any programming problem can be modeled as one or more objects. So an object models an abstraction by defining the properties and functions making that object different from others. You abstract your car as an object with red-color property and letting you reach you work smoothly each day, as a function. Or as an object being of those luxury Ferari's and having that ghost-oriented shape as properties while operating as a social status need.

let's Java a bit:
Properties of an object are also called attributes and are defined in Java with fields. Functions, also known as operations, are called methods.
Collectively fields and methods in a class are called members.
We may categorize objects to the same class: Car is the class of all cars in our example. nicely this matches the Java naming: class

Let's recapitulate:
Java is an OOP language. A class defines a category of objects. Fields and methods are defined by a class which acts as a footprint for its objects.


Now, let's create our first Java class and give it the name Car:

class Car {

}
class is a Java keyword, it is mandatory and put just before the class name (here, Car). The class scope begin with { and ends with }. They are both mandatory.

From now on I'll write comments within the code itself. // denotes that this code line is a comment. Comments within Java code are for human use and are ignored by the compiler.
class Car { // begin of the class scope

// The constructor. having one String parameter: initialColor
public Car(String initialColor)
{
// Still empty
method body for now.

}
// end of the constructor scope

// Another method with one String parameter: newColor
public void changeColor(String newColor)
{ //begin of changeColor method scope

// Still empty method body for now.

}
//end of this method scope

} // end of the class scope
Car(String initialColor) and changeColor(String newColor) are both Java methods. As with the class, { and } define method's scope.
Generally, a method may have any arbitrary name, by convention, it is a verb or verbal phrase ( ex: changeColor(), startEngin()), followed by its zero or more parameters between enclosing parentheses ( ). Parameters in our example are: String carColor and String newColor.

String is a Java keyword denoting a type. initialColor and newColor are arbitrary parameter names you may choose freely.

As you can see, the method Car(String carColor) has the same name as the class name. In Java, this special method is called a Constructor and is used to create (instantiate, see post) objects from the class.


class Car
{

// carColor variable declaration
String carColor ;

// The constructor method
public Car(String initialColor) {

//
The value of initialColor variable
// is assigned
to carColor variable.
carColor = initialColor ;

}


public void changeColor(String newColor )
{


// carColor variable is assigned the value of newColor
carColor = newColor;

}

}

In this class, we declare one instance field. We choose its name to be initialColor denoting a variable that will hold the car color value. Its type is String.

String carColor; is called in Java a declaration. The semicolon at the end is mandatory indicating the end of the declaration;
Generally, you may declare as many fields as you wish.

see u next time :)


1 comment:

Anonymous said...

you are soooooo clear

love your writing mate