1.4 Basic Concepts of OOP: 1.4.1 Class
1.4 Basic Concepts of OOP: 1.4.1 Class
// Shape.java
package graphicshape;
class Shape {
// class definition
}
// Circle.java
package graphicshape;
public class Circle extends Shape {
// class definition
}
//Circles.java
package graphicshape;
class Circles {
//class definition
}
// Canvas.java
package appcanvas;
import graphicshape.Circle;
class Canvas {
//class definition
}
Public access modifier
The public access modifier is the most liberal one. If a class or its members are declared as public,
they can be accessed from any other class regardless of the package boundary. It is comparable to
a public place in the real world, such as a company cafeteria that all employees can use irrespective
of their department.
❖ a public method in a class is accessible to the outside world only if the class is declared as
public. if the class does not specify any access modifier, then the public method is
accessible only within the containing package.
Private access modifier
The private access modifier is the most stringent access modifier. A private class member cannot
be accessed from outside the class; only members of the same class can access these private
members. It’s comparable to a safe deposit box room in a bank, which can only be accessed by a
set of authorized personnel and safe deposit box owners.
Protected and default access modifier
Protected and default access modifiers are quite similar to each other. If a member method or field
is declared as protected or default, then the method or field can be accessed within the package.
Note that there is no explicit keyword to provide default access—in fact, when no access modifier
is specified, the member has default access. Also, note that default access is also known as
package-protected access. Protected and default accesses are comparable to the situation in an
office where a conference room is accessible only to one department. What is the difference
between protected and default access? One significant difference between these two access
modifiers arises when we talk about a subclass belonging to another package than its superclass.
In this case, protected members are accessible in the subclass, whereas default members are not.
1.4.5 Abstraction, Encapsulation, Inheritance and Polymorphism
Object orientation is built on the foundations of encapsulation, abstraction, inheritance, and
polymorphism. The following sections starts your journey of mastering of each of these four
concepts.
To get a sense of the world of object-oriented programming, take a mental stroll around the
television department of your local consumer electronics retailer. A television is an abstraction
that offers certain functionality through the proper interface (a TV remote). As a viewer, you need
not understand how the TV works; the TV abstracts all the finer grain details of its operation from
the viewer (abstraction). A television object encapsulates properties of the television (such as
brightness, channel number, and volume) and associated behavior to control these properties in a
single entity (encapsulation). In other words, the access to these properties is restricted to the
associated operations. There are different types of televisions, such as CRT television, LED
television, and LCD television; they belong to a single family forming an inheritance hierarchy.
Although all types of televisions support “display” functionality, the internal technology enabling
the display of content may differ (polymorphism).
1.4.5.1 Abstraction
The Latin root of abstraction means “taking away”—you take away everything except the specific
aspect you wish to focus on. In other words, abstraction means hiding lower-level details and
exposing only the essential and relevant details to the users.
For example, in order to drive a car, it is sufficient for a driver to have an essential repertoire of
skills enabling her to interface with the car’s steering, gear shifting, dashboard, braking, and
accelerator systems. For the driver, it is superfluous to know the internal implementation details,
such as how fuel is injected into the combustion chamber or how batteries are charged indirectly
by the engine. The driver as such is concerned only about using the car and not about how the car
works and provides functionality to the driver. In other words, a car abstracts the internal details
and exposes to the driver only those details that are relevant to the interaction of the driver with
the car.
In other words, you should abstract the details of the class by hiding the implementation details.
1.4.5.2 Encapsulation
Structured programming decomposes the program’s functionality into various procedures
(functions), without much concern about the data each procedure can work with. Functions are
free to operate and modify the (usually global and unprotected) data.
In OOP, data and functions operating on that data are combined together to form a single unit,
which is referred to as a class. The term encapsulation refers to combining data and associated
functions as a single unit. For example, in the Circle class, radius and center are defined as private
fields. Now you can adduce methods draw() and fillColor() along with fields radius and center,
since the fields and methods are closely related to each other. All the data (fields) required for the
methods in the class are available inside the class itself. In other words, the class encapsulates its
fields and methods together.
➢ Note: encapsulation combines data (fields) and logically-related operations (methods).
abstraction hides internal implementation level details and exposes only the relevant details
of the class to the users. abstraction is achieved through encapsulation.
1.4.5.3 Inheritance
Central to java and other OOP language is the concept of inheritance, which allows code defined
in one class to be reused in other classes. In java, you can define a general (more abstract)
Superclass and then extend it with more specific Subclasses.
Inheritance is a reusability mechanism in object-oriented programming in which the common
properties of various objects are exploited to form relationships with each other. The abstract and
common properties are provided in the superclass, which is available to the more specialized
subclasses.
For example, a color printer and a black-and-white printer are kinds of a printer (single
inheritance); an all-in-one printer is a printer, scanner, and photocopier (multiple inheritance).
When we say that a class B is inherited from another class A, then class B is referred to as a derived
class (or subclass) and class A is called as a base class (or superclass). By inheritance, the derived
class receives the behavior of the base class, such that all the visible member methods and variables
of the base class are available in the derived class. Apart from the inherited behavior, the derived
class specializes its behavior by adding to or overriding base class behavior. For example, a Car
superclass could define general methods common to all automobiles, but a Ferrari subclass could
override the accelerate () method that was already defined in the Car class.
1.4.5.4 Polymorphism
The Greek roots of the term polymorphism refer to the “several forms” of an entity. In the real
world, every message you communicate has a context. Depending on the context, the meaning of
the message may change and so may the response to the message. Similarly, in OOP, a message
can be interpreted in multiple ways (polymorphism), depending on the object.
For example, in function overloading (one of the polymorphic constructs in Java), you can provide
methods with the same name but with different numbers of arguments or types of arguments. The
concept is simple, yet it provides a lot of power and flexibility to the programmer.