1 / 35

CS213 : PROGRAMMING LANGUAGE II

CS213 : PROGRAMMING LANGUAGE II. Lecture 2 : Introduction to OOP. Lecture Contents. Object Oriented Programming What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class. Need for OOP Paradigm.

jaimiec
Download Presentation

CS213 : PROGRAMMING LANGUAGE II

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. CS213:PROGRAMMING LANGUAGEII Lecture 2 : Introduction toOOP

  2. LectureContents • Object Oriented Programming • What is aclass? • Encapsulation • Classbasics: • Data • Methods • Objects • Defining and using aclass

  3. Need for OOP Paradigm • OOP is an approach to program organization and development, which attempts to eliminate some of the drawbacks of conventional programming methods by incorporating the best of structured programming features with several new concepts. • OOP allows us to decompose a problem into number of entities called objects and then build data and methods (functions) around these entities. • The data of an object can be accessed only by the methods associated with the object.

  4. Object-Oriented Paradigm Some of the Object-Oriented Paradigm are: • Emphasis is on data rather than procedure. • Programs are divided into 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 can not be accessed by external functions. • Objects may communicate with each other through methods.

  5. What is OOP? • One of the ideas of object-oriented software is to organized software in a way that matches the thinking style of our object-oriented brains. you create a model for a real world system. • Instead of machine instructions that change bit patterns in main storage, we want "things" that "do something." • Definition of an Object-Oriented Program: • A collection of one or more computer-coded classes designed to complete a specific task

  6. Object Oriented Paradigm: Features and Java support Encapsulation Data Abstraction Single Inheritance OOP Paradigm Java Polymorphism Multiple Inheritance

  7. What OOP Allows You? • You analyze the objects with which you are working (attributes and tasks on them) • You pass messages to objects, requesting them to take action • The same message works differently when applied to the various objects • A method can work with different types of data, without the need for separate method names • Objects can inherit traits of previously created objects • Information can be hidden better

  8. Advantages of OOP approach • Modularity • The source code of the class can be written and maintained independently of the source code for other classes. • 2.Reusability • It is easy to re-use the class in other programs, once the class has been written once , it never needs to be re-written. • 3.Information hiding • A class has a public interface that other classes can use to communicate with it, but the class can maintain private information.

  9. Definition of a Class • A description of a group of objects with similar properties, common behavior, common relationships. • A template from which objects are created, or instantiated • Class is an abstract representation of a particular type of object • The same class may be used multiple times within a program to create multiple, distinct objects, each with a different name, but each having the same basic form or structure . • A class has two parts(the fields and the methods ): • Fields describe what the class is. • Methods describe what the class does.

  10. class FIGURE Ob1 Ob3 Ob2 CIRCLE SQUARE RECTANGLE Classes • Class is blue print or an idea of an Object • From One class any number of Instances can be created • It is an encapsulation of attributes and methods • Classes are Objects with the same attributes and behavior. Different objects of the same class have the same fields and methods, but the values of the fields will in general differ. • Instantiation : Instantiation is the process by which individual objects are created.

  11. What is an Object? • Objectsare the key concept to understanding object-oriented technology. • Object is a real world thing that is distinguishable from each other. • Object is a specific instance of a class with particular values for the fields. • Any object has two parts : • 1.State or attributes (variables ) • 2.Behaviour (methods) • + Provide the interface between an object and the outside world.

  12. Examples of Classes and Objects • Class: Student • Objects: Sara, Mona ,Hager,... • Class: Teachers • Object: Nagla, Reem, Noha,... • Class: Building • Object: riyadbank, SU, Durma

  13. Instance • Instance is an Object of a class which is an entity with its own attribute values and methods. • Creating an Instance • ClassNamerefVariable; • refVariable= new Constructor(); • or • ClassNamerefVariable = new Constructor();

  14. Java Class Hierarchy • In Java, class “Object” is the base class to all other classes • If we do not explicitly say extends in a new class definition, it implicitly extends Object • The tree of classes that extend from Object and all of its subclasses are is called the class hierarchy • All classes eventually lead back up to Object • This will enable consistent access of objects of different classes.

  15. Inheritance • Methods allows to reuse a sequence of statements • Inheritance allows to reuse classes by deriving a new class from an existing one. • The existing class is called the parent class, or superclass, or base class • The derived class is called the child class or subclass. • The child class inherits characteristics of the parent class(i.e the child class inherits the methods and data defined for the parent class)

  16. Animal Bird weight : int + getWeight() : int Inheritance • Inheritance relationships are often shown graphically in a class diagram, with the arrow pointing to the parent class + fly() : void

  17. Method Overriding. • There may be some occasions when we want an object to respond to the same method but have different behavior when that method is called. • That means, we should override the method defined in the superclass. This is possible by defining a method in a sub class that has the same name, same arguments and same return type as a method in the superclass. • Then when that method is called, the method defined in the sub class is invoked and executed instead of the one in the superclass. This is known as overriding.

  18. Queue Object Enqueue Is Full Data State Is Empty Dequeue Initialize Data Abstraction and Behavioral • Data Abstractions organize data. • Behavioral Abstractions combine procedural and data abstractions. StudentType Name (string) Marks (num) Grade (char) Student Number (num)

  19. Encapsulation • The bundling of data and procedures(methods) into a single unit(called class). • Class : various data elements and member functions are wrapped up together. • Main feature of object orientedprogramming Encapsulation

  20. Summary of OOPS The following are the basic oops concepts: They are as follows: • Objects. • Classes. • Data Abstraction. • Data Encapsulation. • Inheritance. • Polymorphism. • Dynamic Binding. • Message Passing.

  21. Example: Class Usage class Box { double width; double height; double depth; } class BoxDemo { • public static void main(String args[]) { • Box mybox = new Box(); • double vol; • mybox.width= 10; • mybox.height= 20; • mybox.depth= 15; • vol= mybox.width * mybox.height * mybox.depth; • System.out.println("Volume is " + vol); } }

  22. Object Destruction • A program accumulates memory through its execution. • Two mechanism to free memory that is no longer need by the program: 1) manual – done in C/C++ 2) automatic – done in Java • In Java, when an object is no longer accessible through any variable, it is eventually removed from the memory by the garbage collector. • Garbage collector is parts of the Java Run-Time Environment.

  23. Garbage Collection Garbage collection is a mechanism to remove objects from memory when they are no longer needed. Garbage collection is carried out by the garbage collector: The garbage collector keeps track of how many references an object has. It removes an object from memory when it has no longer any references. Thereafter, the memory occupied by the object can be allocated again. The garbage collector invokes the finalize method.

  24. finalize() Method • A constructor helps to initialize an object just after it has been created. • In contrast, the finalize method is invoked just before the object is destroyed: • 1) implemented inside a class as: protected void finalize() { … } • 2) implemented when the usual way of removing objects from memory is insufficient, and some special actions has to be carried out

  25. TheUnified Modeling Language(UML) • 13 • can be defined as a modeling language to capture the architectural, behavioral and structural aspects of asystem. • The UML has an important role in objectoriented • analysis anddesign. • Objects are the key to this object oriented world The basic requirement of is to identify the object efficiently.

  26. UML Classdiagrams • 14 • Class diagrams describes the objects in a system and their relationships, so it is widely used by the developercommunity. • The UML representation of a class is arectangle • containing three compartments stackedvertically. • The top compartment shows the class'sname. • The middle compartment lists the class's attributes. • The bottom compartment lists the class's operations.

  27. Examples 15 What about astudent??

  28. T hestudent class -Exercise Draw UML class diagram for class named Student where data is: name, UnivID, date of birth, department, level,GPA Methods are: PrintData()

  29. Controlling Access toMembers • Accessmodifiers • Public Methods • Client’s view of the services provided by theclass • Privatedata • not accessible outside theclass • protected.

  30. Public and PrivateMembers • Data in class almost always designated private in • definition! • Upholds principles of OOP (Data Hiding) • Hide data fromuser • Allow manipulation only viaoperations • Which are membermethods

  31. Marks for UML-supported visibilitytypes

  32. Using thisReference • 34 • Every object can access a reference to itselfwith • keywordthis. • Implicitly: • refer to the object’s instance variables and other methods inside membermethods. • Enables the class’s code to know which object should be manipulated. • Explicitly: • Can alsouse keywordthisin anon-staticmethod’s body. • Use this reference to access class members.

  33. Importanthint 36

  34. Becareful!! 37

  35. That’s all fortoday…..

More Related