Sunday, 27 December 2015

Basic Oops concept

Basic Oops concept are : 
  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Abstraction
1. Inheritance : 
Inheritance is one such concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes.

In Java, there are two classes:
  • Parent class ( Super or Base class)
  • Child class (Subclass or Derived class )
A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

Read more about Inheritance here.

2. Polymorphism :
Polymorphism means taking many forms, where ‘poly’ means many and ‘morph’ means forms. It is the ability of a variable, function or object to take on multiple forms. Polymorphism in Java is of two types:
  1. Run time polymorphism 
  2. Compile time polymorphism
Read more about Polymorphism here


3. Encapsulation :
Encapsulation is a mechanism where you bind your data and code together as a single unit. It also means to hide your data in order to make it safe from any modification. 

We can achieve encapsulation in Java by:
Declaring the variables of a class as private. Providing public setter and getter methods to modify and view the variables values.

Read more about Encapsulation here.

4. Abstraction :

Abstraction is process of hiding the implementation details and showing only the functionality. 

Abstraction in java is achieved by using interface and abstract class. Interface give 100% abstraction and abstract class give 0-100% abstraction.


Read more about Abstraction here.

Wednesday, 18 November 2015

Method Overloading

 Method Overloading :  

Method Overloading is a feature that allows a class to have two or more method in the same class with same name but their parameters declarations are different (different number of parameters, different types of parameters, or both).

It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.

There are different ways to perform method overloading :
  • By changing the number of parameters
  • By changing the datatypes of the parameters
  • By changing the sequence of datatype of parameters. 
1.By changing the number of parameters :
    
package com.tutorial4u;
class Addition {
       public void sum(int a, int b) {
             int sum = a+b;
             System.out.println("sum of a and b :"+sum);
       }
       public void sum(int a , int b, int c) {
             int sum = a+b+c;
             System.out.println("sum of a, b, and c : "+sum);
       }
}
public class OverloadingExample {
       public static void main(String[] args) {
             Addition add = new Addition();
             add.sum(10, 20);
             add.sum(10, 20, 30);
       }
}
    
Output : 

sum of a and b :30
sum of a, b, and c : 60
2. By changing the datatypes of the parameters :

package com.tutorial4u;
class Addition {
       public void sum(int a, int b) {
             int sum = a+b;
             System.out.println("sum of a and b :"+sum);
       }
       public void sum(double a , double b) {
             double sum = a+b;
             System.out.println("sum of a, b, and c : "+sum);
       }
}
public class OverloadingExample {
       public static void main(String[] args) {
             Addition add = new Addition();
             add.sum(10, 20);
             add.sum(10.5, 20.5);
       }
}

Output : 

sum of a and b :30
sum of a, b, and c : 31.0

3.By changing the sequence of datatype of parameters. 

package com.tutorial4u;
class Tutorial {
       public void display(int a, char c) {
             System.out.println("I am the first definition of method");
       }
       public void display(char c , int b) {
             System.out.println("I am the second definition of method");
       }
}
public class OverloadingExample {
       public static void main(String[] args) {
             Tutorial add = new Tutorial();
             add.display(10, 'a');
             add.display('b', 20);
       }
}
Output : 

I am the first definition of method
I am the second definition of method

Case 1: what happen when method signature is same but return type is different ?

package com.tutorial4u;
class Addition {
       public int sum(int a, int b) {
             int sum = a+b;
             return sum;
       }
       public double sum(int a , int b) {
             double sum = a+b;
              return sum;
       }
}
public class OverloadingExample {
       public static void main(String[] args) {
             Addition add = new Addition();
             add.sum(10, 20);
             add.sum(10, 20);
       }
}
Answer : It will through a compilation error. like : Duplicate method sum(int, int) in type Addition

Case 2:  Can we overload java main() method?

Answer : Yes, We can overload the main method any numbers of times but JVM calls main() method which receives string array as arguments only  .
Example : 

class OverloadingExample{ 
  public static void main(String[] args){
       System.out.println("method 1");
       } 
  public static void main(String args){
         System.out.println("method2");
         } 
  public static void main(){
         System.out.println("method3");
        } 
}

Output : method 1

Important Points : 

  1. Java does not allow overloading by changing the return type, though overloaded methods can change the return type.
  2. private methods can be overloaded.
  3. final methods can be overloaded.
  4. Main method can be also overloaded.
  5. static method can be overloaded.

