Class and Interface in JavaClass in JavaA 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:
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.
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 JavaThe 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.
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. 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 interfaceIn 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 inheritanceJava 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.... Next TopicClassCast Exception in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India