0% found this document useful (0 votes)
10 views47 pages

Chapter 1 - OOP For CS

This document provides an introduction to Object-Oriented Programming (OOP) and various programming paradigms, including procedural, structured, functional, imperative, declarative, and OOP itself. It discusses the characteristics, advantages, and examples of each paradigm, with a focus on Java as a prominent OOP language and its applications. Additionally, the document outlines the history and evolution of programming languages, particularly C, C++, and Java.
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)
10 views47 pages

Chapter 1 - OOP For CS

This document provides an introduction to Object-Oriented Programming (OOP) and various programming paradigms, including procedural, structured, functional, imperative, declarative, and OOP itself. It discusses the characteristics, advantages, and examples of each paradigm, with a focus on Java as a prominent OOP language and its applications. Additionally, the document outlines the history and evolution of programming languages, particularly C, C++, and Java.
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/ 47

Chapter One

Introduction to Object Oriented


Programming

Compiled by: | Sinodos G


Introduction
 What is programming Language?
• a computer language that is used by programmers (developers)
to communicate with computers.

• It is a set of instructions written in any specific language ( C,


C++, Java, Python, PHP, JS, …) to perform a specific task.

• mainly used to develop desktop applications, Enterprise,


websites, and mobile applications.

• In general it is used to design and implement different types of


software's.

2
Programming Paradigm’s
 a way to classify programming languages based on their style
and approach to solving problems.
 Different paradigms provide different ways of thinking about and
structuring code.
 Paradigm's a style, or “way,” of programming.
 Some languages make it easy to write in some paradigms but not
others.
 different ways or styles in which a given program or
programming language can be organized.
 Each paradigm consists of certain structures, features, and
opinions about how common programming problems should be
tackled.

3
Cont’d …

 Different programming languages follow different approaches to


solving programming problems
 A programming paradigm is an approach to solving programming
problems
 Some of the common programming paradigms
• Procedural Programming
• Structured Programming
• Functional Programming
• Imperative Programming
• Declarative Programming
• Object-Oriented Programming

4
Cont’d …
 Procedural Programming
• focuses on functions and procedures that manipulate data.
• follows a top-down approach during the designing of a
program.
• It gives importance to the concept of the function and divides
the large programs into smaller parts or called as functions.
• straightforward.
• It follows a step-by-step approach in order to break down a
task into a set of variables and routines via a sequence of
instructions.
• Examples:
• ALGOL, COBOL, BASIC, PASCAL, FORTRAN, C.

5
Cont’d …

Example:
public class ProceduralEx {
public static void main(String[] args) {
int a = 5;
int b = 3;
int sum = add(a, b);
System.out.println("Sum: " + sum);
}

static int add(int x, int y) {


return x + y;
}
}
6
Cont’d …

 Structured Programming
• Also known as modular programming.
• characterized by the use of procedures or functions, control flow
constructs, and well-defined, modular structures.
• It improve code readability, maintainability, and reliability by
avoiding unstructured practices like goto statements.
• intends to optimize the code by using the program control flow
constructs , decision making (If , If Then , Else ) constructs and
the iteration constructs (For , while loops ), blocks and the
functions.
• code will execute the instruction by instruction one after the other.

• Example: C, C++, Java, C#

7
Cont’d …
Example:
public class StructuralEx {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
else {
System.out.println("The number is not positive.");
}
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
}
}
8
Cont’d …

 Declarative Programming
• focuses on describing what the program should do rather than
how it should be done.
• Programming by specifying the result you want, not how to get it.
• SQL is an example in the context of databases

 Functional Programming
• Functional programming emphasizes immutability and the use of
pure functions.
 Imperative Programming
• Programming with an explicit sequence of commands that update state.

9
Cont’d …
Object-oriented Programming
• OOP is based on objects and classes, promoting the
organization of data and methods into objects that interact with
each other.
• It is based on the concept of object.
• An object contains data in the form of fields that are known as
attributes and the procedures are known as methods.
• programs are divided into what are known as objects.
• It follows the bottom-up flow of execution.
• It introduces concepts like data abstraction, inheritance, and
overloading of functions and operators overloading.
• Example: Java, C++, Python, C#, Perl, Kotlin, Ruby

10
Cont’d …
Example:
class Dog {
String name;

void bark() {
System.out.println(name + " barks!");
}
}
public class OOPEx {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.bark();
}
}
11
Cont’d …

Some Advantages
of OOP Paradigms

12
Java
 What is Java?

 What is the difference between java and C++

• Java is not only a Programming language but also a programming


atmosphere to develop and deploy enterprise applications.

13
Cont’d …
 Java:
• Programming language and a platform.
• Developed by sun microsystems (James Gasoling)
• a high level, robust, secured and object-oriented programming language.
• based C/C++
• widespread acceptance
 Platform:
