A class models a real world object. It is an abstract representation of a real world object. A instance of the class is called an object. Every object has a state and behavior. The properties of a class together represent the state of an object, while the methods represent its behavior.
The property of a class is represented by a variable. A variable is defined by a data type. Depending on whether the property is a text, a number or any other type, the variable is declared with the corresponding data type.
A class is defined within a pair of curly braces. It can have any number of properties and methods.
Let us define a simple Java class. The class represents an item, which has a name, in this case. The name property is declared as a String data type. The Item class does not exhibit any behavior as yet. It has two methods - an accessor method and a mutator method; they operate on the property name.
The design of a class is based on the fundamental principle of data hiding. The properties of a class are declared private, which means they are not directly accessible outside the class. By declaring the data variables private, we ensure that the data cannot be directly modified b other parts of the program.
Encapsulation is a term usually employed to abstract away the state and the behavior of a real world object into a software representation. A class encapsulates the state and the behavior of an object in the world around us.
Java is an object-oriented programming language. The java source code is compiled into an intermediate byte code. The source code has the file name extension “.java” while the byte code has the extension “.class”. The byte code is executed in a Java runtime environment called the Java Virtual Machine or simply the JVM.
Being object-oriented implies that the java language supports the fundamental tenets of object-oriented programming such as:
Objects in the world around us possess attributes and exhibit behavior. In software terms, the attributes are called properties, which define the state of the object. Methods define the behavior. The properties hold the data about the object and the methods manipulate that data. The encapsulation of the state and behavior of an object is called data encapsulation. A java class is an example of data encapsulation.
Inheritance is the ability to derive a new class from an existing class. The new class is called the child class, the subclass or the derived class. The existing class is therefore called the parent class, the super class or the base class. Inheritance allows the subclass to automatically inherit all the properties and methods of the super class. It is also free to extend in order to provide additional behavior. Java supports single inheritance. It means that a class can be derived from only one other class. In other words, the subclass can have only one parent.
Polymorphism is the ability of a subclass to pretend to be its super class: the child poses as its parent. This feature is very powerful and often used to build complex object-object relationships. Many design principles and patterns have evolved from the concept of polymorphism.
The String class represents a collection of characters. A new String object is created using a constructor of the class.
// using a no-argument constructor
String newStr = new String();
newStr = "my new string";
// by passing an argument to the constructor
String newStr = new String("my new string");
The example 2 combines the two statements in example 1.
Any character or number may be converted to a string data type using the concatenation operator +, an operation called stringification.
int n = 2;
char c = 'A';
String s = ""+n+c;
The variable s now contains the string "2A".
The String class has many useful methods.
In example 3 above, concatenation is performed by a shorthand notation using the + symbol. However, in order to join two strings, the concat method can be used.
String s1 = "man";
String s1 = "hole";
String s = s1.concat(s2);
The String s will now contain the new string "manhole".
The length of a string is a measure of the number of characters in it. The length of the String s in example 3 is found using the length method.
int len = s.length();
The variable len holds the number 7, to represent the seven characters in the word manhole.
The position of each character in a string is called index. The index starts from zero. The last character is one number less than the length of the string. In the example 3 above, the first character 'm' is at an index of zero.
A string can also be viewed as an array of characters. each charcater is called an element in the array. The first element has its index at 0.
The indexOf method is used to know the position of a character in a string.
int pos = s.indexOf('m');
The pos variable contains 0.
The last character in the string can be found by using the method charAt().
char last = s.charAt(s.length-1);
The variable last holds the character 'e'.
In order to print all the elements of a string, a loop construct maybe used as follows.
The above code fragment prints each character in a line by itself - one below the other.
The loop starts with element 0 and advances by incrementing the variable i by one position until the value of i reaches a value of length minus one (remember the first element starts at zero).