You are on page 1of 34

Chapter One

Object oriented programming

By Temesgen T(MSc).
Chapter one
Introduction to object-oriented programming

Programming paradigms
 Is a way or style of programming while developing
software projects.
 There are many programming paradigms, some of them
are listed below;
1. Imperative programming
 is a programming paradigm that uses statements that
change a program's state.
 consists of commands for the computer to perform.
 First do this and next do that approach.
 Eg. FORTRAN, COBOL
2
Cont…
2. Functional programming
 A simpler and more clean paradigm than imperative
one.
 a style of building the structure and elements of
computer programs by using functions.
 Control flow is expressed by combining function calls,
rather than by assigning values to variables.
 Code is much shorter, less error-occurrence and much
easier to prove correct.
 Evaluate an expression and use the resulting value for
something Approach.

3
Cont…..
3. Logic programming
 is a programming paradigm based on formal logic.
 Consists of a set of facts and rules.
 A program written in a logic programming language is
a set of sentences in logical form, expressing facts and
rules about some problem domain.
 Often used for developing Artificial Intelligence
systems.
 Answer a question via search for a solution approach.
 Eg. Prolog and Data log are the most popular
programming languages.

4
Cont…
4. Object-oriented programming
What is OOP?
 Object-oriented programming (OOP) is a programming
language model organized around objects rather than
"actions" and data rather than logic.
 OOP is based on the sending of messages to objects.
 Objects respond to messages by performing operation.
 Message can have arguments.
 In most object-oriented languages objects are grouped in classes.
 Send messages between objects to simulate the temporal evolution
of a set of real world phenomena.
Eg. Java and C++
5
Why OOP become most popular?

 Allows Code reusability to eliminate redundant code.


 More reliable and more easily maintained.
 It is easy to partition the complex work in a project base on
objects.
 Software complexity can be easily managed.
 The principle of data hiding helps the programmer to build
secure programs.
easing changes to programs.
is widely popular in large scale software engineering.
easier to learn.

6
Principles of OOP
Inheritance
Polymorphism
Encapsulation
Abstraction

7
Inheritance
 Inheritance in Java is a mechanism in which one object acquires all
the properties and behaviors of a parent object.
 When you inherit from an existing class, you can reuse methods and
fields of the parent class.
 Why use inheritance in java?
 For Method Overriding (so runtime polymorphism can be achieved).
 For Code Reusability.
 #Its syntax
 class Subclass-name extends Superclass-name
{
 //methods and fields
}

8
Polymorphism
Polymorphism is the ability of any data to be
processed in more than one form. The word itself
indicates the meaning
as poly means many and morphism means types.
If a class has multiple methods having same name but
different in parameters, it is known as Method
Overloading.

9
Abstraction

Abstraction is a process of hiding the implementation


details and showing only functionality to the user.
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).
There are two ways to achieve abstraction in java
Abstract class (0 to 100%)
Interface (100%)

10
Cont’d

11
Interface

An interface in Java is a blueprint of a class. It has


static constants and abstract methods.

12
Encapsulation
A java package is a group of similar types of classes,
interfaces and sub-packages.
Package in java can be categorized in two form, built-
in package and user-defined package.
There are many built-in packages such as java, lang,
awt, javax, swing, net, io, util, sql etc.
Encapsulation in Java is a process of wrapping code
and data together into a single unit, for example, a
capsule which is mixed of several medicines.

13
Java
History of Java
 James Gosling, Mike Sheridan, and Patrick Naughton initiated the
Java language project in June 1991. The small team of sun engineers
called Green Team.
 Originally developed by James Gosling at Sun Microsystems (which
is now a subsidiary of Oracle Corporation) and released in 1995.
Why "Oak" name
 Why Oak? Oak is a symbol of strength and choosen as a national
tree of many countries like U.S.A., France, Germany, Romania etc.
 In 1995, Oak was renamed as "Java" because it was already a
trademark by Oak Technologies.
 James Gosling:

14
Cont….
• Java is a general purpose, concurrent, class based, object
oriented computer programming language that is specifically
designed, to have a few implementation dependencies as
possible.
Java is a programming language and a platform.
Java is a high level, robust, secured and object-oriented
programming language.
Features of Java
There is given many features of java. They are also known
as java buzzwords. The Java Features given below are
simple and easy to understand.
15
Cont….
1. Simple
 According to Sun, Java language is simple because:
 syntax is based on C++ (so easier for programmers to learn it after C++).
2. Object-oriented
 Object-oriented means we organize our software as a combination of different types of
objects that incorporates both data and behaviour.
 Object-oriented programming (OOPs) is a methodology that simplifies software
development and maintenance by providing some rules.
Basic concepts of OOPs are:
 Class
 Object
 Inheritance
 Polymorphism
 Abstraction
 Encapsulation

16
Cont…
3. Platform Independent:
A platform is the hardware or software environment in which a program runs.
 There are two types of platforms software-based and hardware-based. Java
provides software-based platform.
 The Java platform differs from most other platforms in the sense that it is a
software-based platform that runs on the top of other hardware-based
platforms. It has two components:
I. Runtime Environment
II. API(Application Programming Interface)
 Java code can be run on multiple platforms e.g. Windows, Linux, Sun
Solaris, Mac/OS etc. Java code is compiled by the compiler and converted
into bytecode. This bytecode is a platform-independent code because it can
be run on multiple platforms i.e. Write Once and Run Anywhere (WORA).
17
Cont…
4. Secured
Java is secured because:
No explicit pointer
Java Programs run inside virtual machine sandbox.
Sandbox: is an isolated area where program can be executed with a
restricted portions of resources available.
5. Robust
Robust simply means strong. Java uses strong memory
management. There is lack of pointers that avoids security
problem. There is automatic garbage collection in java. There is
exception handling and type checking mechanism in java. All
these points make java robust.
18
Cont…
6. Portable
 We may carry the java bytecode to any platform.
