My neighbor is taking \"Intro to Java\", and asked me to help explain a few of the first-day concepts. I realized that since I do this everyday, I don\'t have the beginner\
Class teaches Objects how to behave. A class is a blueprint for an object. It tells the virtual machine how to make an object of that particular type. Each object made from that class can have it’s own values for the instance variables of that class. Example: You might use the Button class to make dozens of different buttons, and each button might have it’s own color, size , shape , functionality.
OOP is just one more way of representing Abstract Data Structures in programs. In object-oriented terminology, the type is called a class, and the variable with that type is called an object. More on type <-> class, variable <-> object correspondence.
class == cookie cutter, object == cookie.
Object Oriented programming is about creating programs using as building blocks, "things" that exists in the real world, these real world things are called objects, hence object oriented
For instance, if you're creating a Address Book program, you may define the following objects:
person, address, phone
Among many, many others. Those would be real life objects, and you describe your program in terms of these abstractions.
With that in mind you can start describing some concepts.
Class is used to define the characteristics an objects will have. A class is used only as a template, or a blueprint. For instance, for all the persons in your address book, you may say they all will have:
Person:
- name
- last name
- phone number
- address
Etc.
An address may have:
Address:
- street
- number
- city
- zip code
- country
And so on. As you can notice, a class me be defined in terms of other classes, for instance, in this context, a person has one address.
An Object is a particular instance of a given class. When you add an entry to your address book, you create an object and fill in the attributes.
onePerson ofType Person is (
- name = "Oscar"
- last name = "Reyes"
- phone number = "56 58 11 11"
- address = anAddress ofType Address (
- street = "Tecolotes"
- number = 32
- city = "D.F."
- zip code = 23423
- country = "Mexico"
)
)
So, this object is a class instantiated with data. Other entry in the address book are other objects with different data.
That shows the difference between them.
There are other relevant concepts in OOP that are worth listing, and interrelate with the concept of object and class:
Abstraction You don't need to list all the attributes of a person, to use it. for instance, in this case, you don't care if that person is single or married, even when in real life, persons are either single or married.
Encapsulation Attributes from the person are hidden to other objects and are accessed through methods, this prevent from data corruption.
Polymorphism A different type may respond differently to the same message or method.
Inheritance classes may have subclasses and attributes and behavior which inherit the characteristics of the super classes.
Object is an instance of a class Variable is an instance of a type
That given,a class can be something like "type on steroids":
it can have :
variables which can be from any type or objects from another class
methods,which
can operate on class variables in the same way as different types have their methods(for example +(bool,bool)
)
can have access to the class variables
and it's all defined by yourself!
You can use the classes to model a problem in the optimal way. But there are sometimes other ways to do it ;)(not only OOP)
An object is a thing. A class is a category of things.
"Person" is a class; you are an object, an instance of the Person class. Also, the word "you" can be thought of as a variable, since it refers to a Person, but not always the same Person.