In the real world, we humans like to classify everything from living things to inanimate objects, grouping things by the properties they share. For instance, in the world of living things we classify everything by kingdom, phylum, class, infraclass, order, suborder, family, subfamily, tribe, subtribe, genus and species. Thus the homo sapiens species belongs to the kingdom of animals, the phylum of chordata, the class of mammals, the order of primates, the family of hominidae, the tribe of hominini and the genus homo. Each classification is a more specialised type than the one that precedes it. Thus higher classes are more generic and lower classes more specific. The bat, which is also in the class of mammals, shares the same phylum and kingdom as we do, but differs in its order, family, tribe and genus. Thus we are related to bats in as much as we are both a type of mammal.
This type of classification is known as a taxonomy, and can be represented as a hierarchy, a family tree structure, with the most generic class at the top, or root, and the more specialised types splitting off on each subsequent level.
Just as we classify objects in the real world, we classify objects in the virtual world using a virtual taxonomy. The virtual taxonomy need not accurately reflect any real world taxonomy of the objects we are representing. If we wished to model a dog kennel, then we can limit the taxonomy to the genus: canis. If we also wished to identify the individual species and breed of dog then we might include those more specialised classifications, or we may limit our classification to breed. The point is we need only include those aspects that are actually of relevance to our program. For instance, we don't really need to know that a dog is actually a class of mammal unless we wished to segregate mammals from other classes, such as reptiles, but group them as animals.
By organising objects in this manner, we can greatly reduce the amount of duplicate code we need to write simply by encapsulating the generic functionality within the upper base classes, and placing the more specific functionality within the lower derived classes. For instance, most animals make some sort of noise, thus every animal object can "speak". When we call that method upon a dog, we would expect it to bark, while a cat should meow. These are simply specialised methods of a common, generic method. Animals that don't make a noise can simply default to the generic animal method, which simply produces no sound. Thus we need only cater for those that do make a sound.
Chat with our AI personalities
Take a look around you! The world is just full of objects. The sun, trees, birds, flowers, are all objects created in God's divine plan. Everyday you should give thanks to the Maker who declared and instantiated you and I and all living and nonliving things.
Import only one of the packages containing the classes with the same name. Use the other class by typing out its full namespace.
Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope.
People's Education Society. A world renowned Degree College in Bangalore, India.
The import statement in Java allows to refer to classes which are declared in other packages to be accessed without referring to the full package name. You do not need any import statement if you are willing to always refer to java.util.List by its full name, and so on for all other classes. But if you want to refer to it as List, you need to import it, so that the compiler knows which List you are referring to. Classes from the java.lang package are automatically imported, so you do not need to explicitly do this, to refer to String, for example.