OOPS AND JAVA

  • PIE : 

For any language to be Object Oriented , it must satisfy this PIE rule .
In this P stands for POLYMORPHISM , I stands for INHERITANCE and E stands for ENCAPSULATION .

  • Functions in C++ re called METHODS in JAVA .

WHAT IS A CLASS ?

Class is a user-defined data type that contains data members and methods . Data members are the various  type of variables or constants used in a class and methods are same as member functions in C++ . 

WHAT IS AN OBJECT ?
Object is an instance of class .It represents state and behaviour of class . State is represented by data members and behaviour is represented by member functions or methods .

When object of a class is made , member functions and data members of that class are allocated memory  . With no object no memory is allocated to the class . 

A program of class without an object can be compiled but it will not run until memory is allocated i.e. object is created . 

Consider the following program :

                                                  class Add
                    {
                        void print()
                        {
                            System.out.println("JAVA");
                        }
                    }

This program will be compiled but will nor RUN .


Syntax for OBJECT :

                 Add obj = new Add(); 

Add  ---- > It is the name of class . Whenever we create an object , instead of Add name of class should be used . 

obj ---- > It is the reference variable that refers to the memory location which is allotted to this particular class . Note that it is not called the name of object .


After the object is created the above Add program will appear something like this :

                                      class Add
               {
                    void print()
                    {
                          System.out.println("JAVA");
                    }
                public static void main(String args[])
                {
                  Add obj = new Add();
                  obj.print();
                }
               }


To call a method , dot operator is used (.) . Function called by object first will be executed first .

Methods in a program are used to reduce the length of code . If we need the same coding two times in a program then instead of typing it two times , we Create a method of that code segment and call it whenever needed in the program . 

Methods in JAVA can be defined  in Four ways :

  • Without Arguments , without Return type.
  • Without  Arguments , with Return type.
  • With Arguments , without Return type.
  • With Arguments , with Return type .





Note that here is no return type of method  (Void is no return type) .











CONSTRUCTORS AND DESTRUCTOR S :

CONCEPT OF CONSTRUCTORS :


Same as Method but as following properties :
  • Name of constructor must be same as that of Class name . That is class n constructor name must be same .
  • Constructor does not have any return type .
  • Constructor is by default called when object of a class is created .
  • For understanding of how to create a constructor consider the following illustration :
                            class Add 
           {
              int a,b;
              Add(int a, int b)
              {
                      this.a = a;
                      this.b = b ;
              }
              public static void main(String args[])
              {
                Add obj = new Add(10,20);
                System.out.println(a+b);
               }
             }

In the above program Constructor Add is created and note that when object is created constructor will be automatically called , we do not need to call a constructor and hence arguments should be passed when the object is created .

TYPES OF CONSTRUCTORS : 

  1. DEFAULT CONSTRUCTORS 
  2.  PARAMETRIZED CONSTRUCTORS 


PARAMETRIZED CONSTRUCTORS are those that are defined and declared by user . In simple words we can say that user defined constructors are called parametrized constructors .

Constructors are by default encoded in JAVA . Even if the constructor is not declared by the user , this by default encoded constructor is called .



CONCEPT OF DESTRUCTOR S :


In JAVA , destructor s are not possible by java has garbage collector to deallocate the memories . If garbage collector finds no REFERENCE VARIABLE of any object , ikt frees the memory of that object .


ACCESS Specifiers in JAVA :

We have four types of access specifiers in JAVA .
public , protected , default and private .

If we declare the class as public , methods of that  can be accessed by any class of ant package , inherited or not does not matter . If we declare the class as protected , only classes of same package and the classes inherited from that class can access  the methods of a protected class . If we declare the class as default , only the classes belonging to same package will be able to access the methods of that class . Derived classes belonging to some other package cannot access the methods of a default class . If we do not use any access specifier the case is considered default by the system . If the class is declared as protected , no other class will be able to access the methods of such a class .The derived classes are also not lowed to access the data members as well as methods of a protected class  The classes belonging to same package also wont be able to access the methods of a private class .
For more detailed explanation Click here



Concept of Object classes :

Object class is the class of all classes . It is a by default class .If we have no idea that we need the object of exactly which class , then we can declare the object as the object of base class . It can be accessed by the class whose object was needed . This Type of class and its object is uses while making big projects .
We do not need to create the base class or declare or define this class . It is default and inbuilt class in JAVA . We are to declare the object of this class in our program in the following way :
                
                                          object o = new A();  

It is the fixed or in other words we can say the default syntax for declaring the object of a object class.

SUPER KEYWORD: 

Keyword super always represents previous class. If we use super keyword in constructor , it must be a first statement in a constructor .
Number of arguments and types of arguments in super must be same as number of arguments and type of arguments in the previous class .


Consider the following program :

                     class Dim
                     {
                               int l,b;
                               Dim(int l,int b)
                              {
                                    this.l=l;
                                     this.b=b;
                              }
                               
                              void dis()
                               {
                                           System.out.println("L"+l);
                                            System.out.println("B"+b);
                                }
                      }
                     
                     class Area extends Dim
                     {
                               Area(int l,int b)
                              {
                                        super(l,b);
                              }
                                void disc()
                               {
                                        super.dis();
                                        System.out.println("Area "+(l*b));
                               }
                       }
                    
                      class Main
                      {
                                    public static void main(String args[])
                                    Area obj = new Area(10,20);
                                     obj,dis();

                       }


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes