0% found this document useful (0 votes)
30 views19 pages

DCS 212 Lecture Note

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views19 pages

DCS 212 Lecture Note

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

KEBBI STATE UNIVERSITY OF SCIENCE AND TECHNOLOGY, ALIERO

DEPARTMENT OF COMPUTER SCIENCE


FACULTY OF PHYSICAL SCIENCES
CONSULTANCY SERVICES LIMITED

DCS 212 (INTRODUCTION TO OBJECT ORIENTED


PROGRAMMING JAVA LANGUAGE)
LECTURE NOTE

FOR

DIPLOMA II STUDENTS
KSUSTACONS

1
What is a Program?
A program is a set of unambiguous instructions written in any specific language (e.g. C, C++,
Java, Python) to perform a specific task.

Programming refers to the act or process of writing set of unambiguous instructions


(program) written in any specific language ( e.g. C, C++, Java, Python) to perform a specific
task.

A programming language is mainly used to develop desktop applications, websites, and


mobile applications.

Types of programming language


i. Machine Language

Machine language is a type of low-level programming language. It is also called as machine


code or object code. Machine language is easier to read because it is normally displayed in
binary or hexadecimal form (base 16) form. It does not require a translator to convert the
programs because computers directly understand the machine language programs.

The advantage of machine language is that it helps the programmer to execute the programs
faster than the high-level programming language.

ii. Low-level programming language

Assembly Language

Assembly language (ASM) is also a type of low-level programming language that is designed
for specific processors. It represents the set of instructions in a symbolic and human-
understandable form. It uses an assembler to convert the assembly language to machine
language.

The advantage of assembly language is that it requires less memory and less execution time
to execute a program.

iii. High-level programming language

High-level programming language (HLL) is designed for developing user-friendly software


programs and websites. This programming language requires a compiler or interpreter to
translate the program into machine language (execute the program).

The main advantage of a high-level language is that it is easy to read, write, and maintain.

High-level programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective
C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.

A high-level language is further divided into the following -

2
a. Procedural Oriented programming language

Procedural Oriented Programming (POP) language is derived from structured programming


and based upon the procedure call concept. It divides a program into small procedures called
routines or functions.

Procedural Oriented programming language is used by a software programmer to create a


program that can be accomplished by using a programming editor like IDE, Adobe
Dreamweaver, or Microsoft Visual Studio.

The advantage of POP language is that it helps programmers to easily track the program flow
and code can be reused in different parts of the program.

The advantage of POP language is that it helps programmers to easily track the program flow and
code can be reused in different parts of the program.

Example: C, FORTRAN, Basic, Pascal, etc.

b. Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In this


programming language, programs are divided into small parts called objects. It is used to
implement real-world entities like inheritance, polymorphism, abstraction, etc in the program
to makes the program reusable, efficient, and easy-to-use.

The main advantage of object-oriented programming is that OOP is faster and easier to
execute, maintain, modify, as well as debug.

Note: Object-Oriented Programming language follows a bottom-up approach.

Example: Java, C++, Python, C#, etc.

TRANSLATOR
A computer program is a set of instructions for the computer to perform a certain task. Most
programs are written in high level languages or assembly language. These programs are easy
to read and understand by programmers but not understandable by the computer. The
computer only understands machine language. It consists of binary which is ones and zeros.
Therefore, the high level or assembly program should be converted into machine language
for the computer to understand the instructions and this is done by a program called
translator.

Hence a translator is a program that converts program written in programming languages to


machine executable codes.

3
TYPES OF TRANSLATOR
❖ Compiler
❖ Interpreter and
❖ Assembler

Compiler and interpreter are translators that convert high level language based programs
to machine executable codes.

Assembler is a translator that converts program written in assembly language to machine


executable codes.

✓ JAVA PROGRAMMING LANGUAGE

The Brief History of Java Programming Language

