Encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions).
Encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions).
Data hiding is the ability of objects to shield variables from external access. These private variables can only be seen or modified by use of object accessor and mutator methods. This permits validity checking at run time. Access to other object variables can be allowed but with tight control on how it is done. Methods can also be completely hidden from external use. Those that are made visible externally can only be called by using the object's front door (ie. there is no 'goto' branching concept).
Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An example of where this could be useful is with an employee records system. You could create a generic employee class with states and actions that are common to all employees. Then more specific classes could be defined for salaried, commissioned and hourly employees. The generic class is known as the parent (or superclass or base class) and the specific classes as children (or subclasses or derived classes). The concept of inheritance greatly enhances the ability to reuse code as well as making design a much simpler and cleaner process.
Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An example of where this could be useful is with an employee records system. You could create a generic employee class with states and actions that are common to all employees. Then more specific classes could be defined for salaried, commissioned and hourly employees. The generic class is known as the parent (or superclass or base class) and the specific classes as children (or subclasses or derived classes). The concept of inheritance greatly enhances the ability to reuse code as well as making design a much simpler and cleaner process.
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloading and overriding are two types of polymorphism . Now we will look at the third type: dynamic method binding.
Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. For example 'spinning' a number may mean increase it, 'spinning' an image may mean rotate it by 90 degrees. By defining a method for handling each type of parameter you achieve the effect that you want.
Overridden methods are methods that are redefined within an inherited or subclass. They have the same signature and the subclass definition is used.
Parameters of a primitive type work differently than those of a class type
primitive type parameters are call-by-value, so the calling object's variable is protected within the called method (the called method cannot change it)
class type parameters pass the address of the calling object so it is unprotected (the called method can change it)
For similar reasons, the operators = and == do not behave the same for class types as they do for primitive types (they operate on the address of object and not its values)
Therefor you should usually define an equals method for classes you define (to allow the values of objects to be compared)