Friday, 15 March 2019

Class called Cat

Class is a blueprint for an object. What that means is that it simply is a template of properties and behaviour.

For example we saw Tom object in previous example. Tom is name of a Cat, there can be many more Cat's like Tom. Say we have Whiskers and FunnyTail as another two Cats. Here each object has different names but all of them are of same class - Cat

Now when we talk of a class Cat, we can think of its state or properties - name, color, nap time, favourite food, date of birth, maximum running speed etc etc . Also Cat can do some actions as behaviour - play, meow, sleep, show anger, show cute eyes, run after something, etc

So we see that the nature and ability of different cats is different and this can be represented by the different state of cat objects.

________________________________________
Cat
________________________________________

properties :

name
color
favourite food
nap time
max running speed
________________________________________

behaviour :

meow
eat
run after something
show anger
________________________________________


Now say Whiskers ran twice as fast as Tom, then when Tom is running after Whiskers, he can never catch up
...
Cat tom = new Cat(*magic*);
Cat whiskers = new Cat(*more magic*);
...
...
tom.runAfter(whiskers);





No comments:

Post a Comment