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

Object Oriented Programming Using Java

Uploaded by

r.p.sujan838
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Object Oriented Programming Using Java

Uploaded by

r.p.sujan838
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

www.ChaloExam.

com

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page | 1

UNIT- I

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page | 2

Basics of Java programming

Java is a programming language and a platform. Java is a high level, robust,


object-oriented and secure programming language.

Java was developed by Sun Microsystems (which is now the subsidiary of Oracle)
in the year 1995. James Gosling is known as the father of Java. Before Java, its
name was Oak. Since Oak was already a registered company, James Gosling and
his team changed the name from Oak to Java.

java Example

Let's have a quick look at Java programming examples. A detailed description of


the Hello Java example is available on the next page.

Simple.java

1. class Simple{

2. public static void main(String args[]){

3. System.out.println("Hello Java");

4. }

5. }

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page | 3

Application

According to Sun, 3 billion devices run Java. There are many devices where Java
is currently used. Some of them are as follows:

1. Desktop Applications such as acrobat reader, media player, antivirus, etc.

2. Web Applications such as irctc.co.in, javatpoint.com, etc.

3. Enterprise Applications such as banking applications.

4. Mobile

5. Embedded System

6. Smart Card

7. Robotics

8. Games, etc.

Types of Java Applications

There are mainly 4 types of applications that can be created using Java
programming:

1 Standalone Application

Standalone applications are also known as desktop applications or window-based


applications. These are traditional software that we need to install on every

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |1

UNIT- II

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |2

INHERITANCE AND POLYMORPHISM

Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object. It is an important part of OOPs

(Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of the parent class. Moreover, you can add new methods and
fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child


relationship.

Terms used in Inheritance

Class: A class is a group of objects which have common properties. It is a


template or blueprint from which objects are created.

Sub Class/Child Class: Subclass is a class which inherits the other class. It
is also called a derived class, extended class, or child class.

Super Class/Parent Class: Superclass is the class from where a subclass


inherits the features. It is also called a base class or a parent class.

Reusability: As the name specifies, reusability is a mechanism which


facilitates you to reuse the fields and methods of the existing class when

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |3

you create a new class. You can use the same fields and methods already
defined in the previous class.

The syntax of Java Inheritance

1. class Subclass-name extends Superclass-name

2. {

3. //methods and fields

4. }

The extends keyword indicates that you are making a new class that derives from
an existing class. The meaning of "extends" is to increase the functionality.

Exception Handling in Java

In the terminology of Java, a class which is inherited is called a parent or


superclass, and the new class is called child or subclass.

Java Inheritance Example

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |1

UNIT-III

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |2

EVENT AND GUI PROGRAMMING

Event Handling

Event and Listener (Java Event Handling)

Changing the state of an object is known as an event. For example, click on


button, dragging mouse etc. The java.awt.event package provides many event
classes and Listener interfaces for event handling.

Types of Event

The events can be broadly classified into two categories:

Foreground Events - Those events which require the direct interaction of


user.They are generated as consequences of a person interacting with the
graphical components in Graphical User Interface. For example, clicking on a
button, moving the mouse, entering a character through keyboard,selecting an
item from list, scrolling the page etc.

Background Events - Those events that require the interaction of end user are
known as background events. Operating system interrupts, hardware or software
failure, timer expires, an operation completion are the example of background
events.

Event Handling

Event Handling is the mechanism that controls the event and decides what should
happen if an event occurs. This mechanism have the code which is known as event
handler that is executed when an event occurs. Java Uses the Delegation Event

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |3

Model to handle the events. This model defines the standard mechanism to
generate and handle the events.Let's have a brief introduction to this model.

The Delegation Event Model has the following key participants namely:

Source - The source is an object on which event occurs. Source is responsible for
providing information of the occurred event to it's handler. Java provide as with
classes for source object.

Listener - It is also known as event handler. Listener is responsible for generating


response to an event. From java implementation point of view the listener is also
an object. Listener waits until it receives an event. Once the event is received , the
listener process the event an then returns.

Important Event Classes and Interface

Event Classes Description Listener Interface

ActionEvent generated when button is ActionListener


pressed, menu-item is selected,
list-item is double clicked

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |4

MouseEvent generated when mouse is MouseListener


dragged, moved,clicked,pressed
or released and also when it
enters or exit a component

KeyEvent generated when input is KeyListener


received from keyboard

ItemEvent generated when check-box or ItemListener


list item is clicked

TextEvent generated when value of TextListener


textarea or textfield is changed

MouseWheelEvent generated when mouse wheel is MouseWheelListener


moved

WindowEvent generated when window is WindowListener


activated, deactivated,
deiconified, iconified, opened or
closed

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |1

UNIT - IV

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |2

Multithreading in Java

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.

Advantages of Java Multithreading

1. It doesn't block the user because threads are independent and you can
perform multiple operations at the same time.

2. You can perform many operations together, so it saves time.

3. Threads are independent, so it doesn't affect other threads if an exception


occurs in a single thread.

Multitasking

Multitasking is a process of executing multiple tasks simultaneously. We use


multitasking to utilize the CPU.

Multitasking can be achieved in two ways:

Process-based Multitasking (Multiprocessing)

Thread-based Multitasking (Multithreading)

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Page |3

1. Process-based Multitasking (Multiprocessing)

Each process has an address in memory. In other words, each process


allocates a separate memory area.

A process is heavyweight.

Cost of communication between the process is high.

Switching from one process to another requires some time for saving
and loading registers, memory maps, updating lists, etc.

2. Thread-based Multitasking (Multithreading)

Threads share the same address space.

A thread is lightweight.

Cost of communication between the thread is low.

Note: At least one process is required for each thread.

What is Thread in java

A thread is a lightweight subprocess, the smallest unit of processing. It is a


separate path of execution.

Threads are independent. If there occurs exception in one thread, it doesn't affect
other threads. It uses a shared memory area.

Follow us on Instagram: @ChaloExam


www.ChaloExam.com

Follow us on Instagram: @ChaloExam

You might also like