Docsity

Object-Oriented Programming: A Comprehensive Overview of Programming Paradigms, Thesis of Java Programming

Toggle sidebar
bg1
1
Chapter One: - Introduction
Programming paradigm overview
A programming paradigm is a philosophy, style, or general approach to writing code. Most definitions of
the term are so broad as to be fairly useless—the term tends to make more sense when discussing specific
paradigms. Here, we’ll be comparing three specific paradigms: Imperative, Structured, Declarative,
Functional, Logical, Event-driven and Object-oriented.
The main programming paradigms;
a) Imperative Programming
If you’ve done programming in Python or C, you’ve used imperative programming. Imperative
programming defines the solution to a problem as a series of steps—first do this, then do that, then
do the next thing, and so on. The computer steps through each line of code, executing it and moving
on to the next step. Programs written in the imperative style often resemble recipes—first crack the
eggs, then mix in the flour, then add water. Imperative programs often change the state of the program
on each line, assigning new variables and referring to or changing old ones. Though intuitive for
solving small problems, imperative programs quickly become unmanageable as they become larger.
i. Control flow in imperative programming is explicit:
ii. Commands show how the computation takes place, step by step.
iii. Each step affects the global state of the computation.
iv. First do this and next do that
v. Sample code
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Object-Oriented Programming: A Comprehensive Overview of Programming Paradigms and more Thesis Java Programming in PDF only on Docsity!

Chapter One: - Introduction

Programming paradigm overview

A programming paradigm is a philosophy, style, or general approach to writing code. Most definitions of the term are so broad as to be fairly useless—the term tends to make more sense when discussing specific paradigms. Here, we’ll be comparing three specific paradigms: Imperative, Structured, Declarative, Functional, Logical, Event-driven and Object-oriented.

The main programming paradigms;

a) Imperative Programming If you’ve done programming in Python or C, you’ve used imperative programming. Imperative programming defines the solution to a problem as a series of steps—first do this, then do that, then do the next thing, and so on. The computer steps through each line of code, executing it and moving on to the next step. Programs written in the imperative style often resemble recipes—first crack the eggs, then mix in the flour, then add water. Imperative programs often change the state of the program on each line, assigning new variables and referring to or changing old ones. Though intuitive for solving small problems, imperative programs quickly become unmanageable as they become larger. i. Control flow in imperative programming is explicit : ii. Commands show how the computation takes place, step by step. iii. Each step affects the global state of the computation. iv. First do this and next do that v. Sample code

b) Structured Programming is (sometimes known as modular programming ) a kind of imperative programming where the control flow is defined by nested loops, conditionals, and subroutines, rather than via gotos. It enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Certain languages such as Ada, Pascal, and dBASE are designed with features that encourage or enforce a logical program structure. Program flow follows a simple hierarchical model that employs looping constructs such as "for," "repeat," and "while." Use of the "Go To" statement is discouraged. In this kind of programming variables are generally local to blocks. Example; result = []; for (i = 0; i < length(people); i++) { p = people[i]; if length(p.name)) > 5 { addToList(result, toUpper(p.name)); } } return sort(result); What are the main features of Structural Programming language?

  1. Division of Complex problems into small procedures and functions.
  2. No presence of GOTO Statement
  3. The main statement includes – If-then-else, Call and Case statements.
  4. Large set of operators like arithmetic, relational, logical, bit manipulation, shift and part word operators.
  5. Inclusion of facilities for implementing entry points and external references in program. What is difference between Structured and Unstructured (Imperative) Programming Language?
  6. The main difference between structured and unstructured programming language is that a structured programming language allows a programmer to dividing the whole program into smaller units or modules. But in unstructured programming language, the whole program must be written in single continuous way; there is no stop or broken block.
  7. Structured Programming language is a subset of Procedural Programming language. But in unstructured Programming language no subset exists.

where length(name) > 5 order by name d) Functional Programming Functional programs deal with state by avoiding it as much as possible. Solutions are defined as a series of functions that pass values to one another, leading to a series of transformations. Parts of the program dealing with state, if any, tend to be isolated. A goal of functional programming is predictability, based primarily on the fact that functions, if given a certain input, should always return the same output. Tips x. Control flow is expressed by combining function calls, rather than by assigning values to variables. xi. The real power of this paradigm comes from passing functions to functions (and returning functions from functions). xii. With functional programming; (1) Code is much shorter, less error-prone, and much easier to prove correct (2) There is more inherent parallelism, so good compilers can produce faster code (3) Evaluate an expression and use the resulting value for something (4) Example let( f, fun( people, if(equals(people, emptylist), emptylist, if(greater(length(name(head(people))), 5), append(to_upper(name(head(people))), f(tail(people))), f(tail(people))))), sort(f(people)))

