Professional Documents
Culture Documents
But as they are objects, the chairs must also have a set
of methods (meaning what we can do with these
chairs). The set of methods could be like the
following.
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
Buy ( );
Sell ( );
Repair ( ); Paint ( ); Move ( );
As you can understand, if we execute these methods,
then one or some of the attribute values will be
accessed or changed. For example, to Buy ( ) or Sell (
) the chair, its price attribute will be changed. To
Repair ( ) a chair, we may change its dimensions,
weight or material. To Paint ( ) a chair its color
property will be changed. To Move ( ) a chair, we
change its location.
THE CONCEPT OF CLASS
If some objects are found to have a similar set of
attributes and a similar set of methods, then these
objects can be grouped together into a class. A class
is a collection of declaration of the attribute
variables and the definition of the methods. Once
the class has been defined, we can create as many
Chair3.Move ( );
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
Polymorphism
This means the quality of the same thing to exist in
different forms. This is an integral feature of object
oriented programming that is not found in sequential
programming.
Inheritance
This is also another unique feature of object oriented
programming. This means the creation of a new class
out of an existing class. Inheritance supports the
concept of code reuse and saves a lot of time and
effort to create a new class.
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
PROGRAMMING IN JAVA
Now it is the time to write the first program in Java.
The following is the code for a program for adding
two numbers. We are assuming that you have some
knowledge of the C and C++ language.
import java.io.*;
class Ayan {
public static void main(String args[]) throws IOException {
int a, b, c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\n\tEnter first number : ); a = Integer.parseInt(br.readLine());
System.out.print(\n\tEnter second number : ); b = Integer.parseInt(br.readLine());
c = a + b; System.out.print(\n\tThe sum of +a+ and +b+ is : +c); }
}
import java.io.*;
===================================
class Ayan
{
: :
}
===================================
a = Integer.parseInt(br.readLine());
c = a + b;
}
void output() // METHOD {
System.out.print(\n\tThe sum of +a+ and +b+ is : +c); }
}
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
class Ayan {
public static void main(String args[]) throws IOException {
Add obj = new Add(); // CREATING AN OBJECT OF THE Add CLASS
obj.input(); obj.add(); obj.output();
}
}
WRAPPER CLASSES
Because Java is a strictly object oriented programming
language, the data type themselves are created as
objects. We have to consider that all the data types that
we have mentioned here are themselves the objects of
some class. The classes from which the data type
themselves are declared as objects are called Wrapper
classes. All such classes are part of the java.lang
package.
The Number wrapper class is the super class for six
other wrapper classes that deals with all the numeric
data types. These classes are Double, Float, Byte,
Short, Integer and Long. In the previous example
where we have shown that the integer value is taken
CONSTRUCTORS
A constructor is a member method of any class whose
objective is to initialize the attributes of an object of
that class. A constructor has the following special
properties:
1. The name of the constructor is exactly the same as
the name of the class
2. Although it is a method, a constructor does not have
any return type. Not even the void type.
3. A constructor method need not be called separately
using an object of that class. The constructor is called
automatically whenever an object of that class is
created.
4. Depending upon its use, a constructor may or may
not receive any argument.
The following program demonstrates the use of
different types of constructors:
import java.io.*;
class ABC {
char a;
int b;
double c;
ABC() // DEFAULT CONSTRUCTOR, TAKES NO ARGUMENT {
a = F;
b = 10;
c = 3.1248;
}
ABC(char a1, int b1, double c1) // PARAMETERIZED CONSTRUCTOR, TAKES
ARGUMENTS {
a = a1; b = b1; c = c1;
}
ABC(ABC obj) // COPY CONSTRUCTOR, TAKES AN ENTIRE OBJECT AS
ARGUMENT {
a = obj.a; b = obj.b; c = obj.c;
}
void output() {
System.out.print(\n\n\tCHAR : +a); System.out.print(\n\tINT : +b);
System.out.print(\n\tDOUBLE : +c);
}
}
class Ayan {
public static void main(String args[]) {
ABC obj1 = new ABC(); // CALLING DEFAULT CONSTRUCTOR
ABC obj2 = new ABC(D, 25, 36.997); // CALLING PARAMETERIZED
CONSTRUCTOR ABC obj3 = new ABC(obj2); // CALLING COPY CONSTRUCTOR
obj1.output(); obj2.output(); obj3.output();
}
}
obj1.output(); obj2.output(); }
}
CONSTRUCTOR OVERLOADING
If we look at the program in the previous chapter
where we had used default constructor, parameterized
constructor and copy constructor in the same program,
we can say that this is an example of constructor
overloading. The only exception between constructor
overloading and method overloading is that
constructors do not have any return type and methods
have return type. Otherwise, constructor overloading
involves the creation of multiple versions of the same
constructor with different argument set and body.
STATIC ATTRIBUTES AND STATIC METHODS
The static attributes of a class are based on the same
concept of the static storage class as we find in C /
C++. The most important feature of a variable of static
storage class is that they contain a default initialized
value of zero and they can remember their value
between different method calls.
The static methods of a class are unique in the sense
because with static methods we can intentionally
override one of the most fundamental concepts of
object oriented programming language. So far we have
seen that in order to call a member method of a class,
first an object of that class is created and the methods
can be called using that object. However, if a member
method of a class is declared as static, it can be called
INHERITANCE
Inheritance is an integral feature of any object oriented
programming language. Inheritance is the process of
creating a new class out of an existing class. In Java
terminology, the existing class is called the super
class and the newly created class is called sub class.
Due to inheritance the subclass may inherit some or all
the features of the base class.
Whenever there is a need to create a new class, the
programmer has two choices. Either he/she can create
the class, its attributes and the methods out of scratch,
that may take a lot of labour, time (and thereby,
money). Another option is to look out in the collection
of already created classed and trying to find out a class
that most closely matches the attribute set and the
method set of the class that is required to be created. If
such a class can be found, then all that is needed to be
done is to create the new class (sub class) out of the
already created class (super class) and directly inherit
the common attributes and the methods, and add only
those attributes and methods which are needed
SINGLE INHERITANCE
Let us now explain using some real program. The
following program describes a single inheritance. The
name of the program is InheritanceTest.java
import java.io.*; // SINGLE INHERITANCE
class A {
int a;
void inputA() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\n\tEnter first number : );
a = Integer.parseInt(br.readLine());
}void outputA()
{
} System.out.print(\n\tFirst number : +a);
}
class B extends A
{
int b;
void inputB() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\n\tEnter second number : );
b = Integer.parseInt(br.readLine());
}void outputB()
{
} System.out.print(\n\tSecond number : +b);
}
class InheritanceTest
{
public static void main(String args[]) throws IOException {
B obj = new B();
obj.inputA(); obj.inputB(); obj.outputA(); obj.outputB(); }}
{
public static void main(String args[]) {
B obj = new B(10, 20);
obj.outputA(); obj.outputB(); }
}
Hierarchical Inheritance
The following program shows how we can create a
new class using hierarchical inheritance.
import java.io.*;
class A
{
int a;
A(int a1) {
} a = a1;
void outputA()
{ System.out.print(\n\tFirst number : +a); }
}
class B extends A
{ int b;
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com obj2.outputA(); obj2.outputC(); }
}
METHOD OVERRIDING
This program also stands as an example of what is
called method overriding. We have already discussed
method overloading in the earlier chapter, which
EXCEPTION HANDLING
}
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
MULTITHREADING
A thread can be considered as a part of a process that
can be executed independently by the CPU. Any
running program is called a process. Normally, a CPU
can execute one process at a time. In case of
ayankumarchatterjee@yahoo.com
t.start(); }
}
class HiLoPri {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Clicker hi = new
Clicker(Thread.NORM_PRIORITY + 2); // PRIORITY 7 Clicker lo = new
Clicker(Thread.NORM_PRIORITY - 2); // PRIORITY 3
lo.starting(); hi.starting(); try {
Thread.sleep(10000); }
catch (InterruptedException e) {
System.out.println(Main thread interrupted.); }
lo.stopping(); hi.stopping();
// Wait for children threads to terminate. try
{
hi.t.join();
lo.t.join();
}
catch (InterruptedException e)
{
System.out.println(InterruptedException caught);
}System.out.println(Low-priority thread : + lo.click);
System.out.println(High-priority thread : + hi.click); }
}
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
STRING HANDLING
In C or C++ programming languages, a character array
is called a string. There are some fundamental
difference between an array of any other data type and
that of a character data type.A null character (\0) is
automatically appended at the end of the character
string, which does not happen in the case of an array
of any other data type. Additionally, while trying to
make input or output with a character array, the entire
objects of the String class, the objects can call all the
member methods available in the String class. Here we
should be seeing how to call the member methods of
the String class to perform different types of activities.
The first method is the length ( ) method of the String
class, that returns the number of characters in the
string.
// LENGTH OF A STRING
class StringTest
{
public static void main(String args[]) throws IOException {
String s;
int l;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\n\tEnter a string : ); s = br.readLine();
l = s.length(); System.out.print(\n\tThe string contains +l+ characters.); }
}
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
{
String s;
int i;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\n\tEnter a string : ); s = br.readLine();
for(i = 0 ; i <= s.length() ; i++)
System.out.print(\n\t+s.substring(0, i));
// STARTING FROM THE 0 INDEX, EXTRACTING i NUMBER OF CHARACTERS
}}
type the url for the HTML page stored in the remote
machine. As a result the HTML page is fetched from
the remote machine to the local machine via the
network. The job of providing the Java library files
rests on the HTML browser.
DIFFERENCE BETWEEN AN APPLICATION
AND AN APPLET There are some important
differences between an application and an applet.
1. Applications resides in the same machine where it is
executed, applet resides in a different machine.
2. Applications are fetched from the local hard disk;
applets are fetched from the network.
Teacher of Computer Science and Software Technology
1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email:
ayankumarchatterjee@yahoo.com
}
else
{
if(n2 == 0)
msg = The result is : Undefined; else
{
result = n1 / n2;
msg = The result is : +result; }
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, 50, 50);
}
}
normally as
javac Calculator.java