Home » Core Java » class » Page 6

class

Anonymous inner class – Part 2

Bipartite Graph

This is an example of how to use an anonymous inner class. We have created a class, TalkingClock, that is a clock that prints the time in regular intervals and use it in another class, as described in the following steps: TalkingClock has a method start(int interval, final boolean beat). The method creates a new ActionListener object that overrides the actionPerformed(ActionEvent event) ...

Read More »

Run time Type Identification example

Bipartite Graph

In this example we shall show you how to check a type identification in run time. To check a type identification in run time we have performed the following steps: We have created a class A, with two methods a1() and a2(). We have also created a class B that extends A that overrides the inherited method from A class and ...

Read More »

Class casting example

Bipartite Graph

With this example we are going to demonstrate how to cast an object of a class to another class. In short, to cast an object of a class to another class we have followed the steps below: We have created class A, class B that extends A and has an int field and class C that extends B and also ...

Read More »

Anonymous inner class

Bipartite Graph

This is an example of how to use an anonymous inner class inside a class. In order to use an anonymous inner class we are using the javax.swing package to create a window with a button: We create a new JButton that is an implementation of a “push” button with the message “Close Window”. A new ActionListener object is created ...

Read More »

Anonymous class

Bipartite Graph

In this example we shall show you how to use an anonymous class. To use an anonymous class we are using an interface and override its method when creating a new object, as described in the steps below: We create a new interface AnonClass with a void call() method. We create a new instance of AnonClass, where we override its call() ...

Read More »

Abstract class example

Bipartite Graph

With this example we are going to demonstrate how to create and use an abstract class. In short, we have created an abstract class with an abstract method, that is extended by two other classes, as described below: We have created an abstract class Person, with a String field and a constructor using its field. It has an abstract method ...

Read More »

Use the keyword this to call a constructor – Part 2

Bipartite Graph

This is an example of how to use the keyword this to call a constructor of a class. Within an instance method or a constructor, this keyword is a reference to the current object, that is the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor ...

Read More »

Use the keyword this to call a constructor

Bipartite Graph

In this example we shall show you how to use the keyword this to call a constructor in a class. Within an instance method or a constructor, this keyword is a reference to the current object, that is the object whose method or constructor is being called. You can refer to any member of the current object from within an instance ...

Read More »

Simple use of the keyword this

Bipartite Graph

With this example we are going to demonstrate how to use the keyword this. Within an instance method or a constructor, this keyword is a reference to the current object, that is the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this ...

Read More »

Reference to an inner class

Bipartite Graph

This is an example of how to make a reference to an inner class. In order to make a reference to an inner class we have created a class with inner classes, as described in the steps below: We have created a class, InnerClass1 that has an int field and a method int value() that returns an int field. We have ...

Read More »