Download
object oriented programming oop using java n.
Skip this Video
Loading SlideShow in 5 Seconds..
Object-Oriented Programming (OOP) Using Java PowerPoint Presentation
Download Presentation
Object-Oriented Programming (OOP) Using Java

Object-Oriented Programming (OOP) Using Java

1879 Views Download Presentation
Download Presentation

Object-Oriented Programming (OOP) Using Java

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Object-Oriented Programming (OOP) Using Java Chapter One Fundamental Programming Structures in Java

  2. Belay Kal Section IOverview of OOP

  3. Belay Kal Objectives • Learn about programing paradigm • structured Vs object-oriented paradigm • Understanding OOP concepts and features • Learn the basics of java programming language: • Tokens: Identifiers, Separators, Operators, keywords & Literals • Constants, variables and primitive data types • The main() method • Control statements: if, switch • Loop statements: while, do…while, for and Jumps in loops • Array & Strings in java

  4. Belay Kal Overview of programming • Programming paradigm is a way of conceptualizing what it means to perform computation and how tasks to be carried out and organized on a computer. • Structured Programing • Problem solving would involve the analysis of processes in terms of the procedural tasks carried out and the production of a system whose representation is based on the procedural flow of the processes. • data is separate from code. • programmer is responsible for organizing everything in to logical units of code/data

  5. Belay Kal • A procedural program is divided into functions, and (ideally, at least) each function has a clearly defined purpose & a clearly defined interface to the other functions in the program. Programs with Structural Programming • Unrestricted Access • Functions have unrestricted access to global data. • Real-World Modeling • Unrelated functions and data, the basics of the procedural paradigm, provide a poor model of the real world. • Difficult of Creating New Data Types • Traditional languages are not extensible because they will not let you create new data types.

  6. Belay Kal OOP Approach • A modern programming paradigm that allows the Programmer to model a problem in a real-world fashion as an object. • Major objective is to eliminate some of the unfavorable features encountered in the procedural approach. • Ties data more closely to the functions that operate on it and protects it from unintentional modification by other functions. • Allows us to decompose a problem into a number of entities called objectsand then build data and functions (methods) around them.

  7. Belay Kal Object-oriented paradigm features • Emphasis is on data rather than procedure • Programs are divided into what are known as objects • Methods that operate on the data of an object are tied together in the data structure • Data is hidden and can’t be accessed by external functions • Objects may communicate with each other through methods • New data and methods can be easily added whenever necessary • Follows bottom-up approach in program design • First, the individual base elements of a program are specified (Objects with their data structures and methods) • Then they are linked together to form larger subsystems (using algorithm) • Structured approach is top-down (algorithm comes first, then the data structure)

  8. Belay Kal Basic concepts of OOP Objects and Classes • Object: is a software bundle that has State and Behavior and itoccupies memory Example: dogs have states (name, color, hungry, breed) and behaviors (bark, fetch, and wag tail). • Software Objects are often used to model real-world objects. • Class: is the template or blueprint that defines the states and the behaviors common to all objects of a certain kind. • It is a collection of objects of similar type. • Classes are user-defined data types & behave like the built-in types of programming language.

  9. Belay Kal Message • Software objects interact and communicate with each other by sending messages to each other. • The process of programming in object-oriented language, therefore, involves the following three basic steps: • Creating classes that define objects and their behavior. • Creating objects from class definitions. • Establishing communication among objects. • A message for an object is a request for execution of a procedure, and therefore will invoke a method (procedure) in the receiving object that generates the desired result.

  10. OOP’s fundamental building blocks: • Data Abstraction and Encapsulation • Inheritance • Polymorphism

  11. Data Abstraction & Encapsulation • The wrapping up of data and methods into a single unit (called class) is known as encapsulation. • This insulation of the data from direct access by the program is called data hiding. • Abstraction refers to the set of representing essential features without including the background details or explanations • Classes use the concept of data abstraction, and they are known as Abstract Data Types (ADT) • They hide the private data structure details and communicate with the outside using public methods which act as interfaces

  12. Belay Kal • Inheritance • Is the process by which objects of one class acquire the properties of objects of another class. • It provides the idea of reusability • Polymorphism • Is the ability to take more than one form. • Plays an important role in allowing objects having different internal structures to share the same external interface. • Three types of polymorphism: • Overloading methods • Overriding methods, and • Dynamic method binding

  13. Overloaded methods: methods with the same name signature but either a different number of parameters or different types in the parameter list • Overridden methods are methods that are redefined within an inherited or subclass • They have the same signature and the subclass definition is used • Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at runtime. • Memory is allocated at runtime not at compile time.

  14. Belay Kal Object Orientation • is a set of tools and methods that enable software engineers to build reliable, user friendly, and maintainable, well documented, reusable software systems that fulfills the requirements of its users. • It is claimed that object-orientation provides software developers with new mind tools to use in solving a wide variety of problems. • A software system is seen as a community of objects that cooperate with each other by passing messages in solving a problem.

  15. Belay Kal Section IIBasics of Java Programing Language

  16. Belay Kal Java Overview • Java is a general purpose, object-oriented programming language developed by Sun Microsystems of USA in 1991. • Java applications are object code portable as long as a Java virtual machine is implemented for the target machine. • The java object-oriented programming framework promotes reusability of software and code. • The Java foundation class libraries provide for windowing and graphical user interface programming, network communications, and Multimedia facilities. Together, they demonstrate the practical and productive work done in Java.

  17. Belay Kal Java Features • Compiled and Interpreted • Java combines both these approaches thus makes it a two-stage system. • Java compiler translates the source code into bytecodeinstructions • Java interpreter generates the machine code that can be directly executed by the machine that is running the java program.

  18. Belay Kal Java Interpreter Just in Time Compiler How it Works? Compile-time Environment Run-time Environment Class Loader Bytecode Verifier Java Class Libraries Java Source (.java) Java Bytecodes move locally or through network Java Virtual machine Java Compiler Runtime System Java Bytecode (.class ) Operating System Hardware

  19. Belay Kal Java Virtual Machine (JVM) • Java compiler produces an intermediate code known as bytecode for a machine that does not exist. This machine is known as Java Virtual Machine (JVM) & it exists only inside computer memory. • It is a simulated computer within the computer and does all major functions of the real computer. • The JVM is responsible in interpreting bytecode to machine understandable code (Machine code). • Just In Time compiler (JIT),part of the JVM, is responsible for compiling code as it is needed, during execution.

  20. Belay Kal • Platform independent and Portable • “compile once and execute many times in many platforms” • Most significant contribution of java over other languages is its portability. i.e. Java programs can be easily moved from one computer system to another, anywhere and anytime. • Changes and upgrades in operating systems, processors and system resources will not force any changes in java programs. • Java ensures portability in two ways: • Java compiler generates bytecode instructions that can be implemented on any machine • The size of the primitive data types are machine-independent

  21. Belay Kal • Object-oriented • Java is true object-oriented programming language. • All program code and data reside within objects and classes. • Robust and Secure • It has strict compile time and run-time checking for data types. • It is designed as a garbage-collected language relieving the programmers virtually all memory management problems. • It also incorporates the concept of exception handling which captures serious errors and eliminates any risk of chasing the system. • Java systems ensure that no viruses are communicated within applet programs

  22. Belay Kal • Distributed • Java is designed as a distributed language for creating applications on networks. • It has the ability to share both data and programs. • Java applications can open and access remote objects on Internet as easily as they can do in a local system. • Simple, Small and Familiar • Java is a small and simple language even easier than C and C++. • Java doesn’t use pointers, preprocessor header files, goto statement and many others. To make the language look familiar to the existing programmers, java uses many constructs of C and C++.

  23. Belay Kal • Multithreaded and interactive • Multithreaded means handling multiple tasks simultaneously. • Java supports multithreaded programs: We don’t need to wait for the application to finish one task before beginning another task. • The java runtime comes with tools that support multiprocessor synchronization and construct smoothly running interactive systems. • Dynamic and Extensible • Java allocates memory at runtime not at compile time. • Java is capable of dynamically linking in new class libraries, methods, and objects. • Java program supports functions written in other languages such as C and C++. Such functions are know as native methods • Native methods are linked dynamically at runtime.

  24. Belay Kal Disadvantages of Java • Running bytecode through the interpreter is not as fast as running machine code, which is specific to that platform. • Because it is platform independent, it is difficult to use platform specific features (e.g., Windows taskbar, quick launch) in Java. • Java interpreter must be installed on the computer in order to run Java programs.

  25. Belay Kal Differences Between Java and C++ • Java does not support operator overloading. • Java does not have template classes as in C++. • Java does not support multiple inheritance. • This is accomplished using a new feature called “interface” • Java does not support global variables. • Every variable and method is declared within a class and forms part of that class. • Java does not use pointers • Java has replaced the destructor function with a finalize() function • There are not header files in Java.

  26. Belay Kal Java Environment • Java environment includes a large number of development tools and hundreds of classes and Methods. • The development tools are part of the system known as Java Development Kit (JDK) and the classes and methods are part of the JavaStandard Library (JSL),also known as Application Programming Interface (API). • JDKcomes with a collection of tools that are used for developing and running java programs: • appleviewer (for viewing java applets ) • javac (java compiler) • java (java interpreter) • javap (java disassembler) • javah (for C header files) • javadoc (for creating HTML documents) • jdb (Java debugger)

  27. Belay Kal Java API • It includes hundreds of classes and methods grouped into several functional packages. • Most commonly used packages are: • Language Support Package: a collection of classes and methods required for implementing basic features of java. • Utilities Package: a collection of classes to provide utility functions such as date and time functions. • Input/output Package: a collection of classes required for input/output manipulation. • Networking Package: a collection of classes for communicating with other computers via Internet. • AWT Package (Abstract Window Tool kit package): contains classes that implements platform-independent graphical user interface. • Applet Package: includes set of classes that allows us to create java applets.

  28. Belay Kal Section IIIOverview of Java Programing Language

  29. Belay Kal Introduction • Java is a general-purpose, object-oriented programming language. • We can develop two types of Java programs namely: • Stand-alone application • Web applets • Executing stand-alone Java program involves two steps: • Compiling source code into byte code javac compiler. • Executing the bytecode program using java interpreter. • Applets are small programs developed for Internet applications. • An applet located on a distant computer (server) can be downloaded via Internet and executed on a local computer (client) using a Java-capable browser.

  30. Belay Kal Compiler Two Ways of writing Java Programs

  31. Belay Kal Simple Java Program The simplest way to learn a new language is to write a few simple example programs and execute them. public class Sample { public static void main (String args []) { System.out.print(“Java is better than C++.”); } }

  32. Belay Kal • Let us discuss the program line by line: • Class Declaration: the first line class Sample declares a class, which is an object constructor. Classis keyword and declares a new class definition and Sample is a java identifier that specifies the name of the class to be defined. • Opening Brace “{“: Every class definition in java begins with an opening brace and ends with a closing brace “}”. • The main line: the third line public static void main(String args[]) defines a method named as main. • Is the starting point for the interpreter to begin the execution of the program.

  33. Belay Kal Note: • A java program can have any number of classes but only one of them must include the main method to initiate the execution. • Java applets will not use the mainmethod at all. • The third line uses a number of keywords: public,static and void • Public: is an access specifier that declares the main method as “unprotected” and therefore making it accessible to all other classes. • static: declares that this method as one that belongs to the entire class and not a part of any objects of the class. • Main must always be declared as static since the interpreter uses this method before any objects are created. • void : states that the main method does not return any value (but prints some text to the screen.)

  34. Belay Kal Contd. • All parameters to a method are declared inside a pair of parenthesis. Here, String args[ ] declares a parameter named args, which contains array of objects of the class type String. • The Output Line: The only executable statement in the program is System.out.println(“Java is better than C++.”); • This is similar to cout<< constructor of C++. • The printlnmethod is a member of the out object, which is a static data member of System class.

  35. Belay Kal Java Program Structure

  36. Belay Kal Documentation Section • comprises a set of comment lines giving the name of the program, the author and other details, which the programmer would like to refer at a later stage. • Java supports three types of comments: • Single line comment // • Multiple line comment /*……………… ………………*/ • Documentation comment /**….*/ • This form of comment is used for generating documentation automatically.

  37. Belay Kal Package Statement • Is the first statement in Java file and is optional. • It declares a package name and informs the compiler that the classes defined here belong to this package. Example: package student; import statements • Next to package statements (but before any class definitions) a number of import statements may exist. This is similar to #include statements in C or C++. • Using import statements we can have access to classes that are part of other named packages. Example: import java.lang.Math;

  38. Belay Kal interface Statements • An interface is like a class but includes a group of method declarations. • is also an optional section. • is used only when we wish to implement the multiple inheritance features in the program Class Definitions • A Java program may contain multiple class definitions. • Classes are the primary and essential elements of a Java program. • These classes are used to map objects of real-world problems. • The number of classes depends on the complexity of the problem.

  39. Belay Kal Main Method Class • Since every Java stand-alone program requires a main method as its starting point, this class is the essential part of a Java program. • A simple Java program may contain only this part. • The main method creates objects of various classes and establishes communications between them. • On reaching the end of main, the program terminates and control passes back to the operating system.

  40. Belay Kal Section IVJava Tokens

  41. Belay Kal Introduction • A class in java is defined by a set of declaration statements and methods containing executable statements. • Most statements contain expressions, which describe the actions carried out on data. • Smallest individual units in a program are known as tokens. • In simplest terms,a java program is a collection of tokens, comments, and white spaces. • Java has five types of tokens: Reserved Keywords, Identifiers, Literals, Separators and Operators .

  42. Belay Kal Keywords • Are essential part of a language definition and can not be used as names for variables, classes, methods and so on. • Java language has reserved 60 words as keywords.

  43. Belay Kal Identifiers • Are programmer-designed tokens. • Are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program. • Java identifiers follow the following rules: • They can have alphabets, digits, and the underscore and dollar sign characters. • They must not begin with a digit • Uppercase and lowercase letters are distinct. • They can be of any length.

  44. Belay Kal POP QUIZ Which of the following are valid Identifiers? • $amount • 6tally • my*Name • salary • score • first Name • total# • cast

  45. Belay Kal Literals • Literals in Java are a sequence of characters(digits, letters and other characters) that represent constant values to be stored in variables. • Five major types of literals in Java: • Integer Literals: refers to a sequence of digits (decimal integer, octal integer and hexadecimal integer) • Floating-point Literals • Character Literals • String Literals • Boolean Literals

  46. Belay Kal Separators • Are symbols used to indicate where groups of code are divided and arranged. • They basically define the shape and functions of our code. • Java separators include: • Parenthesis ( ) :- used to enclose parameters, to define precedence in expressions, surrounding cast types • Braces { } :- used to contain the values of automatically initialized arrays and to define a block of code for classes, methods and local scopes.

  47. Belay Kal Contd. • Brackets [ ] :- are used to declare array types and for dereferencing array values. • Semicolon ; :- used to separate statements. • Comma , :- used to separate consecutive identifiers in a variable declaration, also used to chain statements together inside a “for” statement. • Period . :- Used to separate package names from sub-package names and classes; also used to separate a variable or method from a reference variable.

  48. Belay Kal Operators • Are symbols that take one or more arguments (operands) and operates on them to a produce a result. • Are used to in programs to manipulate data and variables. • They usually form a part of mathematical or logical expressions. • Expressions can be combinations of variables, primitives and operators that result in a value.

  49. Belay Kal Java Operators • There are 8 different groups of operators in Java: • Arithmetic operators • Relational operators • Logical operators • Assignment operator • Increment/Decrement operators • Conditional operators • Bitwise operators • Special operators

  50. Belay Kal Arithmetic Operators • Java has five basic arithmetic operators • They all work the same way as they do in other languages. • We cannot use these operators on boolean type . • Unlike C and C++, modulus operator can be applied to the floating point data. • Order of operations (or precedence) when evaluating an expression is the same as you learned in school (BODMAS).