0% found this document useful (0 votes)
69 views6 pages

1.4 Basic Concepts of OOP: 1.4.1 Class

1. Object oriented programming (OOP) revolves around modeling real-world objects as classes that encapsulate both data and behavior. 2. Key OOP concepts include classes, objects, and access modifiers. A class defines the attributes and methods common to all objects of that type, while an object is an instance of a class. Access modifiers determine the visibility of classes, methods, and fields. 3. Other important OOP concepts are abstraction, encapsulation, inheritance, and polymorphism. Abstraction hides lower-level details and exposes only essential details. Encapsulation combines related data and methods into a class. Inheritance allows classes to inherit attributes and behaviors from parent classes.

Uploaded by

shimelis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
69 views6 pages

1.4 Basic Concepts of OOP: 1.4.1 Class

1. Object oriented programming (OOP) revolves around modeling real-world objects as classes that encapsulate both data and behavior. 2. Key OOP concepts include classes, objects, and access modifiers. A class defines the attributes and methods common to all objects of that type, while an object is an instance of a class. Access modifiers determine the visibility of classes, methods, and fields. 3. Other important OOP concepts are abstraction, encapsulation, inheritance, and polymorphism. Abstraction hides lower-level details and exposes only essential details. Encapsulation combines related data and methods into a class. Inheritance allows classes to inherit attributes and behaviors from parent classes.

Uploaded by

shimelis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

1.

4 Basic Concepts of OOP


First of all, let’s examine (in very rough terms) how software systems are designed using the
Structured (sometimes called Functional) approach.
In Structured Programming, the general method was to look at the problem, and then design a
collection of functions that can carry out the required tasks. If these functions are too large, then
the functions are broken down until they are small enough to handle and understand. This is a
process known as functional decomposition.
Most functions will require data of some kind to work on. The data in a functional system was
usually held in some kind of database (or possibly held in memory as global variables).
The problem with this approach is that if the problem we are tackling becomes too complex, the
system becomes harder and harder to maintain. The other major problem here is that the absence
of code reusability and of changing one procedure may cause the whole code to be re- written,
error in one method causes the whole project to fail.
OO tries to lessen the impact of this problem by simply combining related data and functions into
the same module.
Java is an object-oriented programming(OOP) language. Object orientation helps a developer to
achieve a modular, extensible, maintainable and reusable system. To write a good quality
programs, a programmer must have a firm command of OOP concepts.
Object oriented programming revolves around the concept of an object, which encapsulates data
and behavior acting on the data together.
1.4.1 Class
✓ Is a design or blueprint in which we used to model real world objects having similar state
and behavior and later objects created from.
✓ A template that describes the kind of state and behavior that objects of its type support.
✓ A class is a fundamental abstraction entity and building block in OOP.
✓ A class encapsulates state (data) and behavior (operations) of an entity.
✓ A class is simply a template for an object. A class describes what attributes and methods
will exist for all instances of the class.
The following is the general skeleton of a java class.
1.4.2 Object
✓ Is an actual instance of a class.
✓ At runtime, when the JVM encounters the New Keyword, it will use the appropriate class
to make objects that is an instance of that class. That object will have its own state and
access to all of the behaviors defined by its class.
✓ Objects model real world objects in which we use in daily activities.
✓ “Object” is a kind of idiom or metaphor that addresses the human psychology and the way
humans perceive the real world and think.
The following diagram shows that the way of object creation and instantiation.
1.4.3 members
In object oriented members are that of states and behaviors of a class. States are attributes
whereas behaviors are methods.
❖ State (instance variables): each object (instance of a class) will have its own unique set of
instance variables defined in the class. Collectively, the values assigned to an object’s
instance variables make up the objects state.
❖ Behaviors (methods): when a programmer creates a class, he/she creates methods for that
class. Methods are where a class’s logic is stored and where the real work gets done. They
are where algorithms get executed and data gets manipulated.
1.4.4 Class member visibility (Access modifier)
Access modifiers determine the level of visibility (and therefore access) for a Java entity (a class,
method, or field). Access modifiers enable you to enforce effective encapsulation. If all member
variables of a class can be accessed from anywhere, then there is no point putting these variables
in a class and no purpose in encapsulating data and methods together in a class.
Java supports four types Access of modifiers:
✓ Public
✓ Protected
✓ Private
✓ Default
The following class library show the general outline of access modifier.

// 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.

You might also like