In: Computer Science
Classes and Objects are the central of Object Oriented Programming (O.O.P.) This is a style of programming that focuses on using objects to design and build many great applications.
Do the following:
Difference between class and instance of a class:
A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class. For instantiating a class, the 'new' operator instantiates a class by allocating memory for a new object and returning a referebce to that memory. And the new operator also invokes the object constructor. Instantiating a class is nothing but creating an object and when yo create an object , you are actually creating an instance of a class, which is therefore instantiating a class. The name of the constructor provides the name of the class to instantiate.
"has-a" relationship
The has-a relationship is the composition of calsses. When a class is formed as a collection of other classes,it is called aggregation between these classes. It is called as 'has-a' relationship. It simply means that an instance of one class has a reference to an instance of another class or an other instance of the same class =. For example a car has engine and dog barks etc,.
'this' keyword
'this' is a reference variable that refers to the current object. There are 6 usages of this keyword. They are as follows: this can be used to refer current class instance variable, can be used to invoke current class method (implicitly), this() can be used to invoke current class constructor, can be passed as an argument in the method call/ constuctor call, used to return the current class instance from the method. If there is an ambiguity between the instance variables and parameters of a class, this keyword resolves the problem of ambiguity in the program.