The concept of classes and objects in object-oriented programming

In the curriculum of object-oriented programming, I see people write these concepts a very general way and confusing, his initial study very hard and always. So in this article I will highlight some of the concepts of classes and objects, and most simply contact made possible Tet.

The concept of classes and objects

Maybe you still remember the biology we learned about the classes as interesting layers, reptile, bird class,… In each class, including properties, specific characteristics such as the vertebrate mammals, have 4 lead, birds have 2 wings and 2 leg, classes often reptile reptilian underground.

object-oriented programming

So how is called a class. You understand that the class is an overarching concept to say the most common characteristics of the elements, the members of that class.
What is the object? Object is an element, a specific member of the class. For example, in mammals, the monkey A, monkey B is the object as it actually exists in reality. Each object people also called an instance of the class as the object through which we know is the most common characteristics of the class. For example, when looking at the monkey A, it belongs to the class interesting and so we know that these mammals vertebrate, have 4 lead,…

Did you notice, in each layer, the objects in that class with properties and certain actions. Then the properties we call attributes (Attributes 4 lead), the actions we call the method (local food, Sleeping,…).

For example a little closer Student class (student, student) shall include name (name) and scores (scores), Attributes such as fish, every student to eat (Oh) and study (school) it is a method.

object-oriented programming

From concept to programming

When did remember and distinguish between the concept of layers, Subjects, attributes and methods, how can we apply it to programmer? You can use the language object-oriented programming such as C # various, Java hay php, Java used herein to describe his, other languages ​​are similar, I did not mention the use of mild language.

For example, we have the following code to declare a class Student. Each Student has 2 is the name attributes and scores, neck 2 is eat and study methods.

package cachhoc.net;

public class Student {
	String name;
	double scores;

	void eat() {
		System.out.println(name + " eat");
	}

	void study() {
		System.out.println(name + " study");
	}

	public static void main(String[] args) {
		Student student1 = new Student();
		Student student2 = new Student();
		student1.name = "Jone";
		student2.name = "Ana";

		student1.study();
		student2.study();
	}
}

Have you noticed in the main method, we can not directly call methods (eat, study) or properties to perform processing that we need to adopt a certain object to perform (here is 2 object student1 and student2). Like the fact that we can not directly call the class students go to eat, grade students go to eat, Students attend school classes where students have to say is going to eat a, She went to school students,…

To declare an object we use the operator new as above.