• Any hardware or software environment in which a program runs, is known
as a platform.
• Since Java has its own runtime environment (JRE) and API, it is called
platform.

14
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
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games etc.
15
Types of Java Applications
 There are mainly 4 type of applications that can be created using java
programming:
1. Standalone Application
• It is also known as desktop application or window-based application.
• An application that we need to install on every machine such as media
player, antivirus etc.
• AWT and Swing are used in java for creating standalone applications.
2. Web Application
• runs on the server side and used to create dynamic pages
• Currently, servlet, JSP, struts, JSF etc. technologies are used for creating
web applications in java.
16
Cont’d …
3. Enterprise Application
• An application that is distributed in nature, such as banking
applications etc.
• It has the advantage of high level security, load balancing and
clustering.
• In java, EJB is used for creating enterprise applications.

4. Mobile Application
• An application that is created for mobile devices.
• Currently Android and Java ME are used for creating mobile
applications.
17
History of Programming

ALGOL International group 1960

BCPL Created by Martin Richards in 1967

B Created by Ken Thompson in 1970

Created by Dennis Ritchie in 1972


C

C++ It is an Extension of C developed by Bjarne in


1980Created by Dennis Ritchie in 1972

Java A futures of C++ and developed in 1991 by James


Gasoling
18
Cont’d …
 ALGOL:
• Algol is a computer programming language

• The root of all modern languages ( introduce in 1958).

• Named for the algorithmic process of definition of a


programming problem.

• Short for Algorithmic Language.

• ALGOL uses a structure programming.

• ALGOL is popular in Europe

19
Cont’d …
 BCPL:
• In 1967, Created by Martin Richards B:
in 1967 • Created by Ken Thompson In
• Basic Combined Programming 1970
Language) • created for UNIX OS at Bell
• Primarily BCPL is developed for Laboratories.
system software • Both BCPL and B were “type
• Operating systems and less” languages
compilers • It was derived from BCPL
• Designed for primarily non-
• high portability.
numeric applications.
• It is the successor to
the CPL programming language.

20
History of C

• During 1970 Dennis Ritchie created C


Programming language to develop the UNIX
operating system at Bell Labs.

• C is a general-purpose, high-level language.

• C was originally first implemented on the


PDP-11 computer in 1972.

Dennis Ritchie
• He is an American computer scientist.
• He created the C programming language and, with
long-time-colleague ken Thompson, the UNIX OS.

21
Cont’d …
 C was evolved from ALGOL, BCPL and B.

 Added new features and concepts like “data types”.

 Developed along with the UNIX operating system.


• In 1983 American National Standards Institute (ANSI) appointed a technical
committee to define a standard for C.
• approved a version of C in December 1989 which is now known as ANSI C.

 In 1990 International Standards Organization (ISO) has approved C and


this version of C is referred to as C89.

22
History of C++
• C++ Development started in 1979.

• It is an extension of C language.

• During the creation of Ph.D. thesis,


Bjarne Stroustrup worked with language
called Simula.
Bjarne Stroustrup
• Simula is programming language
basically useful for the simulation work.

23
Cont’d …
 Simula was first language to support object-oriented programming
language (OOP).

 OOP is a formal programming approach that combines data and


associated actions (methods) into logical structures (objects).

 Bjarne Stroustrup identified that this OOP features can be included


in the software development, however the Simula language was
far too slow for practical use.

24
Cont’d …

 After that Bjarne Stroustrup started working on the C language and


added more extra OOP features to the classic C.

 He added features in such a fashion that the basic flavor of C


remains unaffected.

 C++ is an object oriented & hybrid language.


• Possible to program as a c programming language and as an Object
Oriented Programming style.

 In mid 2011 the new C++ standard C++11 was published.

25
History of Java
• Java started out as a research project.

• James Gosling is generally credited as the inventor


of the Java programming language.

• He was the first designer of Java and implemented


its original compiler and virtual machine.

• He is also known as the Father of Java. James Goslimg

26
Cont’d …

 Research began in 1991 as the Green Project at Sun


Microsystems, Inc.
 Research efforts birthed a new language, OAK.
 A tree outside of the window of James Gosling’s office
at Sun.

 Java is available as JDK (Java Development Kit) and it


is an open source software.

• Oak is a symbol of strength and chosen as a national


tree of many countries like U.S.A., France, Germany,
Romania etc.
27
Cont’d …

 Java language was created with 5 main goals:


• an object oriented.
• A single representation of a program could be executed on
multiple operating systems. (write once, run anywhere)
• fully support network programming.
• execute code from remote sources securely.
• easy to use.
 In 1995, Oak was renamed as "Java" because it was already a
trademark by Oak Technologies.
 Now Sun Microsystems is a subsidiary of Oracle Corporation.

28
Cont’d …

• Java is not an abbreviation.

• Java is name of
an island of Indonesia, south of Borneo,
from which it
is separated by the Java Sea.

