Object Oriented Programming Part-2

"final" keyword:
It is called as modifier because it modifies the behaviour of variable, method & class.

By using final keyword we can prevent value of the variable, method overriding & inheritance.

Final values cannot be changed.
Final methods cannot be overridden.
Final classes cannot be inherited.

Access Modifiers:
Access specifiers are also called as access specifiers because they specify
access permissions to variables, methods, classes, interfaces, ... etc.,

There are four access modifiers in Java:
1) private 2) protected  3) public 4) No name -> default access modifier

Scope of private member is class.
Scope of protected member is package & sub class(sub class can be outside the package also)
Scope of default member is package.
Scope of public member is not restricted.

Relationships in Java:
1) "IS-A" Relationship
2) "HAS-A" Relationship

Overloading Vs Overriding

Overloading:

1)   If two or more methods with the same name and with different parameters list, then it is said to be method overloading.
2)   In method overloading, return type can be same or different.
3)   Methods can be overloaded in the same class
4)   Methods can be overloaded in inheritance also.
5)   In method overloading access modifiers can be same or different
6)   Final methods can be overloaded.
7)   Static methods can be overloaded.
8)   Private methods can be overloaded.

Overriding:

1)   If two or more methods with the same name and with the same parameters list, then it is said to be method overriding.
2)   In method overriding, return type must be same except covariant return type.
3)   Methods cannot be overridden in the same class because of ambiguity to call
4)   Methods can be overridden in inheritance only.
5)   In method overriding, overriding method can have same access modifier or less restrictive access modifier(must not be more restrictive)
6)   Final methods cannot be overridden because final keyword used to prevent method overriding.
7)   Static methods cannot be overridden because static members are not a part of an object.
8)   Private methods cannot be overridden because private members cannot be inherited.

Covariant return type:
Java permits sub class type as a return type while overriding a method. This is known as covariant return type.

Example:
class A{}
class B extends A{}
class C extends B
{
                    A get(){} => Overridden method
}
class D extends C
{
                    B get(){} => Overriding method
}

In the above example overriding method return type is sub class of overridden method return type. This is called covariant return type.

Upcasting:
Ø Assigning an object or object reference of sub class to super class type is known as upcasting.
Ø Upcasting done by system implicitly.
Ø Upcasting always valid.

Examples:
1)   A ob = new B(); (For example A is a super class and B is a sub class)
2)   B ob1=new B(); A ob2=ob1;

Downcasting:
Ø Assigning an object or object reference of super class to sub class typ is known as downcasting.
Ø Downcasting must be done by programmer explicitly, otherwise compile time error occurs.
Ø Downcasting always needs upcasting

Example:
1)   A ob1=new B(); => It is upcasting
B ob2=(B)ob1; => It is downcasting
(In the above example, assume A is a super class and B is a sub class)

Abstract Class:
Ø A class that is declared with abstract keyword is called as an abstract class.
Ø Abstract class can have only abstract methods, only non abstract methods or both abstract and non abstract methods.

Abstract Method:
Ø A method that has no body is called as an abstract method.
Ø Abstract method must be declared with abstract keyword in Java, otherwise compile time error occurs.

Example:   abstract void show();
Ø Non abstract methods are called concrete methods. A method that has a body is called as concrete method.

Example:   void print(){}
Ø If the class contains an abstract method, then the class must be declared with abstract keyword, otherwise compile time error occurs.
Ø Abstract classes cannot be instantiated.
Ø Instantiation means object creation.
Ø Abstract class can be inherited by using extends keyword.
Ø Whenever abstract class is inherited, then all abstract methods of an abstract class must be overridden in a sub class or sub class must be declared with abstract keyword, otherwise compile time error occurs.

Example:
abstract class A
{
          abstract void show();
          void print()
          {
          System.out.println(“print() method”);
}
}
class B extends A
{
          void show()
          {
                    System.out.println(“show() method”);
}
void display()
{
          System.out.println(“display() method”);
}
public static void main(String args[])
{
          B ob=new B();
          ob.show();
          ob.print();
          ob.display();
}
}

Ø Abstract methods cannot be final
Ø Abstract methods cannot be static
Ø Abstract methods cannot be private
Ø Abstract class cannot be final
Ø Abstract class can have constructors and those constructors are called whenever an object is created to sub class.
Ø Abstract class can have static members also.
Ø Abstract class can have main method also.

Interfaces:
Ø An interface is a collection of public static final variables and public abstract methods.
Ø In interface all variables are implicitly public static final and all methods are implicitly public abstract.
Ø An interface itself implicitly abstract
Ø Interfaces cannot be instantiated.
Ø Interface can be inherited into a class by using implements keyword.
Ø Interface can also be inherited by using extends keyword.
Ø Whenever interface is inherited then all methods of an interface must be overridden in a sub class or sub class must be declared with abstract keyword, otherwise compile time error occurs.

Example:
interface A
{
          int x=10;
          void show();
}
class B implements A
{
          int y=20;
          public void show()
          {
                    System.out.println(“show() method”);
}
void print()
{
          System.out.println(“print() method”);
}
public static void main(String args[])
{
          System.out.println(A.x);
          B ob=new B();
          System.out.println(ob.y);
          ob.show();
          ob.print();
}
}

Note: Class cannot be inherited in interface.
Interfaces are introduced in Java to achieve multiple inheritance.

Encapsulation:
Binding of variables with methods and those methods operating on variables. This is known as an encapsulation.

Example:
class Emp
{
          private int age;
          void setAge(int age)
          {
                    if(age>100)
                      this.age=100;
                    else if(age<0)
                      this.age=0;
                    else
                      this.age=age;
}
int getAge()
{
          return age;
}
}
class Demo
{
          public static void main(String args[])
          {
                    Emp e=new Emp();
                    e.setAge(200);
                    int x=e.getAge();
                    System.out.println(x);
}
}

In the above example, age variable bound with setAge() and getAge() methods and these methods are operating on same age variable. This is known as an encapsulation.

Inner Classes:
A class that is defined in another class is called as an inner class.

There are four types of inner classes:
1)   Member class
2)   Static member class
3)   Local class
4)   Anonymous class

1) Member class:
A class that is defined as member of another class is called as member class.

Example:
class A
{
          class B
          {
                    void show()
                    {
          System.out.println(“Welcome”);
}
}
}
class Demo
{
          public static void main(String args[])
          {
                    A a=new A();
                    A.B b=a.new B();
                    b.show();
}
}

Static Member Class:
A class that is defined as a static member of another class is called as static member class.

Example:
class A
{
          static class B
          {
                    void show()
                    {
          System.out.println(“Welcome”);
}
}
}
class Demo
{
          public static void main(String args[])
          {
                    A.B b=new A.B();
                    b.show();
}
}

Local Class:
A class that is defined in a method is called as local class.

Example:
class Demo
{
          public static void main(String args[])
          {
                    class Test
                    {
                              void show()
                              {
                              System.out.println(“Welcome”);
}
}
Test t=new Test();
t.show();
}
}

Anonymous Class:
It is a one type of local class that has no name. It is always sub class of a class or interface.

Example:
interface Test
{
                    void show();
}
class Demo
{
          public static void main(String args[])
          {
                    Test t=new Test()
                    {
                              public void show()
                              {
                                        System.out.println(“Welcome”);
}
};
t.show();
}
}
Ø Outer classes cannot be private and cannot be protected.
Ø Member class & Static member class can have all access modifiers.
Ø Access modifiers cannot be applied to local class and anonymous class.