II – UNIT 1. Procedure Oriented Programming 2. Object Oriented Programming Paradigm 3. Basic Concepts of OOPS 4. Benefits of OOPS 5. Class fundamentals 6. Declaring Objects 7.Assigning Object References 8. Introducing Methods 9. Constructors 10. this keyword 11. Garbage Collection 12. finalize ( ) Method 13. Overloading Methods 14. Object as parameters 15. Returning Objects 16. Access Control 17. static and final keyword 18. Nested classes and Inner Class 19. Classes with Command Line arguments 20.Packages 21.Enums in Java 1 PROCEDURE ORIENTED PROGRAMMING 1. In Procedural programming, Programmer combines related sequences of statements into one single place, called procedure. 2. A procedure call is used to invoke the procedure. 3. After the sequence is processed, flow of control proceeds right after the position where the call was made. 4. But the approach in oops is that classes and objects are used to model real world entity taking help of methods which performs the functions. 5. This technique is also known as Top – down programming OBJECT ORIENTED PROGRAMMING PARADIGM 1. The major objective of object oriented approach is to eliminate some of the flaws encountered in the procedural approach. 2. OOP treats data as a critical element to the program development and does not allow it to flow freely around the system. 3. It ties data more closely to the function that operate on it an protects it2from unintentional modification by other functions. 4. OOP allows us to decompose a problem into a number of entities called Objects and then build data and functions (known as methods in Java) around these entities. 5. The combination of data and methods make up an object Methods Methods Data Methods Methods Object = Data + Methods 6. The data of an object can be accessed only by the methods associated with that object. 3 7. However, methods of one object can access the methods of other object. Procedural vs. Object-Oriented Procedural Withdraw, deposit, transfer Object-Oriented Customer, money, account 4 Procedural vs. Object-Oriented – Contd., • Real world Objects are mapped to software in OOPs • Objects in the problem domain are mapped to objects in software Questions • What are Objects? • What are Classes? • What are Messages? Answers • Objects reflect instances that embody those concepts. • Classes reflect concepts. • Messages are communication between objects. object Rita class Gita Sita Mita What are Objects? • “An object is a software bundle of variables and related methods” • Software objects are model real-world objects or abstract concepts • Real-world objects have states and behaviors – Dogs' states: name, color, breed, hungry – Dogs' behaviors: barking fetching • Software objects implement real-world objects by: – Using variables to implement states – Using methods to implement behaviors Visual Representation of a Software Object 8. Some of the features of object oriented paradigm are: • Emphasis is on data rather than procedure. • Programs are divided into what are known as Objects. • Data Structures are designed such that they characterize the objects. • Methods that operate on the data of an object are tied together in the data structure. • Data is hidden and cannot 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. 9. Our definition of object oriented programming is: Object oriented programming is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and function that can be used as templates for creating copies of such modules on demand. 10 BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING 1. Class 2. Objects 3. Data Abstraction 4. Data Encapsulation 5. Data Hiding 6. Inheritance 7. Polymorphism 8. Dynamic Binding 9. Message Communication 1. Class The entire set of data and code of an object can be made a user – defined data type using the concept of a class. A class may be thought of as a ‘data type’ and an object as a ‘variable’ of that data type. Once a class has been defined, we can create any number of objects belonging to that class. Each object is associated with the data of type class with which they are created. A class is thus a collection of objects of similar type. 2. Objects Objects are the basic runtime entities in an object – oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program may handle. They may also represent user – defined data types such as 11 vectors and lists. Any programming problem is analyzed in terms of objects and the nature of communication between them. What are Classes? • A class is a blueprint or prototype defining the variables and methods common to all objects of a certain kind. • An object is an instance of a certain class. • After you have created a class, you must create an instance of it before you can use. • The benefit of Classes: Reusability • A class captures the common properties of the objects instantiated from it • A class characterizes the common behavior of all the objects that are its instances 13 CLASS Example class Account { private String accountName; private double accountBalance; public withdraw(); public deposit(); public determineBalance() } 14 OBJECTS • Object-oriented is a way of looking at a Software System as a collection of interactive objects • Object is an instance of a class • Object represents an entity either physically, conceptual, or software. 15 OBJECTS • All the objects share the same attribute names and methods with other objects of the same class. • Each object has its own value for each of the attribute. 16 CLASSES / OBJECTS 17 OBJECTS 18 What goes inside a class 19 ADDING METHODS • A Class with only data fields has no life. Objects created by such a class cannot respond to any messages. • Methods are declared inside the body of the class but immediately after the declaration of data fields. The general form of a method declaration is: type methodName (Paramter-list) { method-body; } 20 ADDING METHODS TO CLASS CIRCLE 21 CREATING OBJECTS OF A CLASS 22 CREATING OBJECTS OF A CLASS 23 Accessing Object Data 24 EXECUTING METHODS ON OBJECT 25 BASIC PRINCIPLES OF OO Encapsulation Data Abstraction OOP Paradigm Inheritance Polymorphism 26 Encapsulation • It associates the code and the data it manipulates into a single unit; and keeps them safe from external interference and Encapsulation misuse OOP Paradig m Data Abstraction Inheritance Polymorphism 27 ENCAPSULATION • All information (attributes and methods) in an object oriented system are stored within the object/class. • Information can be manipulated through operations performed on the object/class – interface to the class. Implementation is hidden from the user. • Object support Information Hiding – Some attributes and methods can be hidden from the user 28 ENCAPSULATION - Example 29 Without Encapsulation? 30 With Encapsulation 31 INHERITANCE • New data types (classes) can be defined as extensions to previously defined type. • Parent Class ( Super Class ) –Child Class ( Encapsulation Sub Class) • Subclass inherits properties from the class OOP Paradig m Data Abstraction Inheritance Polymorphism 32 INHERITANCE - Example 33 INHERITANCE - Example 34 INHERITANCE - Example 35 Polymorphism Encapsulatio n OOP Paradi gm Data Abstraction Inheritance • An Object of type circle or rectangle can be assigned to a Shape object. The behavior of the object will depend on the object passed. Polymorphis m 36 POLYMORPHISM – Method Overloading 37 ENCAPSULATION Encapsulation OOP Paradigm Data Abstraction Inheritance Polymorphism 38 Inheritance mechanism is that it allows the programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the class in such a way that it does not introduce any undesirable side effects into the rest of the classes. In java, the derived class is known as ‘subclass’ Bird Attributes: Feathers Lay eggs Flying Bird Non FlyingBird 39 7. Polymorphism Polymorphism is another important OOP concept. Polymorphism means the ability to take more than one form. For example, an operation may exhibit different behaviour in different instances. The behaviour depends upon the types of data used in the operation. For example consider the operation of addition. For two number, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation. Consider the following example: Shape Draw() Circle Object Box Object Triangle Object Draw(circle) Draw(box) Draw(triangle) Polymorphism plays an important role in allowing objects having different interval structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ. Polymorphism is extensively used in 40 implementing inheritance. Network of objects communicating between them Object 1 Object 5 Object 2 Object 3 Object 4 41 Benefits of OOPS 1. Through inheritance, we can eliminate redundant code and extend the use of existing classes. 2. We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity. 3. The principle of data binding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. 4. It is possible to map objects in the problem domain to those objects in the program. 5. It is possible to have multiple objects to coexist without any interference. 6. It is easy to partition the work in a project based on objects. 7. The data – centered design approach enables us to capture more details of a model in an implementable form. 8. Object – Oriented systems can be easily upgraded from small to large systems. 9. Message passing techniques for communication between objects make the 42 interface descriptions with external systems much simpler. Application of OOPS 1. Real – time systems. 2. Simulation and modelling. 3. Object – oriented databases. 4. Hypertext, hypermedia and expertext. 5. AI and expert systems. 6. Neural networks and parallel programming. 7. Decision support. 8. Office automation systems. 9. CIM / CAD / CAM system. 43 The Java Buzzwords or Java Features or Java Characteristics ■ Simple ■ Secure ■ Portable ■ Object-oriented ■ Robust ■ Multithreaded ■ Architecture-neutral ■ Interpreted ■ High performance ■ Distributed ■ Dynamic 44 44 Methods Introductions 45 Introduction • Methods are nothing but function. • It is a set of statements which can be included in java class. • It can be called at any point in java class by simply calling method name. • Methods have so much of power and flexibility. • Methods should be declared in the class. • It can be public, private, and protected. • Methods are of two types, one is with argument and another is without argument. 46 Types of Methods • Methods are used to access the data fields in a class • These methods are linked with the objects • Class creates an object using new keyword, and this object can be used for calling the methods. • An instance method is called by prefixing it with an object 47 • Methods can be declared using various types such as 1. Return type method -returns value or does not return any values (void). 2. Modifiers – it is public private, and protected 3. Parameter – it should be listed in parentheses,(). 4. Method body – declare between braces • Method declaration • static - class method, no need to create class reference • final - method cannot be overridden • abstract - method is not implemented in another cl • ass/method 48 Empty Method class emptymethod { int i,j,k; void method1() { i=10; j=10; k=i*j; System.out.println(" Result k is:"+k);} public static void main(String args[]) { emptymethod e=new emptymethod(); e.method1(); }} • Output : • Result K is 100 49 Method using argument class argument { int i; String st; void meth(int a, String s) { int i=a; String st=s; System.out.println("Employee Id:"+i); System.out.println("Employee Id:"+st); } public static void main(String args[]) { argument obj=new argument(); obj.meth(5001,"Kumar"); }} 50 Methods using return keyword class example { int c; int add(int a, int b) { return a+b; } int subtract(int a, int b){ return a-b;} int multiply(int a, int b){ return a*b;} int division(int a, int b) { return a/b; }} class returnexa { public static void main(String[] args) { example obj= new example(); obj.add(10,20); System.out.println("Addition: " +obj.add(30,15)); System.out.println("Subtraction: " +obj.subtract(30,15)); System.out.println("Multiplication: " +obj.multiply(30,15)); System.out.println("Division: " + obj.division(30,15)); }} 51 CLASS • A Class represents a template for several objects that have common properties • A class defines all the properties common to the object – attributes and methods. • A Class is a set of attributes and operations that are performed on the attributes. • A Class is the blueprint from which individual objects are created. • An object is an instance of a class 52 53