5.3 Modifiers & InheritanceHomepage  « Java5 Certification « 5.3 Modifiers & Inheritance

In this lesson we examine the effect of modifiers on inheritance with respect to constructors, instance or static variables, and instance or static methods.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 5: OO Concepts

    • Explain the effect of modifiers on inheritance with respect to constructors, instance or static variables, and instance or static methods.

Access Modifiersgo to top of page Top

The table below shows the four types of access available in Java from the most open (public) to the most restrictive (private). We can only explicitly apply the public access modifier to our top-level classes (the classes we compile) but for members and constructors we can explicitly apply the protected and private access modifiers as well. We will talk about packaging in the Packages lesson, but for now we are going to examine how to protect our class members from unwanted access and modification.

Access modifier Class Member Constructor Description
publicYesYesYesA top-level class may be declared with the public access modifier, and if it is the class is accessible to all other classes everywhere.
A member may be declared with the public access modifier, and if it is the member is accessible to all other classes everywhere, assuming the class it resides in is accessible.
A constructor may be declared with the public access modifier, and if it is the constructor is accessible to all other classes everywhere, assuming the class it resides in is accessible.
protectedNoYesYesA member may be declared with the protected access modifier, and if so, is only accessible within its own package and also by a subclass of its class in other packages.
A constructor may be declared with the protected access modifier, and if so, it is only accessible to the package the implementation is in.

See the Packages lesson for more information on packaging.
See the Inheritance Basics lesson for more information on subclassing.
no modifier
package-private /
(the default)
YesYesYesIf a top-level class has no explicit access modifier, which is the default and is also known as package-private, it is accessible only within its own package.
If a member has no explicit access modifier it is only accessible within its own package.
If a constructor has no explicit access modifier, it is only accessible to the package the implementation is in.

See the Packages lesson for more information on packaging.
privateNoYesYesA member may be declared with the private access modifier, and if it is the member is only accessible within its own class.
A constructor may be declared with the private access modifier, and if it is the constructor can only be constructed from within its own class.

Preventing Inheritance/Overridinggo to top of page Top

There might be a scenario whereby, you need to stop a class from being inherited. We can stop inheritance occurring by using the final keyword in the class definition:


/*
  A final Class
*/ 
public final class A {

}
/*
  B Class
*/ 
public class B extends A {

}

run extinding final test

The above screenshot shows the result of trying to compile class B.

On a similar note there might be a situation whereby, you need to stop a method from being overridden. We can stop this happening by using the final keyword in the method definition:


/*
  A Class
*/ 
public class A {

    final void aMethod () {
        System.out.println("Marked final so we can't override this method.");
    }   
}
/*
  B Class
*/ 
public class B extends A {

    void aMethod () {
        System.out.println("This won't work.");
    }   
}

run extending final test

The above screenshot shows the result of trying to compile class B.


Constructor Inheritancego to top of page Top

If you mark a superclass constructor with the private access modifier, any subclass that tries to extend it will fail with a compiler error when trying to access the superclass constructor with super(), either implicitly or explicitly. This of course defeats the whole point of inheritance, but is just something to be noted.

See OO Concepts - Superclass Constructors for detailed information on constructor inheritance.


Instance Inheritancego to top of page Top

If you mark a superclass instance variable with the private access modifier it is not inherited by subclasses but can be accessed using public setters and getters of the superclass, which is the essence of encapsulation and of course is available for instantiation purposes through implicit or explicit calls to super(). Other accessibility to a superclass instance variable is dependant upon the access modifier used and packaging as described in the Access Modifiers table above.


Static Inheritancego to top of page Top

If you mark a superclass static variable with the private access modifier it is not inherited by subclasses. Other accessibility to a superclass static variable is dependant upon the access modifier used and packaging as described in the Access Modifiers table above. Static methods that are accessible are also inherited by subclasses unless the subclass has a method with the same name and parameters which is known as method hiding and is discussed in OO Concepts - Static Overrides?


Related Java5 Tutorials

Objects & Classes - Instance Scope
Objects & Classes - Statics
OO Concepts - Encapsulation
OO Concepts - Inheritance Basics
OO Concepts - Superclass Constructors

go to home page Java5 Tutor Homepage go to home page Top