Object Oriented Programming in Java A Simple Explanation with Examples

Problems in Structured Programming   


  • No Restriction on global data
  • No clear relationship between global data and functions
Principles of Object Oriented Programming  
1. Encapsulation          2. Inheritance                3. Polymorphism   

Class & Object

  • Class is logical structure.    but                        Object is a physical entity.
  • A class defines a new data type.  as well as             A class is the template of its objects.
  • A class encapsulates the members of the object. and             A class can have two types of members.
  • Data Members                                                      Member functions
  • Global variables declared inside the class are called data members.
  • The functions defined in side the class are called member functions.
  • All the members of a class are accessible to the object of the class.
  • An object can access any of it members through dot(.) operator.    
  • Data members are also called instance variable.
  • When ever an object of the class is created a separate copy of the data members is created.
  • Each object has a unique copy of data members.
  • Member functions are created once in memory.
Example 
 

  • Multiple Objects of a Class.
  • Accessing object through Multiple Reference Variables.
  • Constructor Function
  • Constructor Overloading.
Multiple Objects of a Class.
  • Multiple objects can be created for a class.
  • Each object will be completely distinct from other objects.
  • A separate copy of the data members is created for each object.
  • An object cannot access the data members of another object.






Accessing object through Multiple  

  • Reference variables are the variable of class data type.
  • A reference variable of a class can hold the reference to the object of that class.
  • We can access an object through multiple reference variables.




Constructor Function

  • Constructor function is a  member function of a class having:
  • Same name as class name
  • No return type
  • Is called automatically at the time of  creation of an object.
  • Cannot be called like other member functions
  • Is called only once in the life time of an object i.e. at the time of creation.
  • Is normally used to initialize the data members of an object.




Constructor Overloading.  

Two or more than two constructor functions having different number of arguments or same number of arguments with different data types is called constructor overloading









Instance Variable Hiding 

  • The data members and local variable of a class may have the same name.
  • This phenomena creates a conflict for accessing the exact data variable.
  • The local variable of the function gets precedence.
  • It can be said the local variable of a function hides the data members of the class 
  • This key word is used to resolve conflict


Garbage Collection  

A mechanism that periodically checks un referenced objects in memory.
It de allocates the memory allocated to objects.


Post a Comment

0 Comments