Java is an Object-Oriented programming language developed by James Gosling in the early 1990s. The
team initiated this project to develop a language for digital devices such as set-top boxes, television,
etc. Originally C++ was considered to be used in the project but the idea was rejected for several
reasons (For instance C++ required more memory). Gosling endeavoured to alter and expand C++
however before long surrendered that for making another stage called Green. James Gosling and his
team called their project “Greentalk” and its file extension was .gt and later became known as “OAK”.
Why “Oak”? The name Oak was used by Gosling after an oak tree that remained outside his office.
Also, Oak is an image of solidarity and picked as a national tree of numerous nations like the U.S.A.,
France, Germany, Romania, etc. But they had to later rename it as “JAVA” as it was already a
trademark by Oak Technologies. “JAVA” Gosling and his team did a brainstorm session and after the
session, they came up with several names such as JAVA, DNA, SILK, RUBY, etc. Java name was decided
after much discussion since it was so unique. The name Java originates from a sort of espresso bean,
Java. Gosling came up with this name while having a coffee near his office. Java was created on the
principles like Robust, Portable, Platform Independent, High Performance, Multithread, etc. and was
called one of the Ten Best Products of 1995 by the TIME MAGAZINE. Currently, Java is used in internet
programming, mobile devices, games, e-business solutions, etc. The Java language has experienced a
few changes since JDK 1.0 just as various augmentations of classes and packages to the standard
library. In Addition to the language changes, considerably more sensational changes have been made
to the Java Class Library throughout the years, which has developed from a couple of hundred classes
in JDK 1.0 to more than three thousand in J2SE 5.

Features of Java
Following are some of the useful and advanced features of java:

1. Simple

Java is a very simple programming language, it is easy to learn, read and write in Java. The syntax of
Java is clean and easy to understand.

2. Platform Independent

4
Java is a platform independent language. Compiler (javac) converts source code (.java file) to the byte
code (.class file). JVM executes the bytecode produced by compiler. This byte code can run on any
platform such as Windows, Linux, Mac OS etc. Which means a program that is compiled on windows
can run on Linux and vice-versa.

3. Secure

Security is one of the biggest concern in programming language as these programming languages are
used to develop some of the critical and sensitive applications that needs to be secured such as
banking applications. Java is more secure than C/C++ as does not allow developers to create pointers,
thus it becomes impossible to access a variable from outside if it’s not been initialized.

4. Object-Oriented Programming language

Object oriented programming is a way of organizing programs as collection of objects, each of which
represents an instance of a class.

5. Robust

Robust means reliable. Java programming language is developed in a way that puts a lot of emphasis
on early checking for possible errors, that’s why java compiler is able to detect errors that are not easy
to detect in other programming languages.

6. Distributed

Using java programming language, we can create distributed applications. RMI (Remote Method
Invocation) and EJB(Enterprise Java Beans) are used for creating distributed applications in java.

7. Multithreading

Java supports multithreading. Multithreading is a Java feature that allows concurrent execution of two
or more parts of a program for maximum utilisation of CPU.

8. Portable

As discussed above, java code that is written on one machine can run on another machine. The
platform independent byte code can be carried to any platform for execution that makes java code
portable.

9. Architectural Neutral

As we know Java is a platform independent language, which means program written and compiled on
one machine can run on any other machine having different operating system. Java follows the
principle of “Write once run anywhere“

10. Dynamic

Java is a dynamic programming language. OOPs allows developers to add new classes to the existing
packages, add new methods to the existing classes as well as modifying the method without changing
the original method code by using the concept of method overriding.

5
12. Performance

Java is significantly faster than other traditional interpreted programming languages. Compiled java
code which is known as byte code is like a machine code, that allows a faster execution. Java uses Just
in Time compiler which can execute the code on demand, this allows to execute only the method that
is being called, which makes it faster and efficient.

First Java Program

In the previous example, we created a Java file called Main.java, and we used the following code to
print "Hello World" to the screen.

6
Example explained

Every line of code that runs in Java must be inside a class. In our example, we named the class Main.
A class should always start with an uppercase first letter.

Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.

The name of the java file must match the class name. When saving the file, save it using the class
name and add ".java" to the end of the filename.

Identifiers
All Java variables must be identified with unique names. These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
The general rules for naming variables are:

• Names can contain letters, digits, underscores, and dollar signs


• Names must begin with a letter
• Names should start with a lowercase letter and it cannot contain whitespace
• Names can also begin with $ and _
• Names are case sensitive ("myVar" and "myvar" are different variables)
• Reserved words (like Java keywords, such as int or boolean) cannot be used as names.

