Here is an excerpt from a 1994 Rolling Stone interview where Apple co-founder Steve Jobs explains what object-oriented programming is
Objects are like people. They're living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we're doing right here.
Here's an example: If I'm your laundry object, you can give me your dirty clothes and send me a message that says, "Can you get my clothes laundered, please." I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, "Here are your clean clothes."
You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can't even hail a taxi. You can't pay for one, you don't have dollars in your pocket. Yet I knew how to do all of that. And you didn't have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That's what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.
Object-oriented programming provides a clear and modular structure for creating programs.
It is easy to maintain and modify existing code using the class structure.
1. Classes act as templates for the creation of objects, specifying their state and behavior
2. An object created by a class is called an instance of that class
3. A class is itself an object, and an instance of the class "Class" or a "blueprint" for creating objects
public class Person {
// Just an empty class,
// we don't create anything yet here
}
To create a class, use the keyword class
public class Person {
String name = "Bob";
public static void main(String[] args) {
// create object of person
Person person = new Person();
System.out.println(person.name);
}
}
To create an object of Person, specify the class name, followed by the object name, and use the keyword new
In the previous chapter, we used the term variable for name in the example (as shown below). It is actually an attribute of the class. Or you could say that class attributes are variables within a class:
public class Person {
String name = "Bob";
int = 20;
}
You can access attributes by creating an object of the class, and by using the dot syntax (.)
public class Person {
String name = "Bob";
public static void main(String[] args) {
// create object of person
Person person = new Person();
System.out.println(person.name);
}
}
You can access attributes by creating an object of the class, and by using the dot syntax (.)
public class Person {
String name = "Bob";
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
person.name = "Nur Ratna";
System.out.println(person.name);
}
}
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes
public class Person {
String name;
// Create a class constructor for the MyClass class
public Person() {
// Set the initial value for the class attribute name
name = "Bob";
}
public static void main(String[] args) {
// Set the initial value for the class attribute name
Person person = new Person();
System.out.println(person.name);
}
}
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes
public class Person {
String name;
public Person(firstName, lastName) {
name = firstName + " " + lastName;
}
public static void main(String[] args) {
Person person = new Person("Nur", "Ratna");
System.out.println(person.name);
}
}