Thursday, June 23, 2011

OOps concepts


What is an object ?


An object is anything that has a state and behavior.
Take a minute to just observe the observe the real-world objects that are in your immediate area 


Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).


An object stores its states in variables and its behaviors are exposed in functions. 


Method work on object's internal state and serve as primary mechanism for object to object communication.


What is a class ?


A class is a blueprint from which objects are created.


What is inheritance ?


Inheritance provides a way to extend existing classes to make a new classes with specialized behavior. Inheritance creates a hierarchy of classes and helps you reuse the code of a parent class in a child class.
A  Class inherits the attributes and behavior of its parent class or superclass. The attributes and behavior of the superclass need to be public or protected. Members of the superclass can be inherited with some restrictions.


There are two types of inheritance 
Single inheritance & Multiple inheritance 
Single inheritance : A subclass inherits from a single class.
Multiple inheritance : A class inherits from more than one immediate superclass.


HINT: Java classes cannot extend multiple classes. In Java, Only interfaces are allowed to extend multiple interfaces. Also, classes can implement multiple interfaces.


Although a subclass inherits all the members of its superclass, it cannot access the private member variables of the superclass. The public and protected member variables of the superclasses are accessible to a subclass. If the subclass is in the same package as the superclass, default members are also accessible.










No comments:

Post a Comment