e) Logic programming Logic programming is a way of writing computer programs using languages that are based on formal logic. Logic is the study of how truth is defined, and how we prove that certain statements are true or false. One way to look at a program is as a series of statements about the world with the goal of determining whether they are true. In that sense, it is an example of declarative programming: instead of instructing the computer exactly how to transform data, you provide the rules that govern the world described by your data. These rules are often given by premises (things assumed to be true) and consequences (things that happen if the premises are true). Logic programming languages (like Prolog) use a notation that replicates many aspects of First-order logic, in which we establish relations between known facts (thus creating a knowledge base) and later write rules that the interpreter or compiler can translate to a plan for execution in the Warren Abstract Machine, a virtual machine that understands and resolves logical statements. Logic programming has a number of advantages and disadvantages, and here are some of them: Advantages : ▪ Logic programming can be used to express knowledge in a way that does not depend on the implementation, making programs more flexible, compressed and understandable. ▪ It enables knowledge to be separated from use, ie the machine architecture can be changed without changing programs or their underlying code. ▪ It can be altered and extended in natural ways to support special forms of knowledge, such as meta-level or higher-order knowledge. ▪ It can be used in non-computational disciplines relying on reasoning and precise means of expression. Disadvantages : ▪ Initially, due to insufficient investment in complimentary technologies, users were poorly served.

g) Object Oriented Programming Object-oriented programming deals with state by designating certain functions that operate specifically on that state. Objects in object-oriented programming are a combination of state, or data, with functions that work specifically on that data. Rather than isolate state from the rest of the program, the object-oriented approach allows only certain parts of the program to operate on certain pieces of data. i. OOP is based on the sending of messages to objects. ii. Objects respond to messages by performing operations. iii. Messages can have arguments, so "sending messages" looks a lot like calling subroutines. iv. “Send messages between objects to simulate the temporal evolution of a set of real-world phenomena” Example result = [] for p in people { if p.name.length > 5 { result.add(p.name.toUpper); } } return result.sort;

Overview of OO principles

Object-Oriented Programming (OOP) is the term used to describe a programming approach based on objects and classes. The object-oriented paradigm allows us to organize software as a collection of objects that consist of both data and behavior. This is in contrast to conventional functional programming practice that only loosely connects data and behavior. Why has object-oriented programming become the preferred approach for most software projects? i. OOP offers a new and powerful way to cope with complexity. ii. Instead of viewing a program as a series of steps to be carried out, it views it as a group of objects that have certain properties and can take certain actions.

iii. It results in programs that are clearer, more reliable, and more easily maintained. Problems with Structured Programming No matter how well the structured programming approach is implemented, large programs become excessively complex. i. First, functions have unrestricted access to global data. I, e. Global data can be accessed by any function in the program. ii. Second, unrelated functions and data. The basis of the procedural paradigm, provide a poor model of the real world. In a large program, there are many functions and many global data items. This leads to an even larger number of potential connections between functions and data, as shown in the following figure This large number of connections causes problems in several ways. i. It makes a program’s structure difficult to conceptualize. ii. It makes the program difficult to modify. A change made in a global data item may necessitate rewriting all the functions that access that item. The more important problem with the procedural paradigm is that its arrangement of separate data and functions does a poor job of modeling things in the real world. In the physical world we deal with objects such as people and cars. Such objects aren’t like data and they aren’t like functions. Complex real-world objects have both attributes and behavior. Examples of attributes (sometimes called characteristics) are, for people, eye color and job title; and, for cars, horsepower and number of doors.

