Short Notes on OOP
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). A feature of objects is an object's procedures that can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OOP, computer programs are designed by making them out of objects that interact with one another. OOP languages are diverse, but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types.
Science 7 - LAND and SEA BREEZE and its Characteristics
OBJECT ORIENTED PROGRAMMING LANGUAGE - SHORT NOTES
1. OBJECT ORIENTEDPROGRAMING LANGUAGE
1.What is Multi threading
A process is a program in execution. Two or more processes running concurrently in a computer
is called multi tasking. In java it is term as multi threading.
Ex. While typing a document, we can take printout.
2. what is thread
A single sequential flow of control.
All programs have at least one thread-the main thread.
More than one thread can executing concurrently.
3. types of computer
There are two types of computers .They are,
1.Uni processor
One CPU
One memory unit
Simulated concurrency
2.Shared memory multiprocessor
Many CPUs
A single shared memory unit.
True concurrency
4.Advantages of multithreading
Reduces complexity of large programs.
Maximizes CPU utilization.
Increases the speed of execution.
5.Classes in thread
Java’s multi threading operation is based on the thread class. Thread is a class and it contains
constructors and methods for creating threads. Thread class is available in java.lang package. Thread
class contains different methods to control threads. They are,
1.Run( )
2.Start( )
3.Yield( )
4.Sleep( )
5.Stop( )
2. 6.Suspend( )
7.Resume( )
8.Notify( )
6.defining and running a thread
A thread class can be created by extending the thread class. The thread class is available in
java.lang package.the following steps are followed to define and run a thread.
1.Declare the class by extending thread class.
2.Over ride the run( ) method in extended class.
3.Create the thread object using the class declared.
4.Call the start( ) method to initiate thread execution.
7. Life cycle of a thread
There are five states in a lifetime of a thread. at a time, the thread may be in any one of the
following five states.they are,
1.New born state
2.Runnable state
3.Running state
4.Blocked state
5.Dead state
8. Thread priorities
The programmer can set the priorities for threads. This can be done using setpriority( ) method
of thread class. The syntax is,
Threadname.setpriority (value);
The value is an integer value. This integer value must be between 1 to 10. In addition the thread
class defines some predefined constants also.
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10
The default value is 5.
3. 9.Thread scheduling
Allocating CPU time for the threads in the program is called thread scheduling.
10.thread synchronization
The data and the methods that are common to many threads may commonly be placed outside
the thread.
In such situations more than one thread can try to access the same method and data at the same
time or a thread can try to access the method that is currently in use by another thread.
This problem can be overcome by using a technique called synchronization the general form is,
Synchronized return-type method-name
{
---------------------------
Statements;
--------------------------
}
11.what is applet
Applets are java programs thart are used for internet applications. it can be executed using java
enabled web browser or using appletviewer. It can perform arithmetic operations, display graphics, play
sounds, accept user input and create animation there two types of applet. they are,
1. Local applet
2. Remote applet
12.Local applet
An applet developed locally and stored in a local computer is known as local applet.there is no
need for internet connection to execute local applets.
13.Remote applet
Remote applets are developed by someone else stored in remote computer. Internet connection is
needed to execute remote applet. If our system is connected to the internet, we can download the remote
applet onto our system via the internet and run it.
4. 14.what is JAVA
Java is an object oriented language developed by James Gosling and coworkers at SUN
Microsystems.
Java began as a hardware independent language for powering small hand-held devices
such as complex remotes.
Java’s hardware independence made it ideal for use on the web and in 1994 the HotJava
web browser was introduced. It was the first demo of interactive applets.
In March 1995, it was announced that support for Java would be built into Netscape
Navigator. And thus began the rapid growth of Java.
15. what does mean by OBJECT ORIENTED
An object is an instance of a class.
A class is a programming construct that groups together data (properties) and functions
(methods) that can be performed on that data.
For example, consider representing a chair
Properties Methods
Height Create
Width Destroy
Weight Move-X
Color Move-Y
X-Position Paint
Y-Position
16. what is JAVA APPLET
Applets are Java programs that may be embedded into HTML documents. The applet
runs in the page as soon as it has been downloaded.
More specifically Java applets are Java programs that inherit the Applet (awt) or JApplet
class (swing).
5. 17. life cycle of an applet
The life cycle of an Applet consists of inherited functions that are called at various times by the
applet container (browser).
public void init()
{ // called when applet is first created }
public void start()
{ // called when applet is started and then restarted }
public void paint(Graphics G)
{ // invoked when screen needs to be drawn or redrawn }
public void stop()
{ //called when the user leaves the page containing the applet }
public void destroy()
{ // called when applet is permanently destroyed }
18.simple applet ideas
A calculator for a desired property
Body Mass Index or Bone Density for Allied Health
Unit Conversion or PV=nRT for Physics or Chemistry
Interest Calculations for Business and Management
Quadratic formula or Pythagorean Theorem for Math
A quote of the Day Generator
- create an array of strings (string [] quoteArray = new string[100];) holding the quotes and use a
random number generator to see which string gets passed to drawString(). The code to generate the
random number is:
Random r = new Random(seedValue);
index = r.nextInt() % quoteArray.length;
6. 19. How to applet adding HTML
Insert the following where you wish the applet to appear:
<applet code=“MyApplet.class” height=“200” width=“100”>
<param name=“” value=“”> </param>
<param name=“” value=“”> </param>
<param name=“” value=“”> </param>
</applet>
20.what is inheritance
Inheritance allows one to build a new class that builds onto the properties of an existing
class.
Models an “is a” relationship. For example, a faculty member is person who possesses some
additional properties such as rank or number of publications.
Inheritance keeps you from having to rewrite code. For example, Java applets are required
to have over 200 methods but inheritance takes care of most of them.
In Java inheritance is indicated by using the extends keyword.
21.what is swing
Swing is a collection of classes and interfaces that one can use to display GUI components or
inherit from to create new components.
22.what is JDBC
JDBC stands for Java Data Base Connectivity. JDBC is used for to develop applications using
database withot considering the nature of the database. It is available in the Application Programming
Interface(API).
When using JDBC, an application program requests the driver manager to establish a connection
to aspecfied data source. In response, the driver manager searches its list of registered drivers and selects
one appropriate type to the datasource.once the driver is selected, the application communicates directly
with it, sending requests for data and receiving the results.this os shown in the following fig.
7. JDBC API CLASSES
JAVAPROGRAM JDBC DRIVER DBMS
MANAGER
JDBC DRIVER
23. important classes in JDBC
The important classes and interface of JDBC are available in the package java.sql.they are,
Java.sql.driverManager
Java.sql.Connection
Java.sql.Statement
Java.sql.PreparedStatement
Java.sql.CallableStatement
Java.sql.ResultSet
Java.sql.SQLException
Java.sql.SQLWarning
24.what are the JDBC drivers
Drivers is an interface which is used to connect the JDBC with the database. There are four types
of driver available. They are,
1. JDBC-ODBC Bridge
2. Native –API-Parity-Java Driver
3. JDBC-Net-All-Java driver
4. Native-Protocol-All-Java driver
25.Steps for JDBC
There are seven steps involved while establishing connection with database. They are,
1. Importing the java.sql package
2. Loading and registering the driver
8. 3. Establishing the connection
4. Creating a statement
5. Executing the statement
6. Retrieving the result
7. Closing the connection and statement
26. Servlets
A servlet is like an applet, but on the server side.
A servlet is any class that implements the javax.servlet.Servlet interface
In practice, most servlets extend the javax.servlet.http.HttpServlet class
Some servlets extend javax.servlet.GenericServlet instead
Servlets, like applets, usually lack a main method, but must implement or override certain
other methods
Client sends a request to server
Server starts a servlet
Servlet computes a result for server and does not quit
Server returns response to client
Another client sends a request
Server calls the servlet again and etc.
27.Servers
A server is a computer that responds to requests from a client
Typical requests: provide a web page, upload or download a file, send email
A server is also the software that responds to these requests; a client could be the browser
or other software making these requests
Typically, your little computer is the client, and someone else’s big computer is the
server
However, any computer can be a server
It is not unusual to have server software and client software running on the same
computer.
servlet
9. 28.what is apache
Apache is a very popular server
66% of the web sites on the Internet use Apache
Apache is:
Full-featured and extensible
Efficient
Robust
Secure (at least, more secure than other servers)
Up to date with current standards
Open source
Free
29.what is port
A port is a connection between a server and a client
Ports are identified by positive integers
A port is a software notion, not a hardware notion, so there may be very many of them
A service is associated with a specific port
Typical port numbers:
21—FTP, File Transfer Protocol
22—SSH, Secure Shell
25—SMTP, Simple Mail Transfer Protocol
53—DNS, Domain Name Service
80—HTTP, Hypertext Transfer Protocol
8080—HTTP (used for testing HTTP)
7648, 7649—CU-SeeMe
27960—Quake III
These are the ports of
most interest to us
10. 30.what is CGI
CGI stands for “Common Gateway Interface
Client sends a request to server
Server starts a CGI script
Script computes a result for server and quits
Server returns response to client
Another client sends a request
Server starts the CGI script again
Etc.
31. what is TOMCAT
Tomcat is the Servlet Engine than handles servlet requests for Apache
Tomcat is a “helper application” for Apache
It’s best to think of Tomcat as a “servlet container”
Apache can handle many types of web services
Apache can be installed without Tomcat
Tomcat can be installed without Apache
It’s easier to install Tomcat standalone than as part of Apache
By itself, Tomcat can handle web pages, servlets, and JSP
Apache and Tomcat are open source (and therefore free).
32. introduction to Network programming
The Internet is all about connecting machines together. One of the most exciting
aspects of Java is that it incorporates an easy-to-use, cross-platform model for
network communications that makes it possible to learn network programming
without years of study. This opens up a whole new class of applications to
programmers.
33.what is socket
Sockets provide an interface for programming networks at the transport layer.
Network communication using Sockets is very much similar to performing file I/O
11. In fact, socket handle is treated like file handle.
The streams used in file I/O operation are also applicable to socket-based I/O
Socket-based communication is programming language independent.
That means, a socket program written in Java language can also communicate to a
program written in Java or non-Java socket program.
34.socket communication
A server (program) runs on a specific computer and has a socket that is bound to a specific port.
The server waits and listens to the socket for a client to make a connection request.
35.what are the java sockets
A socket is an endpoint of a two-way communication link between two programs running
on the network.
A socket is bound to a port number so that the TCP layer can identify the application that
data destined to be sent.
Java’s .net package provides two classes:
Socket – for implementing a client
36.How to implementing the Server
1. Open the Server Socket:
ServerSocket server;
DataOutputStream os;
DataInputStream is;
server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to the client
is = new DataInputStream( client.getInputStream() );
12. os = new DataOutputStream( client.getOutputStream() );
4. Perform communication with client
Receive from client: String line = is.readLine();
Send to client: os.writeBytes("Hellon");
5. Close sockets: client.close();
For multithreaded server:
while(true) {
i. wait for client requests (step 2 above)
ii. create a thread with “client” socket as parameter (the thread creates streams (as in step (3) and does
communication as stated in (4). Remove thread once service is provided.
}
37.what is internet address
Internet addresses are manipulated in Java by the use of the InetAddress class.
InetAddress takes care of the Domain Name System (DNS) look-up and reverse
look-up; IP addresses can be specified by either the host name or the raw IP
address. InetAddress provides methods to getByName(), getAllByName(),
getLocalHost(), getAddress(), etc.
38.what is IP address
IP addresses are a 32-bit number, often represented as a "quad" of four 8-bit
numbers separated by periods. They are organized into classes (A, B, C, D, andE) which are used to group
varying numbers of hosts in a hierarchical numbering
scheme.
Class A
1.0.0.0 to 126.255.255.255, inclusive. About 16 million IP addresses in a class A
domain.
Class B
128.1.0.0 to 191.254.255.255, inclusive. About 64 thousand IP addresses in a
class B domain.
Class C
192.0.1.0 to 223.255.254.255, inclusive. 256 IP addresses in a class C domain.
Class D
224.0.0.1 to 239.255.255.255, inclusive, denote multicast groups.
Class E
13. 240.0.0.0 to 254.255.255.255, inclusive. Reserved for future use.
The IP address 127.0.0.1 is special, and is reserved to represent the loopback or
"localhost" address.
39.what is port number
The port number field of an IP packet is specified as a 16-bit unsigned integer.
This means that valid port numbers range from 1 through 65535. (Port number 0
is reserved and can't be used). Java does not have any unsigned data types,Java's short data type is 16 bits,
but its range is -32768 to 32767 since it is a signed type.
40.what is client/server computing
You can use the Java language to communicate with remote file systems using a client/server
model. A server listens for connection requests from clients across the network or even from the same
machine. Clients know how to connect to the server via an IP address and port number. Upon connection,
the server reads the request sent by the client and responds appropriately. In this way, applications can be
broken down into specific tasks that are accomplished in separatelocations.
41.what is socket exception
try {
Socket client = new Socket(host, port); handleConnection(client);
}
catch(UnknownHostException uhe) { System.out.println("Unknown host: " + host); uhe.printStackTrace();
}
catch(IOException ioe) {
System.out.println("IOException: " + ioe); ioe.printStackTrace();
}
42.define serversocket & exception
public ServerSocket(int port) throws IOException
Creates a server socket on a specified port.
A port of 0 creates a socket on any free port. You can use getLocalPort() to identify the
(assigned) port on which this socket is listening.
The maximum queue length for incoming connection indications (a request to connect) is
set to 50. If a connection indication arrives when the queue is full, the connection is refused.
14. Throws:
IOException - if an I/O error occurs when opening the socket.
SecurityException - if a security manager exists and its checkListen method doesn't allow
the operation.
43.what is RMI
Remote method invocation allows applications to call object methods located remotely, sharing
resources and processing load across systems. Unlike other systems for remote execution which require that
only simple data types or defined structures be passed to and from methods, RMI allows any Java object type
to be used - even if the client or server has never encountered it before. RMI allows both client and server to
dynamically load new object types as required. In this article, you'll learn more about RMI.
44.Writing RMI services
Writing your own RMI services can be a little difficult at first, so we'll start off with an example
which isn't too ambitious. We'll create a service that can calculate the square of a number, and the power
of two numbers (238 for example). Due to the large size of the numbers, we'll use the java.math.BigInteger
class for returning values rather than an integer or a long.
45. Writing an interface
The first thing we need to do is to agree upon an interface, An interface is a description of the
methods we will allow remote clients to invoke. Let's consider exactly what we'll need.
1.A method that accepts as a parameter an integer, squares it, and returns a BigInteger public
BigInteger square ( int number_to_square );
2.A method that accepts as a parameter two integers, calculates their power, and returns a
BigInteger public BigInteger power ( int num1, int num2 );
46. implementimg the interface
Implementing the interface is a little more tricky - we actually have to write the square and
power methods! Don't worry if you're not sure how to calculate squares and powers, this isn't a math
lesson. The real code we need to be concerned about is the constructor and main method.
15. We have to declare a default constructor, even when we don't have any initialization code for our
service. This is because our default constructor can throw a java.rmi.RemoteException, from its parent
constructor in UnicastRemoteObject. Sound confusing? Don't worry, because our constructor is extremely
simple.
public PowerServiceServer () throws RemoteException
{
super();
}
47.what is RMI client
What good is a service, if you don't write a client that uses it? Writing clients is the easy part - all
a client has to do is call the registry to obtain a reference to the remote object, and call its methods. All the
underlying network communication is hidden from view, which makes RMI clients simple.
Our client must first assign a security manager, and then obtain a reference to the service. Note
that the client receives an instance of the interface we defined earlier, and not the actual implementation.
Some behind-the-scenes work is going on, but this is completely transparent to the client.
48. Running the client and server
More complex systems, however, might contain interfaces that change, or whose implementation
changes. To run this article's examples, both the client and server will have a copy of the classfiles, but more
advanced systems might share the code of the server on a webserver, for downloading as required. If your
systems do this, don't forget to set the system property java.rmi.server.codebase to the webserver directory in
which your classes are stored.
49.Perform the client and server
To perform the client and server, following the steps. They are,
1. Strat RMI registry
2. Compile the server
3. Start the server
4. Start the client
UNIT III
1) Introduction to JAVA:
Java is a pure object oriented general purpose language. Using java we can develop two types
of programs. They are:
i. Application programs
ii. Applet program
16. 2) Define Application programs and Applet programs:
Application programs are programs to do jobs on a local computer.
Applet programs are programs that have an ability to run on internet through a web browser
3) Difference between Application and Applet
APPLICATION APPLET
1) Application can access the local file
system and resources.
2) Functionality of the applications are
known.
3) Author is known.
4) Creating and running an application is
easy.
5) This is executed by typing commands
on command line.
1) Restricted to access the local file
system and resources.
2) Functionality of applets are not known.
3) Author is not known.
4) Creating and running an applet is
complex.
5) This is executed by using applet viewer
or any browser.
4) What are the features of java program?
The features of java program are:
1. Simple, small and familiar.
2. Object Oriented.
3. Distributed
4. Robust
5. Secure
6. Architectural neutral (or) platform independent
7. Portable
8. Compiled and interpreted
9. High performance
10. Dynamic and extensible.
5) Give the command used for creating, compiling and execution of a program.
17. Java is a command driven language. So the programs are compiled and executed by giving
commands in command prompt.
1) Creating a program:
Create a program using any text editor such as edit in DOS or notepad or word pad
etc. and save it in java directory.
Syntax:
filename.java
2) Compiling the program:
Compile the created program using java compiler.
Syntax:
javac sourcefilename.java
3) Running the program:
Run the compiled program using java interpreter.
Syntax:
java classname
EXAMPLE:
Name of the source file- Sample.java
Name of the class file- Sample.class
To run the program-c>java sample
6) What are Java tokens?
A token is an individual element in java. A program is written by using the available
tokens. The various tokens are:
1. Keywords
2. Identifies
3. Constants or literals
4. Operators
5. Separators
18. 7) What are keywords (or) define keyword with eg:
Keywords are words which belong to java language. They have standard predefined
meaning. The users have no right to change its meaning. Keywords should be written in
lowercase. The list of keywords are:
abstract, boolean, int, float, static, byte, char, for, if,… etc
8) Explain JVM:
Java compiler compiles the source code and produces a machine independent
intermediate code called bytecode. These intermediate codes are called java virtual machine
(JVM)
These intermediate codes can be used by any machine with the help of correct interpreter.
The interpreter produces the machine dependent code called machine code and can be run by
the computer.
Steps involved in executing a java program are:
User
Virtual Real Machine
Machine
9) Define Data types:
Data types specify the size and type of value that are to be stored. The data types are
defined into two categories. They are:
1) Primitive (or) built-in data types.
2) Derived (or) reference data types.
10) What are Primitive and Derived data types :
Primitive data types are data types available in language itself.
Derived data types are user defined data types.
Source
Code
Java
Compiler
Byte
code
Java
Interpreter
Machine
Code
19. The figure below shows the data types under various categories:
11) What is class?
A class is a user defined data type. It contains data and its related methods. The
general form is,
class classname
{
datatype field-1;
……..
…….
Datatypes
Primitive Derived
Numeric NonNumeric Class Arrays
Integer Real Character Boolean
byte Long
Float
Double
Short int
20. datatype field-n;
datatype methodname-1(parameter list)
{
body of the method
}
………
datatype method-n(parameter list)
{
body of the method
}
12) How variables are added to a class?
To add variable in a class it must be defined inside a class. At once a variable is
added to class it becomes instance variable.
eg: class student
{
int regno;
char name;
}
13) Define Methods:
Methods are used to access the data fields in a class. To add a method it must be
defined after the data field definition. At once a method is added to a class it becomes an
instance method or member function. The general form is,
datatype methodname(parameter list)
{
21. body of the method
}
14) How the objects are created?
Objects are created from the defined class. Using a class we can create any
number of objects. All created objects can use the instance variables. Memory space for
the instance variables will be allocated only during object creation.
15) List down the steps to create an object
i) Declare the objects:
Syntax:
classname obj1,obj2,….objn;
ii) Create memory space:
After object declaration, we must allocate memory space foe the instance
variables for each object. This is done with the help of new operator.
Syntax:
obj1= new classname();
obj2= new classname();
obj3= new classname();
…………..
…………..
objn= new classname();
16) Define Inheritance:
Inheritance is the process of creating new classes from the existing classes. The new
classes are called derived classes. The existing classes are called base classes.
The derived classes inherit all the properties of the base class plus its own properties.
The process of inheritance does not affect the base classes.
The figure given below shows the inheritance:
22. 17) What are the types of inheritance and explain:
The different types of inheritance are:
1) Single inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
4) Multiple Inheritance
1) SINGLE INHERITANCE:
A class derived from one superclass is called single inheritance.
2) MULTILEVEL INHERITANCE:
A class derived from other derived class is called multilevel inheritance
3) HIERARCHICAL INHERITANCE
More classes derived from one super class is called hierarchical
inheritance
4) MULTIPLE INHERITANCE:
A class derived from more than one superclass is called nultiple inheritance. But
java does not support direct implementation of multiple inheritance.
Property1
Property2
Property3
Property4
Property1
Property2
Property3
Property4
Base class
Inheritance
Inheritsfrombase class
Definedinthe derivedclass
Derived
class