Object oriented principles
Class & Object
Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities.
Class
A class is a user defined blueprint or prototype from which objects are created.
It represents the set of properties or methods that are common to all objects of one type.
Class declaration includes,
Access specifiers: A class can be public or has default access (package private).
Class name: The name should begin with a capital letter & then follow camel case convention.
Superclass:(if any): The name of the class's parent (superclass), if any, preceded by the keyword extends.
Implicit super class of all java classes is
java.lang.Object
.
Interfaces:(if any): A comma-separated list of interfaces implemented by the class, preceded by the keyword implements.
A class can implement more than one interface.
Body: Class body is surrounded by
{}
.Constructors: Used for initializing new objects.
Fields: Variables that provides the state of the class and its objects.
Methods: Used to implement the behavior of the class and its objects.
Object
It is a basic unit of Object Oriented Programming and represents the real life entities.
A typical Java program creates many objects, which interacts by invoking methods.
An object consists of :
ConstructDescriptionState
It is represented by attributes of an object (properties of an object).
Behavior
It is represented by methods of an object (actions upon data)
Identity
It gives a unique identity to an object and enables one object to interact with other objects. eg : Emp id / Student PRN / Invoice No
Creating an object
The
new
operator instantiates a class by allocating memory for a new object and returning a reference to that memory. Thenew
operator also invokes the class constructor.Constructor is a special method having same name as the class name with no explicit return type and may be parameterized or parameter less (Default constructor).
Parameterized constructor is used initialize the state of the object.
If a class does not explicitly declare any constructor, then the Java compiler automatically provides a no-argument constructor, called the default constructor.
This default constructor implicitly calls the super class's no-argument constructor i.e.,
java.lang.Object
class's constructor.
Usage of this
To unhide, instance variables from method local variables (to resolve the conflict).
To invoke the constructor, from another overloaded constructor in the same class. (constructor chaining -> to avoid duplication)
Encapsulation in Java
Encapsulation is defined as the wrapping up of data & code as a single unit. It is the mechanism that binds together code and the data it manipulates.
It's is a protective shield that prevents the private data from being accessed by the code outside this shield.
The variables or data of a class is hidden from any other class and can be accessed only through any member function/method of own class in which they are declared.
As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
Tight Encapsulation can be achieved by declaring all the variables in the class as private and writing public methods as its accessors.
Advantages of Encapsulation:
Data Hiding (security)
Increased flexibility: We can make the variables of the class as read-only or write only or r/w.
Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.
Testing code is easy.
Summary
Encapsulation, consists of Data hiding + Abstraction.
Information hiding is achieved by private data members & supplying public accessors.
Abstraction is achieved by supplying an interface to the Client (customer).
Highlighting only WHAT is to be done & not highlighting HOW it's internally implemented.
method local variables v/s instance data members
Method local variable are allocated on stack, by default uninitialized.
Javac(compiler) doesn't allow accessing ANY(primitive type or reference type) un-initialized variable.
Instance data members are allocated on heap, initialized to default values.
Last updated