Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Core Java

Uploaded by

usghormare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Core Java

Uploaded by

usghormare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Method Overriding in Java 2) Java package provides access protec on.

If subclass (child class) has the same method as 3) Java package removes naming collision.
declared in the parent class, it is known
What are Java Excep ons?
as method overriding in Java.
In Java, Excep on is an unwanted or
In other words, If a subclass provides the
unexpected event, which occurs during the
specific implementa on of the method that has
execu on of a program, i.e. at run me, that
been declared by one of its parent class, it is
disrupts the normal flow of the program’s
known as method overriding.
instruc ons. Excep ons can be caught and
Usage of Java Method Overriding handled by the program. When an excep on
occurs within a method, it creates an object.
o Method overriding is used to provide This object is called the excep on object. It
the specific implementa on of a contains informa on about the excep on, such
method which is already provided by its as the name and descrip on of the excep on
superclass. and the state of the program when the
o Method overriding is used for run me excep on occurred.
polymorphism Major reasons why an excep on Occurs
Rules for Java Method Overriding  Invalid user input
1. The method must have the same name  Device failure
as in the parent class
 Loss of network connec on
2. The method must have the same
parameter as in the parent class.  Physical limita ons (out-of-disk
memory)
3. There must be an IS-A rela onship
(inheritance).  Code errors

Java Packages  Out of bound

A java package is a group of similar types of  Null reference


classes, interfaces and sub-packages.  Type mismatch
Package in java can be categorized in two form,  Opening an unavailable file
built-in package and user-defined package.
 Database errors
There are many built-in packages such as java,
lang, awt, javax, swing, net, io, u l, sql etc.  Arithme c errors

Here, we will have the detailed learning of


crea ng and using user-defined packages.

Advantage of Java Package

1) Java package is used to categorize the classes


and interfaces so that they can be easily
maintained.
 Graphics Class Java Applet

 In Java, the Graphics class is an abstract  Applet is a special type of program that
class that provides methods for drawing is embedded in the webpage to
shapes, text, and images on a generate the dynamic content. It runs
component. It is part of the Abstract inside the browser and works at client
Window Toolkit (AWT) package. side.

 Here's a breakdown of its key aspects:  Advantage of Applet

 Abstract Class:  There are many advantages of applet.


They are as follows:
 Graphics is an abstract class, meaning
you cannot directly create instances of  It works at client side so less response
it. Instead, you obtain a Graphics object me.
through various means, such as:
 Secured
 Overriding
 It can be executed by browsers running
the paintComponent() method in a
under many plateforms, including Linux,
Swing component like JPanel.
Windows, Mac Os etc.
 Using the getGraphics() method of a
 Drawback of Applet
component.
 Plugin is required at client browser to
 Drawing Methods:
execute applet.
 Graphics provides methods for drawing
various shapes, including:

 drawLine(): Draws a line between two


points.

 drawRect(): Draws a rectangle.

 drawOval(): Draws an oval.

 drawString(): Draws a string of text.

 drawImage(): Draws an image.


 Input Output Java What is token in Java?
 Java provides a rich set of classes and The Java compiler breaks the line of code into
interfaces in the java.io package to text (words) is called Java tokens. These are the
handle input and output smallest element of the Java program. The Java
opera ons. Here's a short note on compiler iden fied these words as tokens.
managing I/O in Java: These tokens are separated by the delimiters. It
is useful for compilers to detect errors.
 Streams:
Remember that the delimiters are not part of
 Founda on of Java I/O: Data is handled the Java tokens.
as a sequence of bytes or characters
Types of Tokens
known as a stream.
Java token includes the following:
 Types:
o Keywords
 Byte Streams: Used for reading and
wri ng raw binary data (e.g., images, o Iden fiers
audio).
o Literals
 Character Streams: Used for reading
o Operators
and wri ng text data, handling
character encoding automa cally. o Separators
 Direc on: o Comments
 Input Streams: Read data from a source Data Types in Java
(e.g., file, keyboard).
Data types specify the different sizes and values
 Output Streams: Write data to a that can be stored in the variable. There are two
des na on (e.g., file, console). types of data types in Java:
 Common Classes: 1. Primi ve data types: The primi ve data
 InputStream: Base class for all input types include boolean, char, byte, short,
byte streams. int, long, float and double.

 OutputStream: Base class for all output 2. Non-primi ve data types: The non-
byte streams. primi ve data types
include Classes, Interfaces, and Arrays.
 Reader: Base class for all input
character streams. 3. There are 8 types of primi ve da ypes:

 Writer: Base class for all output 4. boolean data type


character streams. 5. byte data type long data type
 FileInputStream/FileOutputStream: Re 6. char data type Float data type
ad/write data from/to files.
7. short data type double data type
 FileReader/FileWriter: Read/write text
data from/to files. 8. int data type
9.

You might also like