The public, private, protected, and default access modifiers define the scope of the field in the application. The Java Language Specification (JLS) defines the rules of visibility of data for all fields. These rules define under what circumstances you can access the data in these fields.

  • Methods Methods are procedures associated with a class. Like a field, a method can be declared as static, in which case it can be called globally. If a method is not declared as static, it means that the method is an instance method and can be called only as an operation on an object, where the object is an instance of the class. Similar to fields, methods can be declared as public, private, protected, or default access. This declaration defines the scope in which the method can be called. Objects The fundamental idea behind object-oriented languages is to combine into a single unit both data and the functions/methods that operate on that data. Such a unit is called an object, it is an instance of a class, and it is just a thing, an item, or a noun. For example, a car, a house, an employee of a company, a window that opens on the computer screen, or a TCP/IP socket connection between two computers. An object consists of two major components: i. The attributes of an object are what the object consists of, and ii. The behaviors of the object are what the object does I. Inheritance Inheritance is a fundamental principle of object-oriented programming. It allows a class to "inherit" (behavior or characteristics) of another, more general class. For example, a lion belongs to the biological family of cats (Felidae). All cats that have four paws, are predators and hunt their prey. This functionality can be coded once in the Felidae class and all its predators can reuse it – Tiger , Puma , Bobcat , etc. Inheritance is described as is-kind-of relationship , e.g. Tiger is kind of Animal. Object-oriented programming allows you to define new classes from existing classes. This is called inheritance. The principle in this sort of division is that each subclass shares common characteristics with

the class from which it’s derived. E.g. Cars, trucks, buses, and motorcycles all have wheels and a motor; these are the defining characteristics of vehicles. In addition to the characteristics shared with other members of the class, each subclass also has its own particular characteristics: For instance: Buses have seats for many people, while trucks have space for hauling heavy loads. An example of inheritance hierarchy among different types of students. II. Encapsulation Encapsulation is the idea that the attributes of an entity are enclosed in that entity. This gives context to attributes. This also allows the programmer to restrict access to those attributes so that those attributes are modified and/or used only in ways that the programmer intends to use them. In Java, the basis of encapsulation is the class. When you create a class, you will specify the code and data that constitute that class. Collectively, these elements are called members of the class. Specifically, the data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods. Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the complexity of the implementation inside the class. Each method or variable in a class may be marked private or public.

  • The public interface of a class represents everything that external users of the class need to know, or may know.

III. Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” It is the compiler’s job to select the specific action (that is, method) as it applies to each situation.

Overview of Java

Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1. [J2SE]). The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications. The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is guaranteed to be Write Once, Run Anywhere. Java is a full-fledged and powerful language that can be used in many ways. It comes in three editions: i. Java Standard Edition (Java SE) to develop client-side applications. The applications can run standalone or as applets running from a Web browser. ii. Java Enterprise Edition (Java EE) to develop server-side applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF). iii. Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell phones. Java is −

  • Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
  • Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
  • Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
  • Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
  • Architecture-neutral − Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.
  • Portable − Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
  • Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.
  • Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.
  • Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.
  • High Performance − With the use of Just-In-Time compilers, Java enables high performance.
  • Distributed − Java is designed for the distributed environment of the internet.
  • Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time. Oracle releases each version with a Java Development Toolkit (JDK). 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.

JVM Architecture

  • Class Loader: Class loader loads the Class for execution.
  • Method area: Stores pre-class structure as constant pool.
  • Heap: Heap is in which objects are allocated.
  • Stack: Local variables and partial results are store here. Each thread has a private JVM stack created when the thread is created.
  • Program register: Program register holds the address of JVM instruction currently being executed.
  • Native method stack: It contains all native used in application.
  • Executive Engine: Execution engine controls the execute of instructions contained in the methods of the classes.
  • Native Method Interface: Native method interface gives an interface between java code and native code during execution.
  • Native Method Libraries: Native Libraries consist of files required for the execution of native code.

Difference between JDK and JRE

Java Runtime Environment (JRE): The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications. Java Development Kit (JDK) The JDK also called Java Development Kit is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.

Generally, Java source code is compiled into Java bytecode and Java bytecode is interpreted by the JVM. Your Java code may use the code in the Java library. The JVM executes your code along with the code in the library. To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM, which is an interpreter. It translates the individual instructions in the bytecode into the target machine language code one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated. The following command runs the bytecode for the above java code java Welcome Caution

  • Do not use the extension .class in the command line when executing the program. Use java ClassName to run the program. If you use java ClassName.class in the command line, the system will attempt to fetch ClassName.class.class.
  • If you execute a class file that does not exist, a NoClassDefFoundError will occur.
  • If you execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead of main), a NoSuchMethodError will occur. The Java program-development process consists of repeatedly creating/modifying source code,

Compiling, and Executing programs. i. Java source code is translated into bytecode. ii. Java bytecode can be executed on any computer with a Java Virtual Machine.