java
1. Overloading by changing the number of parameters
2. Method Overloading by changing the data type of parameters
Let’s look at a real-world example:
• Important Points
• Two or more methods can have the same name inside the same
class if they accept different arguments. This feature is known as
method overloading.
• Method overloading is achieved by either:
– changing the number of arguments.
– or changing the data type of arguments.
• It is not method overloading if we only change the return type of
methods. There must be differences in the number of parameters.
Method Binding in Java
First let's look at two definitions from the book:
• Connecting a method call to a method body is
called binding.
• All method binding in Java uses late binding
unless the method is static or final.
• Static Binding and Dynamic Binding
 Connecting a method call to the method body
is known as binding.
 There are two types of binding
 Static Binding (also known as Early Binding).
 Dynamic Binding (also known as Late Binding).
Example: Static Binding
Example: Dynamic Binding
UNIT-3
Types of Exception in Java
Examples of Built-in Exception
• ClassCastException
• // Java Program to illustrate
• // ClassCastException
• class Test {
• public static void main(String[] args)
• {
• String s = new String("Geeks");
• Object o = (Object)s;
• Object o1 = new Object();
• String s1 = (String)o1;
• }
• }
A. Arithmetic exception
• // Java program to demonstrate ArithmeticException
• class ArithmeticException_Demo
• {
• public static void main(String args[])
• {
• try {
• int a = 30, b = 0;
• int c = a/b; // cannot divide by zero
• System.out.println ("Result = " + c);
• }
• catch(ArithmeticException e) {
• System.out.println ("Can't divide a number by 0");
• }
• }
• }
B. NullPointer Exception
C. StringIndexOutOfBound Exception
• // Java program to demonstrate StringIndexOutOfBoundsException
• class StringIndexOutOfBound_Demo
• {
• public static void main(String args[])
• {
• try {
• String a = "This is like chipping "; // length is 22
• char c = a.charAt(24); // accessing 25th element
• System.out.println(c);
• }
• catch(StringIndexOutOfBoundsException e) {
• System.out.println("StringIndexOutOfBoundsException");
• }
• }
• }
D. FileNotFound Exception
E. NumberFormat Exception
F. ArrayIndexOutOfBounds Exception
• // Java program to demonstrate ArrayIndexOutOfBoundException
• class ArrayIndexOutOfBound_Demo
• {
• public static void main(String args[])
• {
• try{
• int a[] = new int[5];
• a[6] = 9; // accessing 7th element in an array of
• // size 5
• }
• catch(ArrayIndexOutOfBoundsException e){
• System.out.println ("Array Index is Out Of Bounds");
• }
• }
• }
• Multithreading in Java
• is a process of executing multiple threads simultaneously.
• A thread is a lightweight sub-process, the smallest unit of
processing. Multiprocessing and multithreading, both are used to
achieve multitasking.
• However, we use multithreading than multiprocessing because
threads use a shared memory area. They don't allocate separate
memory area so saves memory, and context-switching between the
threads takes less time than process.
• Java Multithreading is mostly used in games, animation, etc.
Thread Synchronization
Interthread Communication
Enumerations: Enum is short for "enumerations", which means
"specifically listed".
Example program
enum Level {
LOW,
MEDIUM,
HIGH
}
public class Main {
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
System.out.println(myVar);
}
}
Output:MEDIUM
Enum inside a Class
public class Main {
enum Level {
LOW,
MEDIUM,
HIGH
}
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
System.out.println(myVar);
}
}
Output:MEDIUM
Enum in a Switch Statement
Autoboxing and Unboxing:
The automatic conversion of primitive data types into its equivalent
Wrapper type is known as boxing and opposite operation is known
as unboxing. This is the new feature of Java5. So java programmer
doesn't need to write the conversion code.
Advantage of Autoboxing and Unboxing:
No need of conversion between primitives and Wrappers manually so less
coding is required.
Output:50 5
Java Annotations
• Java annotations are metadata (data about data) for our program source
code.
• They provide additional information about the program to the compiler but
are not part of the program itself. These annotations do not affect the
execution of the compiled program.
• Let's take an example of @Override annotation.
• The @Override annotation specifies that the method that has been
marked with this annotation overrides the method of the superclass with
the same method name, return type, and parameter list.
• It is not mandatory to use @Override when overriding a method. However,
if we use it, the compiler gives an error if something is wrong (such as
wrong parameter type) while overriding the method.
Java MouseListener Interface
kk
Java Abstract Window Toolkit(AWT)
• java AWT is an API that contains large number of classes and methods to
create and manage graphical user interface ( GUI ) applications. The AWT
was designed to provide a common set of tools for GUI design that could
work on a variety of platforms. The tools provided by the AWT are
implemented using each platform's native GUI toolkit, hence preserving the
look and feel of each platform. This is an advantage of using AWT. But the
disadvantage of such an approach is that GUI designed on one platform
may look different when displayed on another platform that means AWT
component are platform dependent.
• AWT is the foundation upon which Swing is made i.e Swing is a improved
GUI API that extends the AWT. But now a days AWT is merely used
because most GUI Java programs are implemented using Swing because of
its rich implementation of GUI controls and light-weighted nature.
Component class
• Component class is at the top of AWT hierarchy. It is an abstract class that encapsulates all the attributes
of visual component. A component object is responsible for remembering the current foreground and
background colors and the currently selected text font.
Container
• Container is a component in AWT that contains another component like button, text field, tables
etc. Container is a subclass of component class. Container class keeps track of components that are added
to another component.
Panel
• Panel class is a concrete subclass of Container. Panel does not contain title bar, menu bar or border. It is
container that is used for holding components.
Window class
• Window class creates a top level window. Window does not have borders and menubar.
Frame
• Frame is a subclass of Window and have resizing canvas. It is a container that contain several different
components like button, title bar, textfield, label etc. In Java, most of the AWT applications are created
using Frame window. Frame class has two different constructors,
AWT Button
AWT Label
AWT TextField
AWT TextArea
Example:
In this example, we are creating checkbox that are used to get user input. If checkbox
is checked it returns true else returns false.
AWT Checkbox
Example:
In this example, we are creating drop-down menu that is used to get user
choice from multiple choices.
AWT Choice
AWT List
AWT Canvas
Java applets
What is Applet?
An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works
at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web
server.
Applets are used to make the website more dynamic and entertaining.
Important points :
• All applets are sub-classes (either directly or indirectly)
of java.applet.Applet class.
• Applets are not stand-alone programs. Instead, they run within
either a web browser or an applet viewer. JDK provides a standard
applet viewer tool called applet viewer.
• In general, execution of an applet does not begin at main() method.
• Output of an applet window is not performed
by System.out.println(). Rather it is handled with various AWT
methods, such as drawString().
9/12/2022 Distributed Computing, M. L. Liu 194
Summary
• An applet is a Java class
• Its code is downloaded from a web server
• It is run in the browser’s environment on the client host
• It is invoked by a browser when it scans a web page and encounters a class specified with the
APPLET tag
• For security reason, the execution of an applet is normally subject to restrictions:
– applets cannot access files in the file system on the client host
– Applets cannot make network connection exception to the server host from which it originated
java.pptx
java.pptx
java.pptx
java.pptx
java.pptx

