Access specifiers

  1. Private
  2. Default also called package private
  3. Public
  4. Protected

Private

The classes,data members and methods declared as private cannot be accessed outside of the class. These  can be accessed within the body of the class.The data members  can be accessed by the  public methods  of the class.

It is the most restrictive form of the access specifiers.

Default 

If we do not specify any access specifier then in Java this default access specifier is applied.

The data members and methods are accessible from the classes within the same package.

Protected

Data members, functions and classes declared as protected can be accessed by the classes present in the same package and by the subclasses present in different package.

Public 

Data members methods and classes declared as public are accessible across all the packages.It is the very least restrictive.

Inheritance

Class A{
int value;
static int count;
public static void calculate();
public void show(){
System. Out.println("Show method of Class A");
}
}
Class B extends A
{
public void show(){
System. Out.println("Show method of Class B");
}
}

When a class B extends the anotherclass A, the instance variables and the instance methods are inherited from the superclass(A)  to the subclass(B).

Constructors are not inherited rather constructor chains are formed.

The class variable and class methods also call static fields and static methods are not inherited.

 we can even access the static fields from the subclass using the subclass  or reference variable of subclass. Static fields can also be accessed through the superclass  or reference of superclass.

It doesn’t matter where is the modification to the static fields are made whether  there it is in subclass and superclass because the static fields have the common memory area.

 Polymorphism

Polymorphism is the ability to assume different forms.

The statement has three parts as 

[Declaration] [assignment (=)] new [object creation]  

Declaration is also referred as reference creation. 
Eg:

A aobj =new B();

Here aobj is a reference type A to the object of type B. 

 Overridden instance methods are called based on the object type Where as static methods are all based on the reference type

If we override  static methods in the sub class , they hide the super class static methods from participating in the  polymorphism at runtime. So there is no worth use of overriding the static methods in subclass.
We can have a reference of superclass assigned to a subclass object. This is so because the subclass possess a relationship is a with the superclass.

 For example a car is a vehicle.

But a subclass reference cannot be assigned to a superclass object.

If we call the overridden methods using the superclass reference then the methods of the particular subclass are executed by the JVM based on the object type this is called runtime polymorphism.

Using superclass reference we cannot call the methods which are not inherited from the superclass by the subclass.

Polymorphism can be seen as

  • Polymorphic method invocation.
  • polymorphic arguments .
  • polymorphic return types.

    Advertisements