Friday, 16 October 2015

Overriding Method

Overriding Method: 

If a method in the subclass has the same and same type signature as that of the method in super class, This is known as method overriding in Java.

  • It is used to achieve run time polymorphism.
  • It is used to provide the own specific implementation of a inherited method without even modifying the parent class code.
Different ways to perform method Overriding :
  • By method name : method name should be same as of super class method 
  • Number of parameter : method must have same number of parameters.
  • Access Modifier : method must not have more restrictive access modifier. 
          (More restrictive) private   ⟹   default   ⟹    protected   ⟹    public  (Less restrictive)

  • Return type : method overriding is possible by changing the return type, but only Covariant return type are allowed.
  • Exception Handling in Overriding  : check here in details
     
Case 1:  By Method Name 


package com.tutorial4u;

class Animal{
       public void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       @Override
       public void display() {
             System.out.println("I am a cat.");
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  //Output : From Super Class
             Cat cat = new Cat();
             cat.display(); //Output : From Sub Class
             Animal animal1 = new Cat();

             animal1.display();// Output from sub class
       }

}

Output : I am a animal.
              I am a cat.
              I am a cat.

Case 2: Access Modifier 

(A)
package com.tutorial4u;

class Animal{
       protected void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       public void display() {
             System.out.println("I am a cat.");
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  //Output : From Super Class
             Cat cat = new Cat();
             cat.display(); //Output : From Sub Class
       }

}

OutputI am a animal.
         I am a cat. 

(B)

package com.tutorial4u;

class Animal{
       protected void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       //Compilation Error, Cannot reduce the visibility of the inherited method from Animal

       //here visibility must be protected or public but not private or default.
       private void display() {
             System.out.println("I am a cat.");
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  
             Cat cat = new Cat();
             cat.display(); 
       }
}


Output : Compilation Error, Cannot reduce the visibility of the inherited method from Animal

Case 3: Number of parameter

package com.tutorial4u;

class Animal{
       public void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       public void display(int i) {
             System.out.println(i);
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  //Output : From Super Class
             Cat cat = new Cat();
             cat.display(); //Output : From super class
             cat.display(10);
       }
}

Output :
I am a animal.
I am a animal.
10

Note :  You must not change the arguments of method in subclass. If you change the number of arguments or types of arguments of overrided method in the subclass, then method will be overloaded not overrided.

Case 4:  Return Type 
You can only return same type or a subclass of the parents return type , it is known as covariant return type. 

Example : 
package com.tutorial4u;

class A {
}

class B extends A {
}

class Super {
       public A m() {
             System.out.println("Super class method");
             return new A();
       }
}

class Children extends Super {
       public B m() {
             System.out.println("Children class method");
             return new B();
       }
}

public class OverridingExample {
       public static void main(String args[]) {
             Super sup = new Super();
             sup.m();
             Children child = new Children();
             child.m();
       }
}

Output :  Super class method
Children class method

Ques 1 : Can we override static method ?
Answer :  No, But we can declare static method with same signature in sub class. It is not consider overriding as there won't be any run time polymorphism.
Here, Method in the derived class hides the method in the base class.

Example 1: 
package com.tutorial4u;

class A {

       public static void m1() {

             System.out.println("method A");

       }

}

class B extends A {

       public static void m1() {

             System.out.println("method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

       }

} 


Output : 
method A

Note :  If non-static method is used in above program, output would be "method B" method.

Example 2: 

package com.tutorial4u;

class A {

       public static void m1() {

             System.out.println("method a1");

       }

       public void m2() {

             System.out.println("method a2");

       }

}

class B extends A {

       //Compilation Error: This instance method cannot override the static method from A

       public void m1() {

             System.out.println("method b1");

       }

       //Compilation Error: This static method cannot hide the instance method from A

       public static void m2() {

             System.out.println("method b2");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

             a.m2();

       }

}

Output : 

method a1

method a2


Important Pints : 
  • private method can't be overridden in java because private method can not inherited in subclass.
  • final method can't be overridden in java because final method can not inherited in subclass.
  • static method can't be overridden in java .
  • Main() method can't be overridden in java because main() method have static method and static method can't be override.
  • Overriding method must not have more restrictive access modifier.
  • Only instance method can be override.

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...