java.pptx

  • 1.
  • 2.
    1. Overloading bychanging the number of parameters
  • 3.
    2. Method Overloadingby changing the data type of parameters
  • 4.
    Let’s look ata real-world example:
  • 5.
    • Important Points •Two or more methods can have the same name inside the same class if they accept different arguments. This feature is known as method overloading. • Method overloading is achieved by either: – changing the number of arguments. – or changing the data type of arguments. • It is not method overloading if we only change the return type of methods. There must be differences in the number of parameters.
  • 6.
    Method Binding inJava First let's look at two definitions from the book: • Connecting a method call to a method body is called binding. • All method binding in Java uses late binding unless the method is static or final.
  • 7.
    • Static Bindingand Dynamic Binding  Connecting a method call to the method body is known as binding.  There are two types of binding  Static Binding (also known as Early Binding).  Dynamic Binding (also known as Late Binding).
  • 10.
  • 11.
  • 68.
  • 86.
  • 88.
    Examples of Built-inException • ClassCastException • // Java Program to illustrate • // ClassCastException • class Test { • public static void main(String[] args) • { • String s = new String("Geeks"); • Object o = (Object)s; • Object o1 = new Object(); • String s1 = (String)o1; • } • }
  • 89.
    A. Arithmetic exception •// Java program to demonstrate ArithmeticException • class ArithmeticException_Demo • { • public static void main(String args[]) • { • try { • int a = 30, b = 0; • int c = a/b; // cannot divide by zero • System.out.println ("Result = " + c); • } • catch(ArithmeticException e) { • System.out.println ("Can't divide a number by 0"); • } • } • }
  • 90.
  • 91.
    C. StringIndexOutOfBound Exception •// Java program to demonstrate StringIndexOutOfBoundsException • class StringIndexOutOfBound_Demo • { • public static void main(String args[]) • { • try { • String a = "This is like chipping "; // length is 22 • char c = a.charAt(24); // accessing 25th element • System.out.println(c); • } • catch(StringIndexOutOfBoundsException e) { • System.out.println("StringIndexOutOfBoundsException"); • } • } • }
  • 92.
  • 93.
  • 94.
    F. ArrayIndexOutOfBounds Exception •// Java program to demonstrate ArrayIndexOutOfBoundException • class ArrayIndexOutOfBound_Demo • { • public static void main(String args[]) • { • try{ • int a[] = new int[5]; • a[6] = 9; // accessing 7th element in an array of • // size 5 • } • catch(ArrayIndexOutOfBoundsException e){ • System.out.println ("Array Index is Out Of Bounds"); • } • } • }
  • 100.
    • Multithreading inJava • is a process of executing multiple threads simultaneously. • A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. • However, we use multithreading than multiprocessing because threads use a shared memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. • Java Multithreading is mostly used in games, animation, etc.
  • 119.
  • 121.
  • 125.
    Enumerations: Enum isshort for "enumerations", which means "specifically listed".
  • 126.
    Example program enum Level{ LOW, MEDIUM, HIGH } public class Main { public static void main(String[] args) { Level myVar = Level.MEDIUM; System.out.println(myVar); } } Output:MEDIUM
  • 127.
    Enum inside aClass public class Main { enum Level { LOW, MEDIUM, HIGH } public static void main(String[] args) { Level myVar = Level.MEDIUM; System.out.println(myVar); } } Output:MEDIUM
  • 128.
    Enum in aSwitch Statement
  • 130.
    Autoboxing and Unboxing: Theautomatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code. Advantage of Autoboxing and Unboxing: No need of conversion between primitives and Wrappers manually so less coding is required.
  • 131.
  • 133.
    Java Annotations • Javaannotations are metadata (data about data) for our program source code. • They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. • Let's take an example of @Override annotation. • The @Override annotation specifies that the method that has been marked with this annotation overrides the method of the superclass with the same method name, return type, and parameter list. • It is not mandatory to use @Override when overriding a method. However, if we use it, the compiler gives an error if something is wrong (such as wrong parameter type) while overriding the method.
  • 145.
  • 148.
  • 151.
    Java Abstract WindowToolkit(AWT) • java AWT is an API that contains large number of classes and methods to create and manage graphical user interface ( GUI ) applications. The AWT was designed to provide a common set of tools for GUI design that could work on a variety of platforms. The tools provided by the AWT are implemented using each platform's native GUI toolkit, hence preserving the look and feel of each platform. This is an advantage of using AWT. But the disadvantage of such an approach is that GUI designed on one platform may look different when displayed on another platform that means AWT component are platform dependent. • AWT is the foundation upon which Swing is made i.e Swing is a improved GUI API that extends the AWT. But now a days AWT is merely used because most GUI Java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature.
  • 153.
    Component class • Componentclass is at the top of AWT hierarchy. It is an abstract class that encapsulates all the attributes of visual component. A component object is responsible for remembering the current foreground and background colors and the currently selected text font. Container • Container is a component in AWT that contains another component like button, text field, tables etc. Container is a subclass of component class. Container class keeps track of components that are added to another component. Panel • Panel class is a concrete subclass of Container. Panel does not contain title bar, menu bar or border. It is container that is used for holding components. Window class • Window class creates a top level window. Window does not have borders and menubar. Frame • Frame is a subclass of Window and have resizing canvas. It is a container that contain several different components like button, title bar, textfield, label etc. In Java, most of the AWT applications are created using Frame window. Frame class has two different constructors,
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
    Example: In this example,we are creating checkbox that are used to get user input. If checkbox is checked it returns true else returns false. AWT Checkbox
  • 162.
    Example: In this example,we are creating drop-down menu that is used to get user choice from multiple choices. AWT Choice
  • 163.
  • 164.
  • 182.
  • 183.
    What is Applet? Anapplet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server. Applets are used to make the website more dynamic and entertaining. Important points : • All applets are sub-classes (either directly or indirectly) of java.applet.Applet class. • Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer. • In general, execution of an applet does not begin at main() method. • Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().
  • 194.
    9/12/2022 Distributed Computing,M. L. Liu 194 Summary • An applet is a Java class • Its code is downloaded from a web server • It is run in the browser’s environment on the client host • It is invoked by a browser when it scans a web page and encounters a class specified with the APPLET tag • For security reason, the execution of an applet is normally subject to restrictions: – applets cannot access files in the file system on the client host – Applets cannot make network connection exception to the server host from which it originated