Tuesday, April 30, 2019

Access Modifiers in Java


In Java Programming, we have Four Access Modifiers, they are:
1.Default
2.Private
3.Public
4.Protected

Default Access Modifier:


If we don’t mention any access modifier means it is a default access modifier, this modifier is can’t be accessed by any other package, and it can be accessed within the package only. The classes which present in the program Core Java Training in Chennai only access this modifier, The outside package access is restricted to this default modifier.Likewise, method and member can be accessed within the class only

Example of Default Access Modifier:


package vbnpackage;

public class Girl {
  /* Since we didn't mention any access modifier here, it would
   * be considered as default.
   */
  int add(int c, int d){
return c+d;
  }
}

Test1.java
package poipackage;

/* We are importing the vbnpackage
* but still we will get error because the
* class we are trying to use has default access
* modifier.
*/
import vbnpackage.*;
public class Test1 {
  public static void main(String args[]){
Girl obj = new Girl();
       /* It will throw error because we are trying to access
        * the default method in another package
        */
obj.add(10, 21);
  }
}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method addTwoNumbers(int, int) from the type Add is not visible
at poipackage.Test1.main(Test1.java:12)

Private Access Modifier:


Private Access Modifier is similar to Default Access Modifier, The scope of this private modifier is limited to the class only. The name itself Java Training Institute in Chennai we can understand this private access modifier, “Private” the data members and packages, methods are only accessed within the class. It cannot create the object to constructor from outside of the class.

Example of Private Access Modifier:

class DEF{  
  private double num1 = 100;
  private int squ(int a){
return a*a;
  }
}  
public class Exp{
  public static void main(String args[]){  
DEF obj = new DEF();  
System.out.println(obj.num1);
System.out.println(obj.squ(10));
  }
}

Output:
Compile - time error

Protected Access Modifier

Protected Access Modifier Java Training Institute in Chennai is quite different, Protected data member and method are only accessed by the class of same package and subclasses present in any package.

Example of Protected Access Modifier:

package asdpackage;
public class Addition {

  protected int add1(int l, int k){
return l+k;
  }
}

Test2.java
package ghtpackage;
import asdpackage.*;
class Test2 extends Addition{
  public static void main(String args[]){
Test2 obj = new Test2();
System.out.println(obj.add1(11, 22));
  }
}

Output:
33

Public Access modifier

Public access modifier can be accessed within and outside the classes , it allow all the package and data member to access the class.Core Java Training in Chennai

Example of Public Access Modifier:

package zxcpackage;

public class Addition1 {

  public int add2(int t, int y){
return t+y;
  }
}

Test3.java
package dsapackage;
import zxcpackage.*;
class Test3{
  public static void main(String args[]){
     Addition1 obj = new Addition1();
     System.out.println(obj.add2(100, 1));
  }
}

Output:
101

Monday, April 15, 2019

Polymorphism - JAVA


Define Polymorphism?
Polymorphism is the feature of oops, it allow the user to do the single action in many different ways.this is called polymorphism.


Types of Polymorphism:
Polymorphism are classified into two types, they are:
1.Static Polymorphism
2.Dynamic Polymorphism


Example of Polymorphism:
public class Animal{
  ...
  public void sound(){
     System.out.println("Animal is making a sound");   
  }
}


public class Horse extends Animal{
...
   @Override
   public void sound(){
       System.out.println("Neigh");
   }
}


public class Cat extends Animal{
...
   @Override
   public void sound(){
       System.out.println("Meow");
   }
}


As you can see all the class extends Animal method,this is called polymorphism,one action is done by many several ways.Java Training Institute in Chennai


Static Polymorphism:
Static polymorphism is also known as compile time polymorphism.this is because these polymorphism is resolved in the compile time.the perfect example of static polymorphism are Method Overloading


Example of static Polymorphism

class SimpleCalculator
{
   int add(int a, int b)
   {
        return a+b;
   }
   int add(int a, int b, int c)  
   {
        return a+b+c;
   }
}
public class Demo
{
  public static void main(String args[])
  {
  SimpleCalculator obj = new SimpleCalculator();
      System.out.println(obj.add(10, 20));
      System.out.println(obj.add(10, 20, 30));
  }
}


Output
30
60


Method Overloading:
Method Overloading allows Core Java Training in Chennai the method to have the same name but different argument list, it is the same as constructor overloading.


3 Different ways we can overload a method:
1.No of Parameters
2.Sequence or order of parameter
3.Data type


Example of Method Overloading:
class DisplayOverloading
{
   public void disp(char c)
   {
        System.out.println(c);
   }
   public void disp(char c, int num)  
   {
        System.out.println(c + " "+num);
   }
}
class Sample
{
  public static void main(String args[])
  {
      DisplayOverloading obj = new DisplayOverloading();
      obj.disp('a');
      obj.disp('a',10);
  }
}
Output:
a
a 10


Dynamic Polymorphism:
Run time Polymorphism is also known as dynamic polymorphism, this is because the dynamic polymorphism function the Java Training Institute in Chennai command in the run time, it is an example of Method Overriding.


Example of Dynamic Polymorphism:
class ABC{
  public void myMethod(){
System.out.println("Overridden Method");
  }
}
public class XYZ extends ABC{


  public void myMethod(){
System.out.println("Overriding Method");
  }
  public static void main(String args[]){
ABC obj = new XYZ();
obj.myMethod();
  }
}
Output:
Overriding Method


Method Overriding:
Method overriding allows the Core Java Training in Chennai user to override the method to one or more times, due to this feature we can able to reuse the code.


Example of Method Overriding:
class Human{
  //Overridden method
  public void eat()
  {
     System.out.println("Human is eating");
  }
}
class Boy extends Human{
  //Overriding method
  public void eat(){
     System.out.println("Boy is eating");
  }
  public static void main( String args[]) {
     Boy obj = new Boy();
     //This will call the child class version of eat()
     obj.eat();
  }
}
Output:
Boy is eating