KEMBAR78
VB Lecture 5 | PDF | Class (Computer Programming) | Anonymous Function
0% found this document useful (0 votes)
29 views65 pages

VB Lecture 5

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, focusing on anonymous inner classes and Java lambda expressions. It explains the use cases, advantages, and limitations of anonymous inner classes, as well as the syntax and application of lambda expressions in Java. Additionally, it covers event-driven programming concepts, including various event types and listener interfaces in Java Swing, along with practical examples and questions for coding exercises.

Uploaded by

ayabahaa
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)
29 views65 pages

VB Lecture 5

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, focusing on anonymous inner classes and Java lambda expressions. It explains the use cases, advantages, and limitations of anonymous inner classes, as well as the syntax and application of lambda expressions in Java. Additionally, it covers event-driven programming concepts, including various event types and listener interfaces in Java Swing, along with practical examples and questions for coding exercises.

Uploaded by

ayabahaa
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/ 65

Lecture [5]

Dr. Gehad Ismail Sayed


Recap – OOP Concepts
What is anonymous inner class?
⚫ An anonymous inner class in Java is an inner class
which is declared without any class name at all.
⚫ In simple words, a nameless inner class is called
anonymous inner class.
⚫ It is used in listener interfaces in graphic programming.
Recap – OOP Concepts
Anonymous inner class
⚫ Anonymous classes enable you to make your
code more concise.
⚫ They enable you to declare and instantiate a
class at the same time.
⚫ They are like local classes except that they
don’t have a name.
⚫ We can use them if we need to use a local
class only once.
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
1. Anonymous inner class that extends a class
(may be abstract or concrete)
2. Anonymous inner class that implements an
interface
3. Anonymous inner class that defined as
arguments of method or constructor.
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
1. Anonymous inner class that extends a class
(may be abstract or concrete)
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
1. Anonymous inner class that extends a class
(may be abstract or concrete)
Inner anonymous
class
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
1. Anonymous inner class that extends a class
(may be abstract or concrete)
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
1. Anonymous inner class that extends a class
(may be abstract or concrete)
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
1. Anonymous inner class that extends a class
(may be abstract or concrete)
If we want to use rewardMethod() just once, we can do this
as well
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
2. Anonymous inner class that implements an
interface

Note: we can declare


interface class but we
cannot instantiate
from it
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
2. Anonymous inner class that implements an
interface

Inner anonymous
class
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
2. Anonymous inner class that implements an
interface
If we want to use print() just once, we can do this as well

Java Lambda
Expression
Recap – OOP Concepts
Java Lambda Expressions:
⚫ A lambda expression is a short block of code which
takes in parameters and returns a value.
⚫ Lambda expressions are similar to methods, but they do
not need a name and they can be implemented right in
the body of a method.
⚫ Lambda can be used for only interface class which has
only a single method
⚫ Syntax:
⚫ parameter -> expression
⚫ (parameter1, parameter2) ->
{ code block }
Recap – OOP Concepts
Anonymous classes can be used in 3 ways:
3. Anonymous inner class that defined as arguments of
method or constructor.

Inner anonymous
class
Recap – OOP Concepts
Anonymous Inner class
⚫ Does the anonymous inner class have a constructor? No
Recap – OOP Concepts
Anonymous Inner class
⚫ Can we use the constructor of the super class in the
anonymous inner class? Yes
Recap – OOP Concepts
Anonymous Inner class
⚫ Can we declare extra methods in anonymous inner
class?
Yes
Recap – OOP Concepts
Anonymous Inner class
⚫ Can we declare extra fields/attributes in anonymous
inner class?
Yes
Recap – OOP Concepts
Anonymous Inner class
⚫ Can we update a local variable in anonymous inner
class?
No
Recap – OOP Concepts
Anonymous Inner class
⚫ Can we call the extra methods in anonymous inner
class?
No
Recap – OOP Concepts
Anonymous Inner class
⚫ Can we call the extra methods in anonymous inner
class?
No
Solution: Either add this method inside the super class
or use var keyword
Recap – OOP Concepts
Var keyword in java
⚫ Type inference is used in var keyword in which it
detects automatically the datatype of a variable based on
the surrounding context.

