Classes and Objects 3 Flashcards
What is the definition of package?
Allows to group classes and interfaces into bundles, that can then be handled together using an accepted naming convention
Why would you group classes into packages?
- Allows reuse, rather than rewriting classes, you can use existing classes by importing them
- Prevents naming conflicts
- classes with the same name can be used in a program, uniquely identifying them by specifying the package they belong to
- Allows access control
- Another level of encapsulation
How to create packages?
package <directory_name_1>.<directory_name_2>;</directory_name_2></directory_name_1>
This implies that the class in directory_2, which is a sub-directory of directory_1
How to use java packages?
To use classes in a package, the import statement, which can take one of the following forms must be used:
What is the definition of default package?
All the classes in the current directory belong to an unnamed package called the default package - no package statement is needed
What is the definition of Information Hiding?
Ability to “hide” the details of a class from the outside world
Referred to as Visibility Control
What is the definition of access control?
Preventing an outside class from manipulating the properties of another class in undesired ways
Why do we use information hiding?
- Allows to safely seal data within the capsule of the class
- Prevents programmers from relying on details of class implementation
- Helps in protecting against accidental or wrong usage
- Keeps code elegant and clean (easier to maintain)
- Enables to provide access to the object through a clean interface
What is the definition of visibility modifiers?
a list of keywords that are used to hide information
What is the definition of public?
Keyword when applied to a class, method or attribute makes it available/visible everywhere (within the class and outside the class)
What is the definition of private?
Keyword when applied to a method or attribute of a class, makes them only visible within that class.
- Private methods and attributes are not visible within subclasses and are not inherited
What is the definition of protected?
Keyword when applied to a method or attribute of a class, makes them only visible within that class, subclasses and also within all classes that are in the same package as that class. They are also visible to subclasses in other packages.
What is the definition of mutable?
A class that contains public mutator methods or other public methods that can change the instance variables is called a mutable class, and its objects are called mutable objects
What is the definition of immutable?
A class that contains no methods (other than constructors) that change any of the instance variables is called an immutable class, and its objects are called immutable objects
What is the definition of delegation?
When a class delegates its responsibilities to other classes?
This is an Association relationship between the classes