-3

Can I say that class is a manual for building up an object?

I agreed that object is one of the instance of class.But if some properties that defined by classes may not make sense for particular object, for example,I have an object called "Duck" which use the class "Bird" for build, but Duck cannot fly,so if I use class "Bird" for build up an object "Duck".In the other words, the manual cannot provided the guideline for build up the object.

So,can i say that class is a manual for building up an object?As well as the manual should provided all the functions that the users need,in case there shouldn 't have any defects inside.

  • Textbook answer: `A class is a *template* for an object`. This means that classes show how an object is supposed to look, not how it behaves. The behaviour is defined by the object, often in the `constructor`. – Susmit Agrawal Jul 27 '18 at 08:07
  • Classes are using to build the structure of your application. But there are some best practice for using them that you should learn. Most important principles, in my idea, is SOLID principles. In your case you Liskov principle can help you. – Siamak Ferdos Jul 27 '18 at 08:16
  • class is a blueprint for similar type of object i.e similar type of objects are related to one class – Deepak Verma Jul 27 '18 at 08:16
  • 1
    [Ducks cannot fly?](https://www.youtube.com/watch?v=G17JEhe4Icc) – Seelenvirtuose Jul 27 '18 at 08:26

1 Answers1

1

You are mixing two things at the same time:

On one hand you have to know that a class is just some kind of a template for an object instance, as it will show you what are the attributes and methods that any instance of that object can do. For example, you can think about the class "Car". Each car will have a different colour and a different plate number. That's a good sign for us to think that those two can be attributes of the class.

On the other hand, you have to know that sometimes classes are related between them and you have some tools to resolve your problems. That's when inheritance and interfaces come along in your example. As you can imagine, any duck is a bird, but not any birds are ducks. Therefore, you can establish a class inheritance between Bird (as a "father" class) and Duck (as a "child" class), because a lot of the behaviour between birds can be defined for all of them. In your specific example, you will define the "flying" methods in Bird subclasses.

Moreover, you can say that this example should have Bird as an abstract class, because Bird is a "concept" but it cannot exist by itself (you can also think in "polygon" and "triangle", polygon doesn't make sense by itself).

CarlosMorente
  • 775
  • 1
  • 7
  • 17