class Demo1 {
public static void main(String[] args){
var x = 100; // int
var y = 1.90; // double
var z = 'a’; // char
var p = "tanu"; // string
var q = false; // boolean}
}
Recap – OOP Concepts
Var keyword in java
⚫ var cannot be used as a Generic type.

class Demo1 {
public static void main(String[] args){
var<var> al = new ArrayList<>(); // Generic list using var
al.add(10);
al.add(20);
al.add(30);
System.out.println(al);}
}
Recap – OOP Concepts
Var keyword in java
⚫ var cannot be used in an instance and global variable
declaration

class Demo3 {
var x = 50;
public static void main(String[] args)
{
System.out.println(x);
}
}
Recap – OOP Concepts
Var keyword in java
⚫ var cannot be used with the generic type.

class Demo1 {
public static void main(String[] args){
var<Integer> al = new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
System.out.println(al);}
}
Recap – OOP Concepts
Var keyword in java
⚫ var cannot be used without explicit initialization.

class Demo1 {
public static void main(String[] args){
var variable;
var variable = null;
}
}
Recap – OOP Concepts
Var keyword in java
⚫ var cannot be used with Lambda Expression.

import java.util.*;
interface myInt { int add(int a, int b); }
class Demo7 {
public static void main(String[] args)
{
var obj = (a, b) -> (a + b);
System.out.println(obj.add(2, 3));
}
}
Recap – OOP Concepts
Var keyword in java
⚫ var cannot be used for method parameters and return
type.
class Demo8 {
var method1() { return ("Inside Method1"); }
void method2(var a) { System.out.println(a); }
public static void main(String[] args)
{
Demo1 obj = new Demo1();
var res = obj.method1();
obj.method2();
}
}
Recap – OOP Concepts
Anonymous class in Swing
Recap – OOP Concepts
Anonymous class in Swing

Note: initializer
block
Event Driven Programming
⚫ Action Event
⚫ Window Event
⚫ Mouse Event
⚫ Key Event
⚫ Focus Event
⚫ Item Event
Swing - KeyEvent Class
⚫ The Java KeyListener is notified whenever
you change the state of key. It is notified
against KeyEvent.
Methods of KeyEvent Class
Method Description
char getKeyChar() Obtains the Unicode character associated with this
event. Only rely on this value for key-typed events.
int getKeyCode() Obtains the key code associated with this event. The
key code identifies the particular key on the keyboard
that the user pressed or released. The KeyEvent
class defines many key code constants for
commonly seen keys. For example, VK_A specifies
the key labeled A, and VK_ESCAPE specifies the
Escape key.
boolean isActionKey() Returns true if the key firing the event is an action
key. Examples of action keys include Cut, Copy,
Paste, Page Up, Caps Lock, the arrow and function
keys. This information is valid only for key-pressed
and key-released events.
Methods of KeyListener
interface

Method Description
public abstract void keyPressed It is invoked when a key has been
(KeyEvent e); pressed. (when the key goes down)
public abstract void keyReleased It is invoked when a key has been
(KeyEvent e); released. (when the key comes up)
public abstract void keyTyped It is invoked when a key has been
(KeyEvent e); typed. (when the unicode character
represented by this key is sent by the
keyboard to system input.)
How to write KeyListener?
If you implement the KeyListener class, you need
to follow 3 steps:
1. Implement the KeyListener interface in the
class
2. Register the component with the Listener

3. Override the KeyListener methods


KeyListener - Example
KeyListener - Example
KeyListener - Questions
Question: write a java code that count number of character written by
the user and if the user print backspace the count number is
decreased. Hint: KeyEvent.VK_BACK_SPACE to get the code of
backspace char. Use the below template code.
KeyListener - Questions
Answer
Event Driven Programming
⚫ Action Event
⚫ Window Event
⚫ Mouse Event
⚫ Key Event
⚫ Focus Event
⚫ Item Event
Swing - FocusEvent Class
⚫ Focus events are fired whenever a component
gains or loses the keyboard focus.

⚫ FocusListener Interface has only two functions.


Methods of FocusListener
interface

Method Description
Void focusGained(FocusEvent) Invoked when a component gains the
keyboard focus.
Void focusLost(FocusEvent) Invoked when a component loses the
keyboard focus.
Methods of FocusEvent Class
Method Description
boolean isTemporary() Returns the true value if a focus-lost or
focus-gained event is temporary.
Component getComponent() Returns the component that fired the focus
event.
Component Returns the other component involved in the
getOppositeComponent() focus change. For a FOCUS_GAINED
event, this is the component that lost the
focus. For a FOCUS_LOST event, this is
the component that gained the focus. If the
focus change involves a native application,
a Java application in a different VM or
context, or no other component, then null is
returned.
How to write KeyListener?
If you implement the FocusListener class, you
need to follow 3 steps:
1. Implement the FocusListener interface in the
class
2. Register the component with the FcousListener

3. Override the FocusListener methods


FocusListener - Example
FocusListener - Question
Write a java code that changes the size of two buttons in a
focusGained mode to 120*20 and to 100*20 in focusLost
mode. Use the below code to write your code.
FocusListener - Question
Answer:
Event Driven Programming
⚫ Action Event
⚫ Window Event
⚫ Mouse Event
⚫ Key Event
⚫ Focus Event
⚫ Item Event
Swing - ItemEvent Class
⚫ Item events occur when a user selects a choice item, a
checkbox menu item or a list item.
⚫ Objects representing item events are created from the
ItemEvent class.
⚫ The corresponding listener interface for ItemEvent class
is ItemListener.
⚫ Each listener for ItemEvent should implement the
ItemListener interface.
Method Description
void itemStateChanged(ItemEvent e) It is called when the selected
item changes.
Methods of ItemEvent Class

Method Description
public Object getItem() Returns the item which was
clicked and triggered the
ItemEvent.
public ItemSelectable Returns the item which was
getItemSelectable() clicked.
public int getStateChange() Returns the current state of item
which was clicked.
How to write ItemListener?
If you implement the ItemListener class, you need
to follow 3 steps:
1. Implement the ItemListener interface in the
class
2. Register the component with the ItemListener

3. Override the ItemListener methods


ItemListener - Example
ItemListener - Example
ItemListener - Questions
Question: Write a java code that displays which item
selected by the user. Use CheckboxObj.getLabel() to return
the name of the selected item with indication either it is
checked or unchecked. Use the template below to write
your code.
ItemListener - Questions
Answer
ItemListener - Questions
Answer
Swing - JOptionPane Class
⚫ JOptionPane makes it easy to pop a standard
dialog box that prompts users for a value or
informs them of something.
⚫ It is used instead of system.in and system.out
⚫ JOptionPane class is part of the java.swing
library.
Swing - JOptionPane Class
There are three dialog boxes:
1. Message Dialog – Displays a message (left
figure)
2. Input Dialog – Prompt for some input. (middle
figure)
3. Confirm Dialog – Asks a confirming question
message (right figure)
JOptionPane Class - Example
MessageDialog - Example
InputDialog - Example
ConfirmDialog - Example
JOptionPane - Questions
Question: Write a java code that takes the user name from
the user and showed it to him a message dialog. Use the
template below to write your code.
JOptionPane - Questions
Question: Write a java code that takes the user name from
the user and showed it to him a message dialog. Use the
template below to write your code.

Answer
65

You might also like