7. Distributed
 We can create distributed applications in java. RMI and EJB are used
for creating distributed applications. We may access files by calling the
methods from any machine on the internet.
8. Multi-threaded
 A thread is like a separate program, executing concurrently. We can
write Java programs that deal with many tasks at once by defining
multiple threads.
 The main advantage of multi-threading is that it doesn't occupy
memory for each thread. It shares a common memory area. Threads are
important for multi-media, Web applications etc.
19
Cont…

20
Where it is used?
 According to Sun, 3 billion devices run java. There are many
devices where Java is currently used. Some of them are as
follows:
 Desktop Applications such as acrobat reader, media player,
antivirus etc.
 Web Applications such as irctc.co.in, javatpoint.com etc.
 Enterprise Applications such as banking applications.
 Mobile
 Embedded System
 Smart Card
 Robotics
 Games etc.

21
Java Editions
There are 4 platforms or editions of Java:
1) Java SE (Java Standard Edition)
 It is a java programming platform. It includes Java programming APIs
such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It
includes core topics like OOPs, String, Regex, Exception, Inner classes,
Multithreading, I/O Stream, Networking, AWT, Swing, Reflection,
Collection etc.
2) Java EE (Java Enterprise Edition)
 It is an enterprise platform which is mainly used to develop web and
enterprise applications. It is built on the top of Java SE platform.
 It includes topics like Servlet, JSP, Web Services, EJB, JPA etc.
3) Java ME (Java Micro Edition)
 It is a micro platform which is mainly used to develop mobile
applications.
4) Java Fx
 It is used to develop rich internet applications. It uses light-weight user
22
interface API.
Java virtual machine
 The Java Virtual machine (JVM) is the virtual machine that runs on
actual machine (your computer) and executes Java byte code.
 The JVM doesn’t understand Java source code, that’s why we need to
have javac compiler that compiles *.java files to obtain *.class files that
contain the byte codes understood by the JVM.
 JVM makes java portable (write once, run anywhere).
 Each operating system has different JVM, however the output they
produce after execution of byte code is same across all operating
systems.

23
Java Runtime Environment (JRE)
It is a software package that contains what is required
to run a Java program. It includes a Java Virtual
Machine implementation together with an
implementation of the Java Class Library.
 The JDK consists of a set of separate programs, each
invoked from a command line, for developing and testing
Java programs.
 Instead of using the JDK, you can use a Java development
tool (e.g., NetBeans, Eclipse, and TextPad)—software that
provides an integrated development environment (IDE) for
developing Java programs quickly.

24
Cont…
Creating, Compiling, and Executing a Java Program
 You save a Java program in a .java file and compile it into a .class file.
 The .class file is executed by the Java Virtual Machine.

 You have to create your program and compile it before it can be executed.

 If your program has compile errors, you have to modify the program to fix
them, and then recompile it.

 Note: The source file must end with the extension .java and must have the
same exact name as the public class name.

 For example, the file for the source code in the following should be named
Welcome.java, since the public class name is Welcome.
25
public class Welcome {
public static void main(String[] args) {
// Display message Welcome to Java! on the console
System.out.println("Welcome to Java!");
}
}
 File name must be Welcome.java.
 A Java compiler translates a Java source file into a Java bytecode file.

 Note: You must first install and configure the JDK before you can
compile and run programs.

 If there aren’t any syntax errors, the compiler generates a bytecode file
with a .class extension.

26
 The Java language is a high-level language, but Java byte code is a low-
level language.

 The byte code is similar to machine instructions but is architecture neutral


and can run on any platform that has a Java Virtual Machine (JVM)

 The virtual machine is a program that interprets Java byte code.

 This is one of Java’s primary advantages: Java byte code can run on a
variety of hardware platforms and operating systems.

 Java source code is compiled into Java byte code and Java byte code is
interpreted by the JVM.

 To execute a Java program is to run the program’s byte code. You can
execute the byte code on any platform with a JVM, which is an interpreter.

27
Creating hello java example
Let's create the hello java program:
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
save this file as Simple.java
To compile: javac Simple.java
To execute: java Simple
Output: Hello Java
28
Class and object example
 public class MyCalculatr {
 public double numb1,numb2;//declaration of instance variable
 public double sum;
 public void Add(double numb1,double numb2)//operation(methods)
{
 sum= numb1+numb2;
 System.out.println(sum);
}
 public static void main(String[] args) {
 MyCalculatr mc= new MyCalculatr();
 double numb1=9;
 double numb2=8;
 mc.Add(numb1,numb2);
 }
}

29
Cont…
Understanding first java program
 Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
 class keyword is used to declare a class in java.
 public keyword is an access modifier which represents visibility, it means it is
visible to all.
 static is a keyword, if we declare any method as static, it is known as static method.
The core advantage of static method is that there is no need to create object to
invoke the static method. The main method is executed by the JVM, so it doesn't
require creating object to invoke the main method. So it saves memory.
 void is the return type of the method, it means it doesn't return any value.
 main represents startup of the program.
 String[] args is used for command line argument. We will learn it later.
 System.out.println() is used print statement. We will learn about the internal
working of System.out.println statement later.
30
 The Java program-development process consists of repeatedly creating/modifying source code,

compiling, and executing programs .

31
// A java program that takes two numbers from the user and displays the sum result

32
Out put
Hello out there.
I will add two numbers for you.
Enter two whole numbers on a line:
56
The sum of those two numbers is
11

33
The End!

34

You might also like