The main Method

7
Java Variables
Variables are containers or named memory space for storing data values.

In Java, there are different types of variables based on values they hold or store, for example:

• String - stores text, such as "Hello". String values are surrounded by double quotes
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• float - stores floating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
• boolean - stores values with two states: true or false.

Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

Where type is one of Java's types (such as int or String), and variableName is the name of the variable
(such as x or name). The equal sign is used to assign values to the variable.

8
However, variables can be classified based on accessibility as follows:

• Local variables: Variables defined inside methods, constructors or blocks are called local variables.
The variable will be declared and initialized within the method and the variable will be destroyed
when the method has completed.
• Instance variables: Instance variables are variables within a class but outside any method. These
variables are instantiated when the class is loaded. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
• Class variables: Class variables are variables declared with in a class, outside any method, with
the static keyword.

9
Java Keywords:
The following list shows the reserved words in Java. These reserved words may not be used as
constant or variable or any other identifier names.

Java Operators
Java divides the operators into the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators

10
Java Assignment Operators
Assignment operators are used to assign values to variables.

Java Comparison Operators


Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions.

The return value of a comparison is either true or false

11
The Bitwise Operators:

String Concatenation
The + operator can be used between strings to combine them. This is called concatenation

Java Methods
A method is a block of code which only runs when it is called. You can pass data, known as parameters,
into a method. Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.

Create a Method
A method must be declared within a class. It is defined with the name of the method, followed by
parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can
also create your own methods to perform certain actions.

12
13
14
15
OOPs (Object-Oriented Programming) CONCEPTS

Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-
Oriented Programming is a methodology or paradigm to design a program using classes and
objects. It simplifies software development and maintenance by providing some concepts:

• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

16
What is an object in Java?
An entity that has state and behaviour is known as an object e.g., chair, bike, marker, pen,
table, car, etc. It can be physical or logical (tangible and intangible). The example of an
intangible object is the banking system.

An object has three characteristics:

• State: represents the data (value) of an object.


• Behaviour: represents the behaviour (functionality) of an object such as deposit,
withdraw, etc.
• Identity: An object identity is typically implemented via a unique ID. The value of the
ID is not visible to the external user. However, it is used internally by the JVM to
identify each object uniquely.

For Example, Pen is an object. Its name is Reynolds; colour is white, known as its state. It is
used to write, so writing is its behaviour.

An object is an instance of a class. A class is a template or blueprint from which objects are
created. So, an object is the instance(result) of a class.

Object Definitions:

• An object is a real-world entity.


• An object is a runtime entity.
• The object is an entity which has state and behaviour.
• The object is an instance of a class.

What is a class in Java?


Class: Collection of objects is called class. It is a logical entity. A class can also be defined as a blueprint
from which you can create an individual object. Class doesn't consume any space.

A class is a group of objects which have common properties. It is a template or blueprint


from which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain:

• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface

Inheritance: when one object acquires all the properties and behaviours of a parent object, it is known
as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

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

17
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.

Why use inheritance in java

• For Method Overriding (so runtime polymorphism can be achieved).


• For Code Reusability.

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 you create a new class. You can use the
same fields and methods already defined in the previous class.

The syntax of Java Inheritance

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. 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.

Types of Inheritance in Java

On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.

18
Polymorphism: If one task is performed in different ways, it is known as polymorphism. For example:
to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism. Another
example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.

Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many
and "morphs" means forms. So polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.

Abstraction: Abstraction is a process of hiding the implementation details and showing only
functionality to the user. Another way, it shows only essential things to the user and hides the
internal details, for example, sending SMS where you type the text and send the message.
You don't know the internal processing about the message delivery. Abstraction lets you focus
on what the object does instead of how it does it.

A class which is declared with the abstract keyword is known as an abstract class in Java. It
can have abstract and non-abstract methods (method with the body). Before learning the
Java abstract class, let's understand the abstraction in Java first.

Encapsulation: Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped with different medicines. A java class is the
example of encapsulation. Java bean is the fully encapsulated class because all the data members are
private here.

19

You might also like