Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>A Class, Classes in Java Language

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



A Class, Types Classes in Java Language

The class and object concepts are included in the Object Oriented Programming Model to easily represent the real world entities in the software programming. We can make following definitions of a class in Java.

Definition 1:-

A Java class is a logical entity which represents a category.

Explanation:

Everythings around us are logical entities(Fruit, Car, Pen, Bike, etc) and physical entities( A Mango, A Honda Accord, Cello Fingergrip, Honda Unicorn, etc). A class is a logical entity because we always use class to represents a category like a Fruit; fruit is logical, we cannot see or eat fruit but we can see or eat a mango or a banana which are physical entities and belongs to the fruit category.

Similarly, we do not see car because it is logical, but we see and drive a Honda Accord which is a physical entity and comes under the car category. We create multiple objects of a class to represent similar type of physical entities.


Definition 2:-

A class in Java is a blueprint/model from which objects are created.

Explanation:

Consider you have a blueprint of a house; you can create or build same type (bunglow, apartment, etc) of houses but with different parameters (color of house, location address, etc).

Here the blueprint of a house is a class and multiple houses constructed with different colours and locations are objects.

A class is a model of an object because by just looking at the data members and member methods of a class we can predict what an object will contain even before creating that object. A class contain fields and methods to describe the state and behaviour of an object.


Syntaxes:-

Class keyword is required to declare a class. Following are syntaxes of a class in Java:

Syntax 1:


[access modifiers]class ClassName
{
// class body
}

Syntax 2:


[access modifiers]class ClassName
{
// class body
};

Note: Writing ‘;’ semicolon at the end of class body is optional in Java.

Note: A class name can be any valid identifier, but should not be a reserved keyword.


class HelloJava
{
// class body
}

What does a Java class contains?

  • Data members (instance variables, class/static variables, constants).
  • Constructors.
  • Static block.
  • IIB- Instance Initializer Block.
  • Methods.
  • Inner class.
  • Nested interface.

Important Rules for Java Class

  • The class keyword is must in class declaration, and must be followed by a legal identifier.
  • A Java class can have only public or default(no modifier) access modifiers.
  • A Java class can be either abstract, final or concrete (normal class).
  • A class in Java may optionally extend one base class only, because Java does not support multiple inheritance for classes. By default, it will extend java.lang.Object.
  • It may optionally implement any number of comma-separated interfaces.
  • In Java everything (variables, blocks, methods, etc) must be declared within the class body, a pair of curly braces {}.
  • If a class is declared public then it must be saved in a file with same name as class name with .java extension. Therefore a source file can contain only one public class. A source file may contain any number of non public classes.
  • An inner class can be private or protected.

Types of classes in Java Language

Concrete class- Any normal class which does not have any abstract method or a class which has implementation of all the methods of its parent class or interface and its own methods is a concrete class.

Abstract class- A class declared with abstract keyword and have zero or more abstract methods is known as abstract class. The abstract classes are incomplete classes therefore to use you strictly need to extend the abstract classes to a concrete class.

Final class- A class declared with final keyword is a final class, and it cannot be extended by another class, for example java.lang.System class.

POJO class- A class which contains only private variables and setter and getter methods to use those variables is called POJO (Plain Old Java Object) class. It is a fully encapsulated class.

Static class- Static classes are nested classes, means a class declared within another class as a static member is called a static class.

Inner Class- A class declared within another class or method is called an inner class.

Share the article to help your friends