[go: up one dir, main page]

0% found this document useful (0 votes)
34 views4 pages

Programming in Java Programming in Java: Anonymous Inner Class Anonymous Inner Class (Contd.)

The document discusses anonymous inner classes in Java. It provides an example of using an anonymous inner class to implement an action listener for a start button that will begin a game. The document also discusses that anonymous inner classes do not have a name and are either a subclass or interface implementer. It then asks which type of inner class can be defined without a name, to which the answer is an anonymous inner class.

Uploaded by

Dhimas N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

Programming in Java Programming in Java: Anonymous Inner Class Anonymous Inner Class (Contd.)

The document discusses anonymous inner classes in Java. It provides an example of using an anonymous inner class to implement an action listener for a start button that will begin a game. The document also discusses that anonymous inner classes do not have a name and are either a subclass or interface implementer. It then asks which type of inner class can be defined without a name, to which the answer is an anonymous inner class.

Uploaded by

Dhimas N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4/19/2021

Programming in Java Programming in Java

Anonymous Inner Class Anonymous Inner Class (Contd.)


Scenario: Scenario (Contd.):
A developer needs to
design a startup
screen interface with
Needs to To implement this functionality, a developer
a start button.
develop a needs to override the actionPerformed()
gaming method of the ActionListener interface.
application by
using the Swing
components. The game should begin
when the start button An anonymous inner class has no name, and it is either a subclass
is clicked. of a class or an implementer of an interface.
A developer In case of the anonymous class, a curly brace is followed by the
semicolon.

Ver 1.0 Slide 1 of 15 Ver 1.0 Slide 2 of 15

Programming in Java Programming in Java

Anonymous Inner Class (Contd.) Just a minute


Consider the following code snippet to implement an anonymous Which one of the following inner classes can be defined without the
inner class: name of the class?
start.addActionListener(new ActionListener() Regular inner class
{
Static inner class
public void actionPerformed(ActionEvent e)
{ Method-local inner class
/* code to be added here */ Anonymous inner class
}
});

Ver 1.0 Slide 3 of 15 Ver 1.0 Slide 4 of 15

1
4/19/2021

Programming in Java Programming in Java

Just a minute (Contd.) Activity 1.1: Creating Inner Classes


Solution: Problem Statement:
Anonymous inner class Mellos Inc. is a software development company. The company has
received an assignment to develop an aptitude assessment
application. The management of the company has assigned this task to
Mike, the Senior Developer of the company. In the initial phase, Mike
needs to create a class that accepts the candidate details, such as
name, qualification, and age. In addition, he needs to validate the age
of the candidate who can appear for the test. The age of the candidate
must range between 21 and 25. Further, Mike needs to logically group
the class, so that each class provides its own functionality. Help Mike in
achieving the preceding tasks.

Ver 1.0 Slide 5 of 15 Ver 1.0 Slide 6 of 15

Programming in Java Programming in Java

Activity: Creating Inner Classes (Contd.) Implementing Type Casting


Solution: To perform the activity, refer the steps given in the In Java, it is possible to assign a sub class object to the base class.
embedded document. You cannot assign the base class reference to a sub class
reference. Therefore, it is necessary to convert from one type into
other.
Java supports the following types of type casting:
Microsoft Word
Type casting primitive data types
Document
Type casting objects

Ver 1.0 Slide 7 of 15 Ver 1.0 Slide 8 of 15

2
4/19/2021

Programming in Java Programming in Java

Type Casting Primitive Data Types Type Casting Primitive Data Types (Contd.)
A primitive data type supports two types of type casting: Consider the following code snippet that casts the integer number
Implicit casting to the byte type:
Explicit casting int a = 10;
Implicit casting: byte b = (byte) a;
Refers to an automatic conversion of one data type into another. System.out.println(b);
Occurs if both the data types are compatible with each other.
Consider the following code snippet of the implicit casting:
int a = 100;
long b = a;
Explicit casting occurs when one data type cannot be implicitly
converted into another data type.
In case of explicit conversion, you must convert the data type into
the compatible type.

Ver 1.0 Slide 9 of 15 Ver 1.0 Slide 10 of 15

Programming in Java Programming in Java

Type Casting Object Type Casting Object (Contd.)


The casting of object references depends on the relationship of the For example, the Manager and Supervisor classes extend the
classes involved in the same hierarchy. Employee class, as shown in the following figure.
Any object reference can be assigned to a reference variable of the
type, Object.
An object supports following types of casting in Java:
Upcasting
Downcasting A Manager class object can be treated as if it were an Employee
Upcasting is usually done along the class hierarchy in a direction object, as shown in following code snippet:
from the derived class to the base class. Employee emp1 = new Employee();
Employee emp2 = new Employee();
Manager mgr = new Manager();
Supervisor spr = new Supervisor();
emp1 = mgr;
emp2=spr;

Ver 1.0 Slide 11 of 15 Ver 1.0 Slide 12 of 15

3
4/19/2021

Programming in Java Programming in Java

Type Casting Object (Contd.) Just a minute


Downcasting is usually done along the class hierarchy in a direction Which one of the following casts allows automatic conversion from
from the base class towards the derived classes, as shown in the one data type into another?
following figure. Upcasting
Downcasting
Implicit casting
Explicit casting

Consider the code given in the embedded document to implement


downcasting:

Microsoft Word
Document

Ver 1.0 Slide 13 of 15 Ver 1.0 Slide 14 of 15

Programming in Java

Just a minute (Contd.)


Solution:
Implicit casting

Ver 1.0 Slide 15 of 15

You might also like