1 / 30

Understanding Classes and Methods in Object-Oriented Programming

Learn about classes, objects, encapsulation, instantiation, and methods in object-oriented programming. Explore different programming languages and their class definitions.

tdemars
Download Presentation

Understanding Classes and Methods in 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. CSCI-383 Object-Oriented Programming & Design Lecture 13

  2. Chapter 4Classes and Methods

  3. Same Ideas, Different Terms • All OOP languages have the following concepts, although the terms they use may differ • classes, object type, factory object • instances, objects • message passing, method lookup, member function invocation, method binding • methods, member function, method function • inheritance, subclassing

  4. Encapsulation and Instantiation • Classes provide a number of very important capabilities • Encapsulation - The purposeful hiding of information, thereby reducing the amount of details that need to be remembered/communicated among programmers • A Service View - The ability to characterize an object by the service it provides, without knowing how it performs its task • Instantiation - The ability to create multiple instances of an abstraction

  5. Internal and External Views • As we noted in the last chapter, encapsulation means there are two views of the same system. • The outside, or service view, describes what an object does • The inside, or implementation view, describes how it does it

  6. Behavior and State • A class can also be viewed as a combination of behavior and state • Behavior: The actions that an instance can perform in response to a request. Implemented by methods • State: The data that an object must maintain in order to successfully complete its behavior. Stored in instance variables (also known as data members, or data fields)

  7. Class Definitions • We will use as running example the class definition for a playing card abstraction, and show how this appears in several languages • Languages considered in the book include Java, C++, C#, Delphi Pascal, Apple Pascal, Ruby, Python, Eiffel, Objective-C and Smalltalk

  8. A Typical Example, Class Definition in C++

  9. Visibility Modifiers • The terms public and private are used to differentiate the internal and external aspects of a class • public features can be seen and manipulated by anybody -- they are the external (interface or service) view • private features can be manipulated only within a class. They are the internal (implementation) view • Typically methods are public and data fields are private, but either can be placed in either category

  10. A C# Class Definition • C# class definitions have minor differences, no semicolon at the end, enum cannot be nested inside a class, and visibility modifiers are applied to methods and data fields individually

  11. Java Class Definition • Java also applied visibility modifiers to each item individually. Does not have enumerated data types, uses symbolic constants instead

  12. Static and Final • Notice how symbolic constants are defined in Java • static means that all instance share the same value. One per class. Similar meaning in many languages • final is Java specific, and means it will not be reassigned. (C++ has const keyword that is similar, although not exactly the same).

  13. Pascal Dialects • We will consider two dialects of Pascal, both descended from the earlier language • Apple Object Pascal, defined by Apple Computer, once widely used on the Macintosh, now much less commonly used • Delphi Pascal, defined by Borland on the PC, still fairly widely used on that platform (called Kylix on the Linux platform) • Many similarities due to the common heritage, but some important differences

  14. Class Definition in Apple Object Pascal • No explicit visibility modifiers (will later see syntax for methods)

  15. Delphi Pascal • Slightly different syntax, must name parent class, has visibility modifier, requires the creation of a constructor

  16. Smalltalk • Smalltalk doesn’t have a textual description for classes, but instead you define classes in a visual interface (revolutionary idea in 1980, but now Visual Basic and Delphi programmers are used to similar facilities)

  17. Methods • Although syntax will differ depending upon language, all methods have the following • A name that will be matched to a message to determine when the method should be executed • A signature, which is the combination of return type and argument types. Methods with the same name can be distinguished by different signatures • A body, which is the code that will be executed when the method is invoked in response to a message

  18. An Example, from C#

  19. Constructor • A constructor is a method that is used to initialize a newly constructed object. In C++, Java, C# and many other languages it has the same name as the class

  20. Accessor (or getter) Methods • An accessor (or getter) is a method that simply returns an internal data value

  21. Why Use an Accessor? • There are many reasons why an accessor is preferable to providing direct access to a data field • You can make the data field read-only • It provides better documentation that the data field is accessible • It makes it easier to later change the access behavior (count number of accesses, whatever) • Some conventions encourage the use of a name that begins with get, (as in getRank()), but this is not universally followed

  22. Setters (or mutators) • A setter (sometimes called a mutator method) is a method that is used to change the state of an object • Mutators are less common than accessors, but reasons for using are similar

  23. Constant Data Fields • Some languages allow data fields to be declared as constant (const modifier in C++, final in Java, other languages have other conventions). Constant data fields can be declared as public, since they cannot be changed

  24. Order of Methods • For the most part, languages don't care about the order that methods are declared. Here are some guidelines • List important methods first • Constructors are generally very important, list them first • Put public features before private ones • Break long lists into groups • List items in alphabetical order to make it easier to search • Remember that class definitions will often be read by people other than the original programmer. Remember the reader, and make it easy for them

  25. Separation of Definition and Implementation • In some languages (such as C++ or Object Pascal) the definition of a method can be separated from its implementation. They may even be in a different file (e.g., the “header” file and the “implementation” file) • Notice need for fully qualified names

  26. Considerations in Method Definitions • In C++ you have a choice to define a method in the class interface, or separately in an implementation file. How do you decide? • Readability. Only put very small methods in the class definition, so that it is easier to read • Semantics. Methods defined in class interface may (at the discretion of the compiler) be expanded in-line. Another reason for only defining very small methods this way

  27. Variations on Classes • We will consider a few of the mostly language-specific variations on the idea of a class • Methods without classes in Oberon • Interfaces in Java (methods without implementations) • Nested classes in Java and C++

  28. Methods without Classes in Oberon • Oberon does not have classes, per se, but allows methods to be defined as a funny type of function

  29. Interfaces in Java • An interface is like a class, but it provides no implementation. Later, another class declare that it supports the interface, and it must then give an implementation. We will have much more to say about interfaces later after we discuss inheritance

  30. Inner or Nested Classes • Some languages (C++ or Java) allow a class definition to be given inside another class definition. Whether the inner class can access features of the outer class is different in different languages

More Related