Java

How to Work with Sub Classes in Java4 min read

In Java, a subclass is a class that inherits properties and methods from its parent class, also known as the superclass. The subclass can also add its own properties and methods, which are specific to that subclass.

To create a subclass, the keyword “extends” is used to indicate that the new class is a subclass of the parent class. For example:




The ChildClass inherits all the properties and methods from the ParentClass, but can also have its own unique properties and methods.

To access the properties and methods of the superclass within the subclass, the keyword “super” is used. For example, to call a method from the superclass, you can use “super.methodName();”.

When the subclass overrides a method of the superclass, it provides a new implementation for that method. The subclass version of the method will be used instead of the superclass version when the method is called on an object of the subclass. This is also known as polymorphism.

It’s also possible to use the “super” keyword to call the constructor of the superclass within the constructor of the subclass.

In this case, the constructor of the ParentClass is called before the constructor of the ChildClass.

You can also use the “super” keyword to access the variables of the superclass.

In this case, the variable x of the Child class will be the value of the variable y of the Parent class.

Following is a Java program which handles the mouse click event using inner class and adapter class:

This is a Java program that creates an Applet named “MyApplet” using the JApplet class. The Applet has a JLabel component named “label” and sets its size to 600×300 pixels with a FlowLayout. The FlowLayout is a layout manager that positions components in a left-to-right, top-to-bottom flow.

The program also adds a MouseListener to the Applet, specifically an instance of the inner class MyAdapter. The MyAdapter class extends the MouseAdapter class, which is an abstract adapter class for receiving mouse events. The MyAdapter class overrides the mouseClicked() method of the MouseAdapter class and sets the text of the label to “Mouse is clicked” when the mouse is clicked.

When the Applet is executed, it will create a window with the specified size, add the label to it, and wait for the user to click on the window. When the user clicks on the window, the text of the label will change to “Mouse is clicked”.

Anonymous Inner Classes

In Java, an anonymous inner class is a type of inner class that does not have a name. Anonymous inner classes are used when a class is needed only once and it is not necessary to give it a name. They are typically used as a shorthand way of providing a class that implements an interface or extends a class.

An anonymous inner class is defined and instantiated in a single step, using the new keyword and the class or interface to be implemented or extended. For example, an anonymous inner class that implements the ActionListener interface might be defined as follows:

In this example, an anonymous inner class is created that implements the ActionListener interface and overrides its actionPerformed() method. The instance of the anonymous inner class is then passed as an argument to the addActionListener() method of the button object.

It’s worth noting that Anonymous Inner classes can only be instantiated once and they cannot have constructors because they don’t have a name.

Also, Anonymous Inner classes can only be declared and instantiated at the same time, making it impossible to define it separately and instantiate later.

In summary, anonymous inner classes are useful when a small, one-time use class is needed and when it is not necessary to give the class a name. They can be a shorthand way of implementing an interface or extending a class and providing an implementation for one or more of its methods.

The above mouse event handling program can further be simplified by using an anonymous inner class as shown below:

In the above program, the syntax new MouseAdapter() {…} says to the compiler that the code written in between the braces represents an anonymous inner class and it extends the class MouseAdapter.

Whenever the line addMouseListener… executes, Java run-time system automatically creates the instance of anonymous inner class.

Take your time to comment on this article.

Leave a Comment