1. It can be said that events, which are the centerpieces of event-driven progra
ID: 666262 • Letter: 1
Question
1. It can be said that events, which are the centerpieces of event-driven programming, have 3 stages in their life-cycle:
-Registering the Event
-Listening for the Event
-Event handling
How are these three stages represented or implemented in Java?
2. How an interface class differs from a concrete class, how interfaces are defined, and what does implementing an interface class mean?
Explanation / Answer
Answer:
i) Registering the Event:
To register the event a class has to be defined that is inherited from existing event class or interface.
Syntax : class <name> extends/implements <Eventclass>/<Interface
Eg: class Myevent implements ActionListener //or
class Myevent extends ActionEvent
Listening for the event: Once registered by inheritance, make listening by the following syntax
Syn: add<Event>(<EventClass>);
Eg: addActionListener(this); //implementing class is current class
iii) Event Handling: Implementing of method defined/overrided by superclass/interface
Eg: public void actionPerformed(ActionEvent ae)
{......}
2. How an interface class differs from a concrete class, how interfaces are defined, and what does implementing an interface class mean?
Answer:
i) Interface: Interface is a pure abstract class contains only signatures of methods. Defined instance variables are final by default.
Syntax:
interface <name>
{
<type> <attributes>=<constant values>;
<return type> <method>(<parameters>);
}
Eg:
interface shape
{
double PI=3.14;
public void area(); //method signature but not body
....
}
ii) Concrete Class:
A class which contains full definition of functions/methods is concrete class.
Syn:
class <name>
{
<type> <attributes>;
<return type> <funcname>(<parameters>)
{
....
....
}
}
iii) Implemention interface:
All declared functions in interface must be implemented by sub-class, which is inheriting. Use implements key word.
Syn: class <name> implements <interface>
{
//other codes
public <return type> <functionname>(<parameters>)
{
//implemented details
}
}
Eg: //implementing shape interface of above
class Circle implements Shape
{
public void area()
{
//take radius r as input
System.out.println(“Area of circle is : “+PI*r*r);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.