31 July, 2013

Attempting to assign weaker access privileges - Error in Java

As per the rule in overriding , you cannot apply weaker access specifier over a stronger access specifier.Suppose , your parent class has a method Display() with stronger access specifier like Public, then you cannot use weaker access specifier like private or public when you are overriding the Display() method.

Note :-

 Public is the 1st stronger access specifier
 Protected is the 2nd stronger access specifier
 Default is the 3rd stronger access specifier
 Private is the most weaker access specifier

 Always keep in mind about the access specifiers has vital role in inheritance (OOPS concept).

 Example :-

 /*
 * @Author Manoj
 *
 */


class AccessTest{
    protected void display(){ //Stronger Access specifier
        System.out.println("Hello AccessTest:Display");
    }
}

class TestWithMain extends AccessTest{
    // I am trying to override with weaker specifier , it show error
    // You can use public or protected (higher or same level of access specifier)
    private void display(){
        System.out.println("Hello TestWithMain:display");
    }   
    public static void main(String str[]){       
        AccessTest acObj=new TestWithMain();
        acObj.display();
    }   
}

 
 Error :-

 TestWithMain.java:8: display() in TestWithMain cannot override display() in AccessTest;
 attempting to assign weaker access privileges; was protected
    private void display(){

   

So, now if you change the access specifier to protected or public then it will work properly.

Changed executable code :-

    public void display(){
        System.out.println("Hello TestWithMain:display");
    }

   
    OR
   
    protected void display(){
        System.out.println("Hello TestWithMain:display");
    }

   

Hope it will help you.

Access Specifier in Method Overriding

As per the rule in overriding , you cannot apply weaker access specifier over a stronger access specifier.Suppose , your parent class has a method Display() with stronger access specifier like Public, then you cannot use weaker access specifier like private or public when you are overriding the Display() method.

Note :-

 Public is the 1st stronger access specifier
 Protected is the 2nd stronger access specifier
 Default is the 3rd stronger access specifier
 Private is the most weaker access specifier

 Always keep in mind about the access specifiers has vital role in inheritance (OOPS concept).

 Example :-

 /*
 * @Author Manoj
 *
 */


class AccessTest{
    protected void display(){ //Stronger Access specifier
        System.out.println("Hello AccessTest:Display");
    }
}

class TestWithMain extends AccessTest{
    // I am trying to override with weaker specifier , it show error
    // You can use public or protected (higher or same level of access specifier)
    private void display(){
        System.out.println("Hello TestWithMain:display");
    }   
    public static void main(String str[]){       
        AccessTest acObj=new TestWithMain();
        acObj.display();
    }   
}

 
 Error :-

 TestWithMain.java:8: display() in TestWithMain cannot override display() in AccessTest;
 attempting to assign weaker access privileges; was protected
    private void display(){

   

So, now if you change the access specifier to protected or public then it will work properly.

Changed executable code :-

    public void display(){
        System.out.println("Hello TestWithMain:display");
    }

   
    OR
   
    protected void display(){
        System.out.println("Hello TestWithMain:display");
    }

   

Hope it will help you.

29 July, 2013

How to inherit a constructor in JAVA

First of all when we are talking about the inheritance , Java does not inherit the construtor from super class.As we know constructor is one of the member of a , but we cannot inherit it for sub class (child class).Yes, we can invoke a super class (parent class) constructor from sub class by using the keyword "super".

Few Points about 'super' Keyword :-

1. Its a reserved keyword by Java API.
2. It is used to invoke or call super class (parent class) members .
3. When 'super' keyword is used inside the sub class (child class) constructor , 'super' keyword
     is the first line inside the constructor.

Example :-

/*
 * @Author Manoj
 * 29/07/2013
 */

class SUP{
    public SUP(String s){
        System.out.println("Hi Super: "+s);
    }
}


//SUB class extending SUP class
class SUB extends SUP{
    public SUB(String p){
        // Explicitly call the super class argument constructor
        // super is the first line inside the constructor
        super(p);
        System.out.println("Hi SUB: "+p);   
    }
   
    public static  void main(String str[]){
        new SUB("Manoj Kumar");
    }
}





OutPut :-



Hi Super: Manoj Kumar
Hi SUB: Manoj Kumar

Note :- If super class(parent class) constructor is a no-argument constructor the no need to call that constructor by using 'super'
keyword inside the sub class (child class) constructor .

When you invoke sub class (child class) constructor with no-argument, automatically super class (parent class) no-argument constructor
called or invoked.

Hope it will help you.