JAVA is one of the most popular programming languages. It
was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and
Mike Sheridan at Sun Microsystems, Inc. in 1991. It was publicly released in
1995. Now, Oracle has acquired Sun Microsystems, so all Java certifications are
provided by Oracle.
Java is an object-oriented, high-level language used to develop web, mobile, and standalone applications.
Its main features are:
- Platform
Independence
- Security
- Orientation:
This refers to a way of thinking in terms of objects. Based on this
perspective, a programming model called Object-Oriented Programming (OOP)
is created. This model simplifies software development and maintenance
with the following concepts:
OOP’s Basic Concepts:
- Object
- Classification
- Generalization
& Inheritance
- Polymorphism
- Data
Encapsulation
Object: An object is a discrete entity with
well-defined attributes and behaviors. It can be a physical thing or an
abstract concept.
Classification: Classification is the process of
grouping objects with common attributes and behaviors into a logical unit
called a class. The concept of classification simplifies object management.
Generalization: Generalization is a process of
abstraction where common features of classes are abstracted to simplify their
management. The concept of generalization simplifies the management of classes.
Inheritance is the means of implementing generalization.
Polymorphism: Polymorphism in programming states that
behaviors can manifest in different ways. There are two aspects of
polymorphism:
- One
object manifests behavior in different ways.
- Different
objects manifest the same behavior in different ways.
Behavior is implemented as methods, which need to be named. A method can be named based on the behavior or its manifestation. Hence, identification is simplified by naming the method based on the behavior. The concept of polymorphism simplifies the management of behaviors.
Encapsulation: Encapsulation deals with the
protection of data from accidental or unintentional corruption by the
programmer. This concept states that the scope of data must be confined within
a logical unit called a class. Data of one class must not be accessed directly
from another class. Any programming language that provides the implementation
of these five concepts is called an Object-Oriented Programming Language.
Difference Between Method and Function: The term
“function” represents a task, whereas the term “method” represents behaviors.
Anonymous Block (init block): An anonymous block is a block of statements that doesn’t have a name associated with it. Init blocks are used in the case of overloaded constructors to define common statements for all constructors. If there is more than one init block in a class, their statements are grouped in the order of their occurrence in the class before they move to the constructor.
Static Keyword: The static keyword is used to define
class members. In a class, there can be the following class members:
- Static
data member: Represents class attributes that are common to all
objects of the class. Only a single copy of a static data member is
created at the time of class loading, and this single copy is shared by
all objects of the class.
- Static
block: A block of statements that is executed on its own just after a
class is loaded. It represents the initialization of a class and is used
to initialize static data members of a class.
static {
// statements to be executed at the time of class loading
}
- Static methods: Represent the behavior of a class and are invoked using the class name.
Final Keyword in Java: The final keyword is
similar to the const keyword in other languages. It is used to
restrict the user and can be used in various ways:
- Variable
- Method
- Class
Final Variable: Used to declare a constant value
which, once assigned, cannot be changed further.
Final Method: A final method cannot be overridden by a subclass. This prevents unexpected behavior in the class.
Note: Private and
static methods are always implicitly final in Java, as they cannot be
overridden.
Final Class: A final class cannot be extended by any other class. Implicitly, all methods of a final class are declared as final, but not its data members. Final methods are inherited but cannot be overridden.