2. Object
Object is a computer representation of some real-world
thing (i.e person, place) or event.
Objects can have both attributes and behaviors.
An object can be considered a "thing" that can perform a
set of related activities.
The set of activities that the object performs defines the
object's behavior.
The process of creating objects from a class is called
instantiation.
An object is an instance of a class.
A reference value denotes a particular object.
3. Class
A class is simply a representation of a type of object.
It is the blueprint/ plan/ template that describe the
details of an object.
A class is the blueprint from which the individual
objects are created.
Class is composed of three things: a name, attributes,
and operations.
4. Encapsulation
Encapsulation (or information hiding) is a principle, about hiding
the details of the implementation of the interface.
It is to reveal as little as possible about the inner workings of the
Interface.
The encapsulation is the inclusion within a program object of all the
resources needed for the object to function - basically, the methods
and the data.
The class is kind of a container or capsule or a cell, which encapsulate
the set of methods, attribute and properties to provide its indented
functionalities to other classes.
In that sense, encapsulation also allows a class to change its internal
implementation without hurting the overall functioning of the system.
An object has properties and behaviors that are encapsulated inside the
object.
The object offers services to the client but implementation of its
properties and behavior is not a concern of the client and can be
altered without client’s knowledge.
5. Association
Association is a (*a*) relationship between two classes,
where one class use another.
It allows one object instance to cause another to
perform an action on its behalf.
The relationship is structural, because it specifies that
objects of one kind are connected to objects of
another.
Association is the more general term that define the
relationship between two classes, where as the
aggregation and composition are relatively special.
6. Aggregation
Aggregation is a special type of an association.
Aggregation is the (*the*) relationship between two
classes.
When object of one class has an (*has*) object of another,
if second is a part of first (containment relationship) then
we called that there is an aggregation between two classes.
Aggregation is used to represent ownership or a whole/part
relationship.
It is either the process of creating a new object from two or
more other objects, or an object that is composed of two or
more other objects.
7. Composition
Composition is recognized as a special type of an
Aggregation.
In the composite relationship in-between say object1 and
object2, they mutually depend on each other.
Composition is used to represent an even stronger form of
ownership than Aggregation.
Example:
Say University aggregate Chancellor or University has an
(*has-a*) Chancellor. But even without a Chancellor a
University can exists. But the Faculties cannot exist without
the University, the life time of a Faculty (or Faculties)
attached with the life time of the University.
8. Abstraction
Abstraction is an emphasis on the idea, qualities and
properties rather than the particulars (a suppression of
detail).
The importance of abstraction is derived from its
ability to hide irrelevant details and from the use of
names to reference objects.
It places the emphasis on what an object is or does
rather than how it is represented or how it works.
Thus, it is the primary means of managing complexity
in large programs.
9. Generalization
Generalization reduces the complexity by replacing
multiple entities which perform similar functions with a
single construct.
Generalization is the broadening of application to
encompass a larger domain of objects of the same or
different type.
Programming languages provide generalization through
variables, parameterization, generics and polymorphism.
It places the emphasis on the similarities between objects.
Thus, it helps to manage complexity by collecting
individuals into groups and providing a representative
which can be used to specify any individual of the group.
10. Specialization
It is the process of defining a new object based on a
(typically) more narrow definition of an existing
object, or an object that is directly related to, and more
narrowly defined than, another object.
To create a subclass is specialization, to factor out
common parts of derived classes into a common base
(or parent) is generalization.
11. Abstract Class
Abstract class cannot be instantiated.
It can only be used as a super-class for other classes that extend
the abstract class.
Abstract class is the concept and implementation gets completed
when it is being realized by a subclass.
In addition to this a class can inherit only from one abstract class
(but a class may implement many interfaces) and must override
all its abstract methods/ properties and may override virtual
methods/ properties.
Abstract classes can carry a functional “payload” that numerous
subclasses can inherit and use directly, compared to interfaces.
Abstract classes can have all or no abstract methods.
The abstract keyword cannot be applied to static methods.
Abstract method cannot be declared private.
Abstract class may have constructors, but they cannot be used to
instantiate the abstract class.
12. Interface
Interface separates the implementation and defines the
structure.
It is useful in cases where you need to change the
implementation frequently.
They are used to define a generic template and then one or more
abstract classes to define partial implementations of the
interface.
An interface like that of an abstract class cannot be instantiated.
A class that implements an interface does not define all the
methods of the interface, then it must be declared abstract and
the method definitions must be provided by the subclass that
extends the abstract class.
An interfaces can inherit other interfaces.
13. Interface versus Abstract
Interface has no implementation, but they have to be implemented.
Abstract class’s methods can have implementations and they have to be
extended.
Interfaces can only have method declaration (implicitly public and abstract)
and fields (implicitly public static)
Abstract class’s methods can’t have implementation only when declared
abstract.
Interface can inherit more than one interfaces
Abstract class can implement more than one interfaces, but can inherit only one
class
Abstract class must override all abstract method and may override virtual
methods
Interface can be used when the implementation is changing
Abstract class can be used to provide some default behavior for a base class.
Interface makes implementation interchangeable
Interface increase security by hiding the implementation
Abstract class can be used when implementing framework
Abstract classes are an excellent way to create planned inheritance hierarchies
and also to use as non-leaf classes in class hierarchies.
14. Polymorphism
Polymorphism is the ability to create a variable, a function,
or an object that has more than one form.
It is the ability to request that the same operations be
performed by a wide range of different types of things.
It enables the objects belonging to different types to
respond to a method, field, or property calls of the same
name, each one according to an appropriate type-specific
behavior (dynamic binding).
It enables the objects of various types to define a common
interface of operations for users.
It allows a reference to denote objects of different types at
different times during execution.
15. Cohesion and Coupling
Cohesion is an inter-class measure of how well-structured and
closely-related the functionality is in a class.
The objective is to design classes with high cohesion, performing
well-defined and related tasks.
Lack of cohesion in a class means that the purpose of the class is
not focused, and unrelated functionality is ending up in the
class.
Coupling is a measure of intra-class dependencies.
Objects need to interact with each other to carry out the task.
The dependencies should however be minimized to achieve
loose coupling, and to aid in creating extensible applications.
The exposure of implementation details of an object is a major
source of Intra-class dependencies and can impede changes in
the implementation of the object.
High cohesion and loose coupling help to achieve the main goals
of OO design: maintainability, reusability, extensibility, and
reliability.
16. Method Overloading and Overriding
Polymorphism is only concerned with the application
of specific implementations to an interface or a more
generic base class.
Method overloading refers to methods that have same
name but different signatures inside the same class.
Method overriding is where a subclass replaces the
implementation of one or more of its parent's
methods.
Neither method overloading nor method overriding
are by themselves implementations of polymorphism
17. Inheritance
It is a technique to compartmentalize and reuse code
by creating collections of attributes and behaviors
called Objects based on previously created objects.
Inheritance aids in the reuse of code.
The new classes, known as subclasses (or derived
classes), inherit attributes and behavior of the pre-
existing classes, which are referred to as super classes.
The inheritance relationships of classes gives rise to a
class hierarchy.
18. Access Modifiers
They determine whether other classes can use a particular field or
invoke a particular method.
There are two levels of access control:
1) At the top level—public, or package-private (no explicit modifier).
2) At the member level—public, private, protected, or package-
private (no explicit modifier).
Public: When the modifier is public member is visible to all classes.
Private: The private modifier specifies that the member can only be
accessed within its own class.
Protected: The protected modifier specifies that the member can only
be accessed within its own package and its subclass even in another
package.
Default: If a class has no modifier (the default, also known as package-
private), member is visible only within its own package.
19. Access Modifiers
A class can only have public or default modifier.
In every java source code file, only one class in the file
is public accessible, and that class must have the same
name as the file.
Use private unless you have a good reason not to.
Avoid public fields except for constants.
Specifier Public Private Protected Default
Class Y Y Y Y
Sub Class Y N Y N
Package Y N N Y
All Classes Y N N N
20. Constructors
Constructors are used to initialize the instance variables (fields)
of an object.
Constructors must have the same name as the class.
Constructors do not have a return value declaration.
If you don't define a constructor for a class, a default parameter-
less constructor is automatically created by the compiler, calling
the default parent constructor (super()) and initializes all
instance variables to default values.
If you define any constructor for your class, no default
constructor is automatically created.
The first line of a constructor must either be a call on another
constructor in the same class, or a call on the superclass
constructor.
If the first line is neither of these, the compiler automatically
inserts a call to the parameter-less super class constructor.
Constructors can have the same access specifiers used for
variables and methods.
21. this and super Keywords
this calls another constructor in same class.
super calls a constructor in a parent class.
The Java compiler inserts a call to the parent
constructor (super) if you don't have a constructor call
as the first statement of you constructor.
22. Static keyword
Fields that have the static modifier in their declaration are
called static fields or class variables.
They are associated with the class, rather than with any
object, hence can be manipulated without creating an
instance of the class.
Methods having static modifier are invoked with the class
name, without the need for creating an instance of class.
A common use for static methods is to access static fields.
Static methods cannot access instance variables or
instance methods directly without an object reference.
Static methods cannot use the this keyword as there is no
instance for this to refer to.
23. Final keyword
The final modifier can be applied to four Java constructs:
variables: a final variable can be set once and only once,
allowing to declare local constants. They must be assigned
exactly once.
fields: a final field can also be set only once, by the
constructor of the class which defines it.
methods: a final method cannot be overridden nor
hidden.
classes: a final class cannot be extended. A final class has
all its method declared final. On the other hand, fields of a
final class do not have any special property.
24. Final keyword continued
Final parameters are not considered part of the
method signature, and are ignored by the compiler
when resolving method calls.
Anonymous local classes can only reference local
variables and parameters that are declared final.
Private and static methods are always implicitely final,
since they cannot be overridden.