Java

Some basic Java stuff

Access Modifiers in Java

As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member. There are four types of access modifiers available in java:

  1. Default – No keyword required
  2. Private
  3. Protected
  4. Public

Access-Modifiers-in-Java.png

  • The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.
  • The methods or data members declared as private are accessible only within the class in which they are declared.
  • Any other class of same package will not be able to access these members.
  • Classes or interfaces can not be declared as private.
  • The methods or data members declared as protected are accessible within same package or sub classes in different package.
  • The public access modifier has the widest scope among all other access modifiers.
  • Classes, methods or data members which are declared as public are accessible from every where in the program. There is no restriction on the scope of a public data members.

Nested Classes

In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.

Nested classes are divided into two types −

  • Non-static nested classes − These are the non-static members of a class.
  • Static nested classes − These are the static members of a class.

inner_classes

Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.

Inner classes are of three types depending on how and where you define them. They are −

  • Inner Class
  • Method-local Inner Class
  • Anonymous Inner Class

Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class. In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined. An inner class declared without a class name is known as an anonymous inner class. In case of anonymous inner classes, we declare and instantiate them at the same time.

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

Abstract class vs Interface

  1. Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
  2. Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
  3. Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
  4. Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
  5. Inheritance vs Abstraction: A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.
  6. Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  7. Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

 

Leave a comment