What's the best way to define the words "class" and "object" to someone who hasn't used them?
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's mind, and it's hard to relate some of this stuff from scratch.
The one that's actually not trivial for me to explain is "what the heck is a class?"
Best I have so far:A variable holds some kind of data; one variable might be a first name, another variable might be your weight in pounds.
A method is a function, it does stuff, and can do stuff with those variables. A method might display your name on screen, or tell you how much weight you should lose to have a good BMI ratio.
An object holds both variables and methods; one object might represent you, a second object might represent me.
A class is kind of the blueprint or template that describes the methods and variables that will be in each object. An object is an instantiated (instance of a) class; an o开发者_如何学运维bject is something, while the class is simply the plans to make that something.
Continuing the example, we have a Person object, which is instantiated to hold Alice's data, and another Person object instantiated to hold Bob's data, and another for Carol, and so on.
How do I tune this example to make more sense, and/or what's a better approach? The word "instantiated" feels too heavy at this point.
(I think this is a useful question, but is obviously subjective; marked as community wiki.)
A class and some class instances:
(public domain image hosted by wikipedia)
Class : Object :: Blueprint : Building
"Car" is a class. My car, sitting in my driveway, is an instance (object).
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.
One of the examples I use during my java courses is the Human
class.
Everyone reading this is a Human
(I least I hope so !), we all have our differences our resemblances but at the end we're all Human
(After all).
Each Human
(known as an instance or object) has specific characteristics such as the eyes color or the voice which are the fields (you called that variables, but the right name would be fields). But the values are different from an Human
instance to another.
There is also a common knowledge, shared with the humanity, principles like the "Pythagorean theorem". This knowledge is common, it can be interpreted as a static field (I know it's an exaggeration) which means that this knowledge is not only contained in one human but in the humanity.
Every Human
can do things such as walking, speaking etc. this is known as method, walking is the same for everyone, but when I walk, not everyone walk. The act of walking only affects the Human
instance which does this, but still it's defined by the Human
class
If you want to get deeper in OOP, Teaching OOP to non-programmers
A class description is like a blueprint for a house. All the houses built from that blueprint are objects of that class. A given house is an instance. A tenant can be a changing variable in the house. An example of a method is the procedure by which the post office sends and receives messages (mail) to the house via its mailbox.
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.
class == cookie cutter, object == cookie.
class:: Man or Woman
object:: me, you ...
A class is a blueprint/template which you use to create objects. An object is an instance of a class.
If and only if he is familiar with Plato's Theory of Forms, you can make an analogy where classes are like Plato's forms and objects are like Plato's real world objects.
See this post for a full description.
Class: Girl
Object : that girl, this girl, my girl...umm maybe not.
Yea all girls should have the properties of a Girl (class in this case).
I always define them as blueprint and product.
A blueprint describes the complete product in every detail, the product is the result that comes out of the machine.
If your neighbor is into classical philosophy, classes are Plato's Forms and objects are the things we see everyday that are based on the Forms.
Class can be defined as the blue print or template which defines attributes,actions,states for the entity and Object would be the one which would be implementation for the entity.
A java class is the structure or blueprint of an object and object is the instance(memory creation) of a class.
Example: Car is an object(real world entity). firstly, make the structure or design of a Car it's class and when the design of car, to convert real world Car it's an Object. the Car(object) is working now base behalf of design of a car(class).
Panda DNA is a class. A Panda running around, eating and performing Panda-like activities is an object.
If they are learning to program OO have them use BlueJ. They should get the differences after walking through the first tutorial.
You define the classes and when you instantiate them they actually appear at the bottom of the GUI at which point you can call methods on them.
It really helps get the point through better than any analogy you want to try. Even if you nail the analogy, it doesn't translate into code for someone who hasn't learned OO yet (even though for all of us it seems really natural and all these descriptions make great sense.)
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.
You might find this talk by Guy L. Steele interesting: http://video.google.com/videoplay?docid=-8860158196198824415#
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)
class: custom variable type
object: a variable, whose type is custom defined (if you don't count the built-in ones)
We can also understand the concept of class and object as: as a class is a template so lets have following two examples: Example 1: a recipe of a cake is a template so its a class and cakes that are made following recipe are the objects. Example 2: A brick maker is a class and bricks are objects
Your question details have pretty good definitions of all the terms. Here's an analogy I found pretty useful - I've listed it in a kind of top-down approach:
class Employee. This is a kind of, as you say, 'blueprint' or 'template' that contains generic details about all kinds of employees in an organization - let's consider the director, a project manager, a developer, a contractor, or a member of the housekeeping staff. They're all employees : hence, they're all instances of the class, or objects.
All the objects have certain attributes in common - they're all allotted, say, an employee ID. They all draw a salary. They all have a designation. One could call these the member variables, as they exist for all objects, but their values are clearly different based on what object they're members of.
And finally, there are some functions that need to be performed for all employees - say onBoarding() or calculateSalary().
Class: Human being
Object : Man, Woman
Class : Fruit
Object : Apple, Banana, Mango ...
Class : Mobile Phone
Object : Apple , Samsung , LG ...
Class : Food
Object : Pizza, Rice ....
An Object is a group of related data and functionality. What the group of data and functionality will consist of is defined in the class. Class is the design or specification of the object.
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.
I can say with an example: Animal, Human, car etc. Here Animal, Human, car considered as Class, Now consider Dog: Here Dog considered as Object, who is under Animal class. If we consider a dog, then its state are - name, breed, color, and the behavior are - barking,Eating, running, Sleeping. Now we can say, A class is a blue print of Animal class from which individual object is created. Here barking(), running(), eating(),Sleeping() etc. are method of the particular Dog object. I think it will be little easier to understand the difference between Class and object.
I would highly recommend telling him/her to buy a copy of a book called The Object-Oriented Thought Process by Matt Weisfeld. It's a really good conceptual introduction to OO. I've lent out my copy to a few people who were just getting into OO and they really liked it.
精彩评论