Chapter-3: Object Oriented Programming Concepts
Topics:
1.        Class
2.        Objects
3.        Abstraction
4.        Polymorphism
5.        Inheritance
6.        Encapsulation
7.        Constructor
8.        Access Modifier
9.        Final Keyword
10.       This Keyword
11.       Static Keyword
1) class
   1.1. Defining class
    Class is collection of objects,methods,constructor etc.
    A class is a user defined prototype in which objects are created.it define the
      set of functions that are common to all objects of one type.
Syntax:
<class modifier> class<class name>
<extends clause><implements clause>
{
//field declaration
//method declaration
}
Example:
public class MyClass {
int x = 5;
}
      1.2. Method
         It is a block of code which only runs when program method is called.
         Into a method, You can pass data, which is known as parameters,.
      Methods are used to perform certain actions, and they are also known as
       functions.
      Why use methods? To reuse code: define the program code once, to use it
       many times.
Example :
public class MyClass {
static void myFunction() {
// code to be executed
}
}
      myFunction() is the name of the method.
      static means that the method belongs to the MyClass class and you not need
       to create object of that class.
      void means that myFunction() method does not have a return value.
   1.3. Call a method
   To call a method in Java, write the method's name which is followed by two
      parentheses () and a semicolon;
   In the following example, myMethod() is used to print a text (the action), when
      it is called.
Example :
public class MyClass {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}
Output :
I just got executed!
2) Object
   Object is an instance of class.
   Object has three characteristics:
         State: represent data (value) of an object.
         Behaviour: represents behavior (functionality) of object.
             Identity: object identity is typically implemented via unique ID. The
             value of ID is not visible to external use. But, it is used internally by JVM
             to identify each object uniquely.
   For example;
   Pen is an object.Reynolds is name of the pen; its color is white etc.known as its
   state. It is used for writing, so its behavior is writing.
   Creating object in two ways;
   Partially created object:
                 Classname objectname;
   Fully created object(new keyword):
                 Classname object_name = new Classname ( );
   Using the new keyword you can call constructor.
3) Abstraction
   In data abstraction, it will show only function and it will hide the implementation
   part.
   For example, in whatsapp or any chat application you are sending a message but
   you don’t know the internal process how message is send.
4) Polymorphism
   In which, process that define multiple methods that have same name in same
   class, but all method perform different task. it will be used when there is one or
   more same name method used in the class. Polymorphism means more than one
   form.
5) Inheritance
   Inheritance means to take something which is already made.
   Using Inheritance the idea of reusability can be achieved.
   Reusability means reuse something that are already available rather than
     creating new of some thing so it will save time.
   Inheritance is the mechanism of deriving a new class from an old class is child
     class.
     A base/ parent/ super
      B derive/ child/ subclass
     Extends keyword used for inheritance
      Syntax:
     Class A
     {
     }
     Class B extends A
     {
     }
6) Encapsulation
   Encapsulation in Java means mechanism of wrapping the data and function with a
   single unit.
7) Constructor
    When you create a new instance (object) of a class using new keyword,
     Constructor for there class is used.
    Constructor is same as methods with some differences.
    If you don't define a constructor for a class, the default ( parameter less)
     constructor is automatically created by JVM.
    constructor is special Member function which follow following condition
     • Constructor name is is same as class name.
     • Constructor has no return type.
     • Constructor has no return statement.
    Three types of constructor
     i) Default constructor
     ii) Parameterized constructor
     iii) Copy constructor
   7.1. Default Constructor
          A constructor without parameter is called as default constructor.
      Example;
      class demo
      {
             demo()
             {
                     System.out.println(“ default constructor”);
             }
      Public static void main(String ar[])
      {
             demo d1= new demo();
      }
   }
   Output:
        default constructor
7.2. Parameterized Constructor
          A constructor that have one or more than one parameter is call
           parameterized constructor.
   Example;
   Class demo
   {
          demo(int a,int b)
          {
                  Int c=a+b;
                  System.out.println(c);
          }
   public static void main(String ar[])
   {
          demo d1= new demo(10,20);
   }
   }
   Output:
        30
7.3. Copy Constructor
      It is initialized an object using another instance of the same class.
   Example;
   Class student
   {
         int id;
         String name;
         student (int i, string n)
         {
                 id=i ;
                 name= n;
         }
         student (student s)
         {
                 id=s.i ;
                 name= s.name;
             }
             void display()
             {
                      System.out.println(id+” “ +name); }
      public static void main(String ar[])
      {
             student S1= new student(61,”ABC”);
             student S2= new student(S1);
             S1.display();
             S2.display();
      }
      }
      7.4 Constructor Overloading
        It means more than one constructor have different argument in same
           program.
      Example;
           Class demo
           {
           demo()
           {
                 System.out.println(“ default constructor”);
           }
             demo(int a,int b)
             {
                    Int c=a+b;
                    System.out.println(c);
             }
             public static void main(String ar[])
             {
                    demo d1=new demo();
                    demo d2=new demo(10,20);
             }
8) Access modifier
   Type of modifier in Java
        Public: public members are available anywhere outside the class weather
           it is in same package or different package
        Default: default members available outside class work inside package.
      Note: if user does not use access specifier then compiler uses default
          Protected: protected members available outside the class but only in
           subclass.
          Private: not available outside class.
          Accessing rule table given as;
                                        Private   Default      Protected   Public
    Same class                          Y         Y            Y           Y
    Same package different class        N         Y            N           Y
    Same package sub class              N         Y            Y           Y
    Different package sub class         N         N            Y           Y
    Different package different class   N         N            N           Y
9) Final keyword
    Final keyword used with
   1) Variable
   2) Method
   3) Class
   9.1 final variable:
    When you declare variable with final keyword then it become final variable.
    When you create final variable you have to initialize that variable.
    Variable declared with final is worked as a constant.
      final int a=5
   9.2 final method:
    When you declare method with Final keyword then it becomes final method.
    When you declare method as final you can't override that method.
       final void hello()
       {
               // can’t override
       }
   9.3 final class:
    when you declare class which final keyboard it become final.
    final class can't inherit.
       final class classname
       {
               // can't inherit
       }
10)this keyword
     Sometimes a method will need to refer to the object that invoked it. To allow
      this Java defines ”this” keyword. It can use inside a method to refer to current
      object.
     when local variable and instance variable have same, name local variable hides
      instance variable, to retrieve value of instance variable, ”this” Keyword is used.
  Example;
  Class demo
  {
     Int a=10; ← instance var
     void test () {
     int a=20; ← local var.
     System.out.println(a);
     System.out.println(this .a);
  }
  public static void main(String[] ar)
  {
     demo d1= new demo();
     d1.test();
  }
  }
11)Static keyword
  Static keyword can be used with
  1) Variable
  2) Method
  3) Block
  9.1 Static variable:
     When a static keyword is used with a variable it is called static variable.
     It is accessed before any object of its class created.you can directly access
      static var.using class name.
            Classname. Varname;
         Static variable creates only one copy for all objects that means,all objects
            of class share same static variable.
9.2 Static method:
     When a static keyword is used with a method then it is called static method.
     Following points must be considered for declaring a static method.
              Static methods access only static members of the class. it can't
                access non-static member
              static method can access directly using class name
                        class name. method name();
                 static method can't use this and super keyword
9.3 Static block:
     Static block executed before creation of any object of class.
     Execute only one time when class is first loaded.
     If there are more than one static block then and it will execute in
sequence in which it is created.