Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
What exactly is the purpose of a class?
I've heard all of the "If you're making a zoo simulation, Zoo would be the class and Monkey would be an object of the class". That doesn't explain how you would practically use a class. How many people actually make zoo simulations? I would like a practical example of a class.
8 Answers
- Jason PLv 51 decade agoFavorite Answer
A zoo technically could be a class as well. Classes properties can be instances of other classes. In purely object oriented languages everything is a class and everything derives from an object base class.
So taking your zoo. There are some things particular to zoos
Zoos have entrances, exits. Those would be properties of the zoo class itself. Zoos also have pens, animals, trainers etc. Each of those are object unto themselves that could also be properties of an instance of a zoo class.
San Diego Zoo is not Cincinnati Zoo
SanDiegoZoo.ReptileHouse.Pen1.AlligatorsCollection.Count() would be valid as would CincinnatiZoo.ReptileHouse.Pen1.CrocidileCollection.Count()
Hope that is clearer than mud.
- 1 decade ago
A class is defined to modularize functionality in a program. A class can be anything. Imagine that you have a house. There is a kitchen and a bedroom and bathroom. A house is modularized so that you can keep a good separation of concerns. You wouldn't want to use the shower in the kitchen (unless you're kramer) and you wouldn't want an oven in your bedroom. It keeps the parts of the program that are independent separated so that if someday you want to add a nursery for the new baby, you don't have to rearrange the entire house to accommodate the new addition. Through the doors in your house is where the data gets sent like messages around your program. It's a modeling technique, that's all. When you are first learning programming it's important to keep an open mind. You want to learn new things, right?
A practical example of the zoo might be a program that monitors their cages. It would be receiving messages from the nodes in the cages that monitor the doors, camera video, and temperature. You want to be able to monitor these inputs and make alarms if they go beyond a certain range. Using object-oriented design, we might make an initial class for the zoo. This class would start (instantiate) a class called the cageManager, this class would have the unique ability to reach out to the monitors and get id numbers and start up information. Then in the process it would start a cage class (an instance) for every cage. The greatest advantage here is that you now have a class called cage that has the specific ability to receive input streams from these cages and can transmit that information to other places in your code--and you only have to write it once.
Now, if you later decide to add audio to your cage class but only for some of them, you'll derive a subclass of cage. Let's call it cageWithAudio. Now your cage with audio class has all the abilities and functionality of the cage class, but you can add on the audio class as well. So, what's happened here is that all your cages without audio still work and your new cages work by only writing the audio stuff and you don't have to change cage or cageManager. Pretty neat, huh?
- 1 decade ago
1. It makes construction of multiple objects so much easier.
2. It allows for a lot of neat stuff like inheritance and polymorphism.
3. If everything is contained in classes (as in Java) and the classes are all in separate files, only one class has to be loaded at a time, which makes the program faster.
4. You can protect data by hiding it in a class and making it private.
What you heard about classes is not correct. The relationship between Monkey and Zoo is HAS-A, not IS-A. Monkey is a field or member of Zoo, not an instance of it. An object in the Zoo class would be Bronx Zoo, and Monkey would be in the class Primate or Animal. This shows the main advantage of object oriented programming. It's just so much more intuitive.
- PaulLv 71 decade ago
The example is wrong - Animal would typically be the base class and then Monkey would be a derived class from Animal. (A zoo would be a collection of Animal objects). Once you understand this example you can move in to more practical stuff, but if you don't understand this yet then you need to spend more time on it.
- How do you think about the answers? You can sign in to vote the answer.
- grezlbeLv 51 decade ago
The class in a Java or C++ environment is where the code is placed. In Java, the class holds variables, methods, and objects that can be referenced to other classes or interfaces. Java's classes mostly hold methods and variables which develop a syntax for execution which occurs in the method main. Sometimes, the entire syntax for a program is put within the method main.
- Jim MarylandLv 71 decade ago
This is actually one of the times where a car analogy works pretty well. A car is a generic term. You can't make a car but you can make specific instances of cars, for example, you can make a Ford Mustang. People know it is a car but specifically it is a Ford Mustang.
In this case, "car" is the class and Ford Mustang is an instance of "car". The "car" class would be defined something like this:
public class Car()
{
// then create methods (C++, Java) or properties (C#)
public void setMake(string make) {...}
public string getMake() {...}
public void setModel(string model) {...}
public string getModel() {...}
}
So the above would define characteristics of a Car, but it could be any car until you create an instance of it.
Example:
public void DoSomething()
{
// Create an instance of the Car class
Car myCar = new Car();
// Set properties of myCar
myCar.setMake("Ford");
myCar.setModel("Mustang");
// I can also make another car
Car myCarToo = new Car();
myCar.setMake("Nissan");
myCar.setModel("350Z");
}
The class is more like a model/blueprint until you define an instance of it.
** Edit **
In your question you have the class and instance relationship mixed up. As Paul said, a class called "Animal" would be more appropriate for defining animals in the zoo.
Sample Animal class:
public class Animal()
{
public void setName(string name) {...}
public void setSpecies(string species) {...}
public void setIsPoisonous(boolean poisonous) {...}
// Likely you'd make a "Diet" class, rather than a string, but for keeping this simple, only go with an animal eating one type of food.
public void FoodEaten(string food) {...}
}
Then you would use it like:
Animal monkey = new Animal();
monkey.setName("Curious George");
monkey.setSpecies("Gorilla");
monkey.setIsPoisonous(false);
monkey.setFoodEaten("banana")
- Anonymous1 decade ago
public class Date {
private int day = 0;
private int month = 0;
private int year = 0;
protected getDay {return day;}
protected getMonth () {return month;}
protected getYear () {return year;}
protected getDay () (int setDay) { day = setDay;}
protected setMonth( int setMo) { month = setMo;}
protected setYear( int setYr;) { year = setYr; }
protected getDate () {return month + "/" + day + "/" + year;}
}
Date today = new Date();
write( today.getDate() ); <<--- returns "0/0/0000"
today.setDay (2);
today.setMonth(7);
today.setYear(2009);
write (today.getDate()); <<-- returns "7/2/2009"
^^ Pick your poison for the programming language... this is just the idea the syntax isn't going to compile... a monkey does is not a very good example of a class "Zoo", whoever gave u that one is dumb.... an object shares methods and properties with other objects of that class, but not the same values.... monkey1 and monkey2 have the same class of Monkey, but monkey1's hair is black and monkey2's is white... they both have hair, just different colors... make sense?
- ulisesLv 44 years ago
i think of its everywhere from 40,000-2 hundred,000. even though it stages reckoning on style of persons in significant other and childrens. A bachelor making 50,000 is seen reliable yet a relatives of four with an stronger half and childrens income of fifty,000 is seen much less financially reliable. So its style of contributors in significant other and childrens whilst in comparison with significant other and childrens income. you are able to variety of gauge it from that usually. yet i think of customarily words a center classification income is defined as adequate funds to offer each member of serious other and childrens with undemanding desires, a minimum of one mode of delivery and a few funds left over for entertainment purposes. Then there is the a number of ranges of middle classification" working middle classification at one end and better middle on the different" It gets kinda hazy there. you have at low end the hourly cost finished time interest with advantages, and at different the profits based finished time interest.