Object-oriented programming (OOP) is a
computer programming model that organizes
software design around data, or objects,
rather than functions and logic. An object can
be defined as a data field that has unique
attributes and behavior.
Every class has data field(data member) and
method(function).
A Java object is an instance of a Java class.
Each object has an identity, a behavior and a
state.
The state of an object is stored in fields
(variables), while methods (functions) display
the object's behavior. Objects are created at
runtime from templates, which are also
known as classes.
A class is a template or blueprint from which
objects are created. So, an object is the
instance(result) of a class.
Originally, object-oriented programming was
used most frequently for two major types of
applications:
Computer simulations,
which attempt to mimic real-world activities
so that their processes can be improved or so
that users can better understand how the
real-world processes operate
Graphical user interfaces, or GUIs
(pronounced “gooeys”), which allow users to
interact with a program in a graphical
environment
To understand object-oriented programming
it requires grasping three basic concepts:
1. Encapsulation
2. Inheritance
3. Polymorphism
A method is a self-contained block of
program code that carries out some action,
similar to a procedure (function) in a
procedural program (C program).
It is like a function in C program but it is
associated some class.
For example, a Dog class might have
methods for walking, eating.
In object-oriented classes, attributes and
methods are encapsulated into objects.
Encapsulation is the enclosure of data and
methods within an object.
Encapsulation allows you to treat all of an
object’s methods and data as a single entity.
Just as an actual dog contains all of its
attributes and abilities, so would a program’s
Dog object.
Encapsulation also refers to the concealment
of an object’s data and methods from outside
sources.
Concealing data is sometimes called
information hiding.
Encapsulation lets you hide specific object
attributes and methods from outside sources
and provides the security that keeps data and
methods safe from inadvertent(undesired)
changes.
An important feature of object-oriented program
design is inheritance—the ability to create
classes that share the attributes and methods of
existing classes, but with more specific features.
For example, Automobile is a class, and all
Automobile objects share many traits and
abilities. Convertible is a class that inherits from
the Automobile class; a Convertible is a
type of Automobile that has and can do
everything a “plain” Automobile does—but with
an added ability
A final important concept in object-oriented
terminology is polymorphism.
Literally, polymorphism means “many
forms”—it describes the feature of languages
that allows the same word or symbol to be
interpreted correctly in different situations
based on the context.
For example, although the classes
Automobile, Sailboat, and Airplane all inherit
from Vehicle, turn and stop methods work
differently for instances of those classes.
It is a Java standard, although not a requirement, to begin
class identifiers with an uppercaseletter and employ other
uppercase letters as needed to improve readability.
(By contrast, method identifiers, like println(), conventionally
begin with a lowercase letter.)
The stylethat joins words in which each word begins with an
uppercase letter is called Pascal casing, or sometimes upper
camel casing. You should follow established conventions for
Java so
your programs will be easy for other programmers to
interpret and follow.
To create a Scanner object and connect it to the
System.in object, you write a statement similar to
the following:
Scanner inputDevice = new Scanner(System.in);
A main() method executes automatically when
you run a program, but other methods do not
execute simply because you place them
within a class—they must be called.
The method header is the first line of a
method. It contains the following:
Optional access specifiers
A return type
An identifier
Parentheses
It is a special type of method which is used to
initialize the object.
Every time an object is created using the
new() keyword, at least one constructor is
called.
It calls a default constructor if there is no
constructor available in the class. In such
case, Java compiler provides a default
constructor by default.
Constructor name must be the same as its
class name
There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
The access modifiers in Java specifies the
accessibility or scope of a field, method,
constructor, or class.
We can change the access level of fields,
constructors, methods, and class by applying
the access modifier on it.
Private: The access level of a private modifier is only
within the class. It cannot be accessed from outside
the class.
Default: The access level of a default modifier is only
within the package. It cannot be accessed from
outside the package. If you do not specify any access
level, it will be the default.
Protected: The access level of a protected modifier is
within the package and outside the package through
child class. If you do not make the child class, it
cannot be accessed from outside the package.
Public: The access level of a public modifier is
everywhere. It can be accessed from within the class,
outside the class, within the package and outside the
package