Object Oriented Programming Part-1

Java is an object oriented programming language. 

Object Oriented Programming Language: A language that supports all principles of an object oriented programming is known as an object oriented programming language.
Object Oriented Principles:
1) Encapsulation
2) Abstraction
3) Inheritance
4) Polymorphism

To use the above principles in a Java programming, we need the following language constructs:

1) Class
2) Object

Class: A class is a collection of variables and methods.

Object: An object is an instance of a class.

Variables:

A variable is a container that contains data.
There are three types of variables in Java:
1) Instance Variables
2) Class Variables
3) Local Variables

1) Instance Variables:

A variable that defined as a member of a class is called as an instance variable. Memory allocated to instance variables whenever an object is created. Instance variables are stored in heap area. There are two ways to access an instance variable. 1) By using object 2) By using object reference

2) Class Variables:

A variable that is defined as a static member of a class is called as class variable. Memory allocated to class variables whenever class is loaded. Class variables are stored in method area. There are four ways to access class variable. 1) Directly 2) By using class name 3) By using object 4) By using object reference

3) Local Variables:

A variable that is defined inside a method is called as local variable. Memory allocated to local variables whenever method is called. Local variables are stored in stack area. There is a only one way to access local variable. i.e. directly.

Note1: Local variables cannot be static in Java.

Note2: No global variables in Java (Outside the class)

Separate copy of instance variable exist for every object. 
Only one copy of class variable exist for all objects.

Use instance variable if the value is different for object.

Use class variable if the value is same for all objects.
Use local variable to perform the task.

Assignment4:



Arrays:

An array is a collection of similar data elements.
Array index always begins with zero and ends with size-1.
In Java, array itself an object and array reference also called as an object reference.
Array contains elements where as array reference contains hash code.

Methods:

A group of statements into a single logical unit is called as method. 
Methods are used to perform the task. Task code must be written in method only.
Advantages of method:
1) Modularity 
2) Reusability
In Java, methods are divided into four categories:






1) Methods with arguments and with return value:
Example:
class Demo
{
     int max(int a, int b)
     {
          if(a>b)
             return a;
          else
             return b;
     }
     public static void main(String args[])
     {
          int x=new Demo().max(10, 20);
          System.out.println(x);
          Demo d=new Demo();
          int y=d.max(23, 45);
          System.out.println(y);
     }
}
2) Methods with arguments and without return value:
Example:
class Demo
{
     void add(int a, int b)
     {
          System.out.println(a+b);
     }
     public static void main(String args[])
     {
           new Demo().add(43, 32);
           Demo d=new Demo();
           d.add(33, 22);
     }
}
3) Methods without arguments and with return value:
Example:
class Demo
{
     int get()
     {
          int a=5;
          return a;
     }
     public static void main(String args[])
     {
            int x=new Demo().get();
            System.out.println(x);
            Demo d=new Demo();
            int y=d.get();
            System.out.println(y);
     }
}
4) Methods without arguments and without return value:
Example:
class Demo
{
     void display()
     {
           System.out.println("Welcome");
     }
     public static void main(String args[])
     {
            new Demo().display();
            Demo d=new Demo();
            d.display();
     }
}

Assignment5:





Method Overloading:

If two or more methods with the same name and with different parameters list, then it is said to be method overloading.
Parameters list can be different in no. of arguments, data types or order of an arguments.
In method overloading, return type can be same or different.

Method Overriding:

If two or more methods with the same name and with the same parameters list, then it is said to be method overriding.
Note: Methods cannot be overridden in the same class because of ambiguity to call.

"static" keyword:

It is called as modifier because it modifies the behaviour of variable, method, class & block.
By using static keyword, we can create class variables, class methods, nested top level classes & static initialization blocks.

"this" keyword:

It is called as an object reference or reference variable because it refers an object. 
It always refers current object
It is implicitly present in instance method, constructor & initialization block.
It is explicitly required to access an instance variable whenever both instance variable and local variable names are same.
Note: static method does not refer this keyword in anyway.







Constructors:

A constructor is a special method which has same name as the class name and which has no return type.
Constructor is called automatically whenever an object is created.
Constructors are used to initialize instance variables.
Constructors are two types:
1) Default Constructor(without arguments)
2) Parameterized Constructor(with arguments)
If the class does not contain any other constructor then only java compiler implicitly provides one default constructor.

Assignment6: