Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Class Members:

Methods

Methods define what an object can do or the behavior of the object.

Constructors

Constructors don’t actually construct the object. The class makes the object and then executes a constructor to initialize the values of the fields (instance variables).

Member Access Specifiers

The members of a class are classified into three categories: private, public, and protected. This chapter mainly discusses the first two types, private and public. In C++, private, protected, and public are reserved words and are called member access specifiers.

Following are some facts about public and private members of a class:

  • By default, all members of a class are private.

  • If a member of a class is private, you cannot access it directly from outside of the class.

  • A public member is accessible outside of the class.

  • To make a member of a class public, you use the member access specifier public with a colon, :

In object-oriented programming, public, private, and protected are access modifiers used to specify the accessibility of class members, such as properties and methods. These access modifiers define the level of access to a class member from outside the class.

  1. Public: Public members are accessible from anywhere, including outside the class. They can be accessed using the dot (.) operator on the object of the class. Public members can be used by any code that has access to the object of the class.UML

  2. Private: Private members are only accessible within the class they are defined in. They cannot be accessed from outside the class. Private members can only be used by the class itself.

  3. Protected: Protected members are similar to private members, but they can be accessed by derived classes. Protected members are not accessible outside the class, but they can be accessed by any derived class that inherits from the class

Encapsulation

  • There are two views of an object:

    • internal  -  the details of the variables and methods of the class that defines it

    • external  -  the services that an object provides and how the object interacts with the rest of the system

  • From the external view, an object is an encapsulated entity, providing a set of specific services

  • These services define the interface to the object

  • No labels