1 / 16

Object-Oriented Programming

Object-Oriented Programming. Object-oriented programming is a new way of programming. Since its early days, programming has been practiced using a number of various methodologies. At each new stage, a new approach was created to make

romanf
Download Presentation

Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming Object-oriented programming is a new way of programming. Since its early days, programming has been practiced using a number of various methodologies. At each new stage, a new approach was created to make programming easier and help the programmer handle more complex programs. At first, programmers had to write programs using laborious binary instructions and data with switches. Later, assembly languages were invented which allowed longer programs to be written. In the 1950s the first high-level language (Fortran) was invented. Using a high-level language like Fortran, a programmer could write a program with several thousand lines of code. But that method only allowed for unstructured programs: programs without any structure and very ad hoc. Object Oriented Programming in Java1

  2. Object-Oriented Programming 2 Later in 1960s, the need for structured programs became clear and languages like Algol, Pascal and C were introduced. C++ was invented in early 1980s and is also a structured language; it also supports object-oriented programming. Java is a pure object-oriented programming language. Structured programming relies on control structures, code blocks, procedures or functions and facilitates recursion. The main characteristic of structured programming is breaking programs into smaller parts. This in turn will help to write better, more structured and larger programs. Using structured programming an average programmer can write and maintain programs that are up 40,000-50,000 lines of code long. Object Oriented Programming in Java2

  3. Object-Oriented Programming 3 With structured programming you can write quite complex programs. But after a certain point even structured programming becomes very hard to follow. To write larger and more complex programs, a new programming approach was invented: object-oriented programming or OO for short. Object-oriented programming combines the best features of structured programming with some new powerful concepts that allows writing more complex and more organized programs. The main new concepts in OO are encapsulation, polymorphism and inheritance. Any programming language that supports these three concepts is said to be an OO programming language. Examples of OO programming languages are C++, Java, Smalltalk…. Unlike C++, Java is a pure OO programming language. Object Oriented Programming in Java3

  4. Object-Oriented Programming 4 Object-oriented programming encourages programmers to break problems into related subgroups. Each subgroup becomes a self-contained object with its own instructions and data. So OO programs consist of objects. An object is similar to an ordinary variable but with its own member functions. Writing large programs is made a lot easier using objects. Each object is a self-contained entity. It is an autonomous entity that can be used and reused in other programs. This also allows for composition of objects to create more complex programs. It’s like the automobile manufacturing business where factories compose new cars out of pre-built parts (objects). These parts or objects may be manufactured by different companies. When a component or part becomes out-of-order or out-of-date, that component is replaced by a new one. Maintenance becomes much easier. Object Oriented Programming in Java4

  5. Object-Oriented Programming 5 In procedural programming languages such as C and Pascal, programming tends to be action-oriented whereas in OO programming languages such as Java and Smalltalk, programming object-oriented. In action-oriented programming, the focus is on actions or functions; in OO programming, the focus is on objects. Suppose ob is an object: print(ob); //focus is function/action ob.print(); //focus is object Most things in the world are classified: a class of students, a class of fish, a class of birds, a class of objects. The word ‘class’ means a type or a group of things which have some common attributes or properties. In most object oriented programming languages, the construct class is used to create a new type or class of objects. Definition: A class is a collection of data, stored in named fields, and code, organized into named methods, that operates on that data. Object Oriented Programming in Java5

  6. Introduction to Encapsulation Encapsulation is the binding together of code and data and keeping both safe from outside interference and misuse. When code and data are bound together like this an object is created. Inside an object, code and data may be private or public to that object. Private data or code is known and accessible to other parts of the object only. So other parts of your program cannot access the private data or code of an object without permission from the object. The object dictates or determines how its private data and functions (code) should be accessed and used. When code or data is public to an object, then it is possible for other parts of your program to access that code/data in the normal way. Usually, the public code of the object is used to provide a controlled way of accessing the private parts of the object. Object Oriented Programming in Java6

  7. Introduction to Polymorphism Polymorphism is the mechanism which allows one name to be used for two or more related but technically different purposes. Earlier on we saw overloading of methods which is an example of polymorphism. The general concept of polymorphism is “one interface, multiple methods”. In other words, you use the same method or mechanism to perform a group of related tasks. As we saw with method over-loading, polymorphism helps reduce complexity. Method overloading is one type of polymorphism, where the same method name is used to perform a number of different but related tasks. Another type of polymorphism is achieved using inheritance. This is the more interesting type and we will look at it a bit later. Object Oriented Programming in Java7

  8. Introduction to Inheritance Inheritance is another important feature of OO programming. With inheritance an object can acquire or inherit the properties of another object. The object that inherits another object acquires all the properties of the parent object and can add its own extra features specific only to itself. Inheritance provides for hierarchical classification which is very important in making information manageable. For example, a square is a kind of rectangle; in turn, a rectangle is a kind of closed geometric shape; in turn, a closed geometric shape is a kind of geometric shape. In each case, the child object inherits all the properties of the parent object and adds some extra features specific to itself. Inheritance is probably the most characteristic feature of OO programming and it’s very important. Object Oriented Programming in Java8

  9. Objects & Classes A class is like a factory for creating objects. We have already used a number of pre-defined classes? You can create your own classes. The fields and methods of a class are called members of the class. The members of a class can be of two types: static or class members associated with the class itself and instance members associated with individual instances of the class (i.e., with objects): - class fields - class methods - instance fields - instance methods An example: public class Circle { //a class/static field public static final double PI=3.14159; //a class/static method public static double radiansToDegrees(double rads){ Object Oriented Programming in Java9

  10. Objects & Classes 2 return rads * 180 / PI; } public double r; //an instance field //two instance methods public double area() { return PI * r * r; } public double circumference(){ return 2 * PI * r; } } The static modifier says that the field is a class field. The final modifier says that this field does not change. The public modifier says that this field can be used by any code; it’s a visibility modifier which we will cover in more detail later. Object Oriented Programming in Java10

  11. Objects & Classes 3 It’s important to know that a class filed is associated with the class itself and not with any instances/objects of that class and that there is only a single copy of it. This field can be accessed inside the class by writing its name only PI, and to access it from outside the class you must write Circle.PI. A class filed is similar to a global variable; it is accessible from all parts and methods of that class. Class methods are declared with the static modifier. Like class fields, they can be called from within the class by just writing its name, but it’s good programming style to specify the class name in which it is a member of, to make it clear that a class method is being invoked. Class methods can use class fields or other class methods but they cannot use any instance fields or methods. Why? Object Oriented Programming in Java11

  12. Objects & Classes 4 Any field declared without the static modifier is an instance field: public double r; Instance fields are associated with instances or objects of the class; each instance of the class has its own copy of an instance field. Any method not declared with the static modifier is an instance method. Instance methods operate on instances of the class, not on the class itself. Instance methods have access to both class fields and class methods. Instance methods operate on class instances or objects; they know which object they operate on because they are passed an implicit reference to the object they operate on; this reference is called this. The instance method area could have been written as: public double area(){ return Circle.PI * this.r * this.r; } Object Oriented Programming in Java12

  13. Constructors A constructor is a special method that is automatically called every time you create a new object. Constructors are used to initialize objects. Since constructors are called automatically, rather than manually, the programmer is relieved from having to initialize objects. A constructor method has the same name as the class name and returns no values. To create and initialize an object: Circle c=new Circle(); The operator new creates a new object of type Circle. In the class Circle defined earlier, no constructors were written; Java provided a default constructor that takes no parameters and performs no initialization. It is always better to specify a constructor for every new class you define to specify how a new object of that class would be initialized: public Circle(double r) {this.r=r;}//why this.r? Object Oriented Programming in Java13

  14. Constructors 2 To create an instance/object of type Circle: Circle c=new Circle(5.0); //object creation & //initialization on one line When writing a constructor for a class: - the constructor name is always the same the class name - the constructor has no return values but may take parameters - the constructor should perform initialization of the new object To provide flexibility in initializing a new object, often multiple constructors are defined: public Circle() { r=1.0;} public Circle(double r) { this.r=r;} The two constructors must have different parameter lists or signatures (method overloading), otherwise ? Object Oriented Programming in Java14

  15. Constructors 3 One important thing about having multiple constructors is that always create a default constructor. If you need a constructor, make sure you have defined a default constructor. If you don’t define any constructors for your class, then Java provides a default one that does nothing. One of the uses of the this keyword is to invoke a constructor from within another constructor: public Circle(double r) { this.r=r;} public Circle() { this(1.0);} Instance fields and class fields are initialized by default: Variables of types char, byte, short, int, long, float, and double are initialized to 0, variables of type boolean are initialized to false and reference-type variables are initialized to null. But local variables (declared inside methods) are not initialized by default. You should initialize them before use. Object Oriented Programming in Java15

  16. Notes on Set and Get Methods As you know, the class’s private fields can be manipulated only by methods of that class. Set and Get methods vs. public data It would seem that providing set and get capabilities is essentially the same as making the instance variables public. This is subtlety of Java that makes the language so desirable for software engineering. A public instance variable can be read or written by any method that has a reference to an object that contains the instance variable. If an instance variable is declared private, a public get method certainly allows other methods to access the variable, but the get method can control how other methods can access the variable. A public set method can- and should- carefully scrutinize attempts to modify the variable’s value to ensure that the new value is appropriate for that data item. Object Oriented Programming in Java16

More Related