Table of content:
Classes in Java- Introduction
Abstract class in Java
Final class in Java
Anonymous class in java
Nested classes in Java
static member classes in Java
Non-static member classes in Java
Local classes in Java
What are classes in Java ?
As we know that java is an object-oriented programming language. This means everything in java exhibits as an object. An object must have some properties, behavior, pattern, and attributes. These properties, behavior, and attributes of an object are defined in the class. A class in java acts as a blueprint for creating an object. Thus a class in java defines the properties and attributes which its objects will exhibit. The basic structure of a class is as below:
class abc{
int i; //instance variable declaration
void a(){// instance method definition
--------
--------
}
}
Here the class is a keyword that tells the compiler that the identifier 'abc' is a class.
'i' is an instance variable, whereas 'a()' is an instance method/function.
Here a class has defined the functions and variables which an object will exhibit.
What is an abstract class in java:
An abstract class is an incomplete class, therefore we cannot create its object. In other words, an abstract class cannot be instantiated. By incomplete class, we mean that the properties or methods are too generic to be used in the practical sense. For instance, a class vehicle is too generic to be used in the real sense. Its object must exhibit some specific operations denoting some specific vehicle-related properties. If a class has any one or more abstract methods, then the class must be declared as an abstract class. An abstract class is used to define certain behavior but its sub-class has to provide more specific behavior.
For e.g.
abstract class vehicle {
int noOfTyres;
int noOfHeadLights;
float tyresize;
public void tyreSize(int i)
{
tyresize=i;
}
abstract public twoOrFourWheeler(int tyreNumbers);
}
Here the method 'twoOrFourWheeler' is abstract i.e. it is only declared but not defined.
i.e. It is not performing any operation. Therefore the entire class 'vehicle' has to be declared as
abstract. Now whatever class inherits this vehicle class has to provide the definition to the
abstract method of the class 'vehicle'.
class car extends vehicle{
public twoOrFourWheeler(4){
System.out.println("It is a four wheeler");
}
}
What is a final class in java?
A final class is one that is complete in itself therefore its subclass cannot be created and its methods cannot be overridden. In other words, no more specialization is possible for the class therefore a final class cannot be extended. A class can be declared as final to indicate that its subclass cannot be created or in other words, the behavior of the final class cannot be changed by subclassing. A class whose definition is complete i.e. all its methods have implementation can be declared as final. Since an abstract class is an incomplete class whereas a final class is complete therefore a class cannot be both final and abstract at the same time.
So, if we want that the behaviors, functions, fields of a class cannot be overridden, and also if we do not want any more specialization of a class, then declare it as final.
Properties of a final class:-
1. A final class is complete i.e. all its functions have a definition.
2. A class marked as final cannot be extended.
3. As the final class cannot be extended, therefore its methods cannot be overridden.
4. A final class can extend an abstract class.
What is an Anonymous class in java?
A class without a name is called an anonymous class. An anonymous class is defined and instantiated in a single step, unlike usual classes which are first defined and then instantiated using the new operator. Since the anonymous classes do not have any name, therefore, the instance of these classes can only be created together with the class definition. Moreover, A constructor cannot be defined for an anonymous class, and all its members are non-static in nature. Usually, the anonymous classes are used to create objects on the fly usually during return statements while returning an object or passing an argument in a method call or while initialization of variables.
An anonymous class also comes under one of the inner class categories.
The syntax for defining and initializing the anonymous class is as follows:
new superclass name (optional argument list)
{
member declaration of the class
}
};
What are nested classes in java?
A class declared within another class or interface is called a nested class. The class within which a class is nested is called a top level class. OR the top level class is the one which is not nested. Nested classes are also called inner classes, and an inner class can access the instance members of its enclosing class. The instance of the enclosing class is called immediately enclosing instance. An instance of an inner class can access the members of its immediately enclosing instance by their names.
The nested classes and interfaces are categorized into 4 categories as shown below:
1. static member classes and interfaces
2. non-static member classes
3. local classes
4. anonymous classes
static member classes and interfaces
A nested class that is defined with keyword static inside a class. A static member class or interface is defined as a static member in a class or an interface. Such a nested class can be instantiated like any ordinary top-level class, using its full name. No enclosing instance is required to instantiate a static member class. A static member class can access only static members of its enclosing class. However, a static member class can define both static as well as non-static members in it. Below is the format for instantiating a static member class:
ToplevelClass.staticMemberClass obj=new ToplevelClass.staticMemberClass();
Non-static member classes
Non-static member classes are inner classes that are defined without the keyword static.
A non-static member class can only be instantiated using an instance of its enclosing class.
A non-static member class cannot define static members inside it.
However, a non-static member class can access all the members of its enclosing class including static as well as non-static.
local classes
An inner class that is defined in a block, is called a local class. A block could be anything like- a method, or a constructor, any static or instance initializer. If a local class is contained in a static block, for e.g. a local class defined inside a static method, then the local class implicitly becomes static. However, a local class cannot be defined with the keyword 'static'. A local class cannot have static members because a local class cannot provide class-specific services. Also, the local classes cannot have accessibility modifiers and the same scope rule applies to the local class as applicable on the local variables. A local class can only be instantiated in the block in which it is defined. If a local class is non-static, then an instance of its enclosing class is required to instantiate a non-static local class.