• politically the most


important island of Indonesia

• Originally a kind of coffee grown on Jav


Java Logo
a and nearby islands of modern Indonesi
a.

29
Java Version History

 There are many java versions that has been released. Current stable
release of Java is Java SE 8.

• JDK Alpha and Beta (1995) • J2SE 1.4 (6th Feb, 2002)

• JDK 1.0 (23rd Jan, 1996) • J2SE 5.0 (30th Sep, 2004)

• JDK 1.1 (19th Feb, 1997) • Java SE 6 (11th Dec,


2006)
• J2SE 1.2 (8th Dec, 1998)
• Java SE 7 (28th July,
• J2SE 1.3 (8th May, 2000) 2011)
• Java SE 8 (18th March, 2014)

30
Java Platforms

There are three main platforms for Java:

 Java SE (Java Platform, Standard Edition)


• runs on desktops and laptops.

 Java ME (Java Platform, Micro Edition):


• runs on mobile devices such as cell phones.

 Java EE (Java Platform, Enterprise Edition)


• runs on servers.

31
Java Terminology
Java Development Kit
 It contains one (or more) JRE's along with the various development
tools like
• Java source compilers,
• Bundling and deployment tools,
• Debuggers, development libraries, etc.

32
Cont’d …

 Java Virtual Machine:


• An abstract machine architecture specified by the Java Virtual
Machine Specification.

• It interprets the byte code into the machine code depending upon
the underlying OS and hardware combination.

• JVM is platform dependent. (It uses the class libraries, and other
supporting files provided in JRE)

33
Cont’d …
 Java Runtime Environment:
• implements Java Virtual Machine, and provides all class libraries and
other facilities necessary to execute Java programs.
• This is the software on your computer that actually runs Java
programs.
• JRE = JVM + Java Packages Classes (like util, math, lang, awt, swing
etc ) + runtime libraries.

34
Java execution procedure

Class Loader Byte


Java Class
Java Source Code Verifier
Libraries

Java Byte Java


codes Just-in-time
Java Interpreter
move Compiler
Compiler
locally
Run Time System

Java
Byte codes Java OS W 32
Win Solaris

MAC Others

Hardware

35
Java Virtual Machine

 JVM is an abstract machine.


 It is called virtual machine because it doesn't physically exist.
 It is a specification that provides runtime environment in which java
bytecode can be executed.
 It can also run those programs which are written in other languages
and compiled to Java bytecode.
 Each kind of computer processor has its own set of executable
instructions, and each computer operating system uses the processor’s
instructions in a slightly different way.

36
Cont’d …

 JVM, JRE and JDK are platform dependent


 There are three notions of the JVM:-
• Specification
• Implementation, and
• Instance.

 The JVM performs following main tasks:-


• Loads code
• Verifies code
• Executes code
• Provides runtime environment

37
Cont’d …

38
Characteristics of Java

 The following are characteristics of Java


• Simple • Secure
• Object-Oriented • Architecture-Neutral
• Distributed • Portable
• Interpreted • High Performance
• Robust • Multithreaded
• Platform Independent • Dynamic

39
Simple Java program

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args)
{
System.out.println(“ Hello Ethiopia! ");
}
}

Output:
Hello Ethiopia!

40
Valid java main method signature
 public static void main(String[] args)
 public static void main(String []args)
 public static void main(String args[])
 public static void main(String... args)
 static public void main(String[] args)
 public static final void main(String[] args)
 final public static void main(String[] args)
 final strictfp public static void main(String[] args)

Invalid java main method signature


 public void main(String[] args)
 static void main(String[] args)
 public void static main(String[] args)
 abstract public static void main(String[] args)

41
Question
 Can you save a java source file by other name than the
class name?

 Yes: if the class is not public. It is explained in the figure


given below

To compile: javac Hard.java


To execute: java Simple
42
Cont’d …

 What is the meaning of System.out.println()?


• System is a class in Java’s java.lang package which is
the main (root) package in Java.

• out is a static member of System class and represents


output buffer.

• The keyword println is an overloaded function used


for printing the output.

43
Cont’d …

 Can we write a Java program without Class?


 No!
• Eevry Java program is going to have at least one class.
• The class that contains the main function is often referred to as the main
class. We always declare the main function inside a class.
• Why is string args used in Java?
• String args [] is used to pass string arguments to the main function.

44
Compiling Java Source Code
 Java was designed to run object programs on any platform.
 With Java, you write the program once, and compile the source
program into a special type of object code, known as bytecode.
 The byte-code can then run on any computer with a Java Virtual
Machine
 Java Virtual Machine is a software that interprets Java byte-code.
Java Byte code

JVM

any
Computer

45
Exercise
 Write a java program to print:

• “Your Name, Id number, and Department” Class Year and


Section on the command prompt ( use notepad)

• Compile the program

• Run the program

46
Question

47

You might also like