Javatpoint Logo
Javatpoint Logo

Class and Interface in Java

Class in Java

A Class can be defined as the collection of objects that have a similar type of properties. It is a logical entity that can be seen as a blueprint for creating the objects. A Class can have many objects where each has the attributes and behavior defined by the class itself. However, we can also create a singleton class that has only a single instance.

In Java, a class can contain the following entities:

  1. Attributes
  2. Behavior(methods)
  3. Constructors
  4. Blocks
  5. Nested class and interface

Consider the following syntax to declare a class in java.

The 'class' keyword is used to declare a class. The class declaration may contain the following components defined in the sequence.

  1. Access Modifier: this defines the access modifier for the class, i.e., whether the class is public, private, protected, or default.
  2. class keyword: it is mandatory to use the class keyword to declare a class.
  3. Class name: the class name should follow the Java naming conventions and must be in CamelCase. It should not be any keyword name also.
  4. Superclass (if required): A class may inherit any base class, if any. The keyword extends used to inherit any base class in our class.
  5. Interface (If required): A class may implement any interface if required. The keyword implements is used to implement any interface in our class.
  6. Class body: the class body starts with an opening brace {and ends with a closing brace}. We can define instance variables, constructors, methods, nested classes and interfaces in the class body.

In Java, a class is only a blueprint, and the memory is occupied only when the object of the class is created. However, java provides new keyword to create an object of the class.

Consider the following example to create an object of the class and using the class's behavior with the object.

Output:

initial amount: 0.0
amount after credit 9002020.0
amount after debit 9000000.0

We can also use another class to instantiate this class, i.e., the main method can take place in another class. Consider the following example.

Bank.java

Main.java

Output:

amount after credit 9002020.0

We can encapsulate multiple instance variables within a single class to form a java bean. However, we can modify the access specifiers to ensure; no one can use the class properties directly.

We can also make use of inheritance in java to ensure the child class uses the properties and behavior of the parent class. Consider the following example for multi-level inheritance using two classes in java.

Animal.java

Dog.java

Output:

My Dog is eating .....
Dog is barking...

Interface in Java

The interface in Java can be defined as the blueprint of the class. An interface can have abstract methods and static constants. By using the interface, we can achieve abstraction in java. We can also achieve multiple inheritance in java using interface. We cannot define the method body in the interface.

An interface is different from abstract classes, i.e., an interface can't be instantiated, just like the abstract class. However, fields are static, public, and final in the interface, whereas; methods are public and abstract.

There are the following reasons for which the interface is mainly used in java.

  1. We can achieve multiple inheritance by using interface.
  2. We can achieve abstraction by using interface.
  3. Interface, in java, is also used to achieve loose coupling.

As we have already discussed, the interface is the blueprint of a class, i.e., it is implemented by a class to define its methods. However, classes implement the interface in their way. Here, we should also consider that any class that implements the interface must define its every method.

In java, the interface keyword is used to declare the interface. Consider the following syntax to declare the interface.

Like class, the interface can also inherit another interface. However, a class implements the interface. Consider the following image to understand the relationship between class and interface.

Class and Interface in Java

Consider the following example to create an interface in java.

Drawable.java

DrawRect.java

DrawCirlce.java

DrawMain.java

Output:

We'll draw rectangle here
We'll draw Circle here

Multiple Inheritance in java by using interface

In the following example, we will implement the multiple inheritance in java by using the interface.

Drawable.java

Printable.java

Main.java

Output:

We will use this one to print something....
We will use this one to draw something....

Default and static methods in inheritance

Java 8 allows us to define the method body in the interface. However, we can only define the body for the default methods. Also, we can define the static methods in the interface. Consider the following example.

Main.java

Output:

this is a static method in the interface
this is a default method in the interface
We will use this one to draw something....





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA