Introduction
In this lab, you are going to learn a very important programming pattern: the object-oriented-programming. You need to know the difference between class and object. Take more practice to get a better understanding.
In this lab, you are going to learn a very important programming pattern: the object-oriented-programming. You need to know the difference between class and object. Take more practice to get a better understanding.
There are many programming languages and almost as many programming styles (sometimes called paradigms). The programs we have written so far are mainly procedural, because the emphasis has been on specifying computational procedures. Most Java programs are object-oriented, which means that the focus is on objects and their interactions. Here are some of the characteristics of object-oriented programming:
Objects often represent the entities in the real world. The majority of methods are object methods (like the methods you invoke on String
s) rather than class methods (like the Math
methods). The methods we have written so far have been class methods. In this chapter, we will write some object methods.
Objects are isolated from each other by limiting the ways they interact, especially by preventing them from accessing instance variables without invoking methods. Classes are organized in family trees where new classes extend existing classes, adding new methods and replacing others.
In Java, every class extends some class or the other, except the class Object
. The most basic class is Object
class. It contains no instance variables, but it provides the methods equals
and toString
, among others.
Many classes extend Object
, including almost all of the classes we have written and many Java types like Integer
, Double
, String
, array etc. Any class that does not explicitly name a parent extends Object
by default. The โfamily treeโ of classes is called class hierarchy. Object
usually appears at the top, with all the child classes below.
Classes and objects usually have some attributes which are used to describe these classes/objects, and the methods are used to perform some specific functions. Here we give you an example to show you what a class is like, save the code to objectTest.java
and use command line to run the code.
Example:
Write the following code in the /home/labex/project/objectTest.java
file:
// use keyword 'class' to create a new class named Person
class Person
{
/*
* there are four properties,
* name of string type, age of int type
* gender of string type, weight of float type
*/
private String name;
private int age;
private String gender;
private float weight;
// this is a constructor with no return type, the same name as the class, no parameters
public Person(){
}
//this is a constructor with two parameters
public Person(String name, int age){
// use parameters to set object's name and age
this.name = name;
this.age = age;
}
// this is a constructor with four parameters
public Person(String name, int age, String gender, float weight){
// use parameters to set object's name, age, gender and weight
this.name = name;
this.age = age;
this.gender = gender;
this.weight = weight;
}
// this is an object method
public void personInfo(){
System.out.println("Name: " + this.name + "\nAge: "+ this.age + "\nGender: " + this.gender + "\nWeight: "+this.weight);
}
}
public class objectTest{
public static void main(String[] args){
// use 'new' to create an instance of class Person
Person p = new Person("aaa",18,"male",50.5f); // p refers to an object
/* you can try these two different construction methods, and see what the output is.
* Person p = new Person();
* Person p = new Person("aaa",18);
*/
// use object to invoke method personInfo()
p.personInfo();
}
}
Output:
Run the objectTest.java
file using the following commands:
javac /home/labex/project/objectTest.java
java objectTest
See the output:
Name: aaa
Age: 18
Gender: male
Weight: 50.5
There are four attributes in the Person
class. All the attributes are private, so they can't be accessed from outside (can only be used inside the class). There is one object method named personInfo
used to print personal information and there are three constructors with the same name as the class name Person
. The only difference among them is their parameters. This behavior is called overloading.
This part is very important. Classes are abstraction of entities with common features and objects are entities with specific features. You should take more time to understand the programming style mentioned above.