You are on page 1of 4

Object Oriented Programming in Java

What is Java OOP?


OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform operations on
the data, while object-oriented programming is about creating objects that contain both data
and methods.
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute.
OOP provides a clear structure for the programs.

Java - What are Classes and Objects?


Classes is a template for Objects , and an Object is an instance of a Class.When the
individual Objects are created, they inherit all the variables andmethods from the Class.

Java Classes...
Java is an object-oriented programming language.Everything in Java is associated with
classes and objects, along with its attributes and methods.

Java Objects...
In Java, an object is created from a class.

Accessing or Modify Attributes/Methods


Can access attributes by creating an object of the class, and by using thedot syntax
(.)Another term for class attributes is fields.

Java Constructors
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the
class is created.
At the time of calling constructor, memory for the object is allocated in the memory. 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.
Rules for creating constructor :Constructor name must be the same as its class name.
A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final, and synchronized.

Default Constructor
A constructor is called "Default Constructor" when it doesn’t have any parameter.
Rule: If there is no constructor in a class, then compiler automatically creates a default constructor.
Syntax :<class_name>(){}

Parameterized Constructor
A constructor which has a specific number of parameters.It is used to provide different values to
distinct objects
Method Overloading
Multiple methods in a class, can have the same name with different parameters
To perform only one operation, having same name of the methods
increases the readability of the program.
For Example : To perform addition of the given numbers but there can beany number of
arguments, if you write the method such as func1(int, int)for two parameters, and func2(int,
int, int) for three parameters then it maybe difficult for you as well as other programmers to
understand the behavior of the method because its name differs.
Advantage : Increases the readability of the program.
Ways to Overload :
By changing number of arguments
By changing the data type

Constructor Overloading
In Java, a constructor is just like a method but without return type. It can also be overloaded
like Java methods. It is a technique of having more than one constructor with different
parameter lists.
They are arranged in a way that each constructor performs a different task. They are
differentiated by the compiler by the number of parameters inthe list and their types.

Java Copy Constructor


There is no copy constructor in Java. However, we can copy the values from one object to
another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java.
By constructor
By assigning the values of one object into another.
By clone() method of Object class.
Important Questions-
Does constructor return any value? (Ans : Yes)
Can constructor perform other tasks instead of initialization? (Ans : Yes)
Is there Constructor class in Java? (Ans : Yes)

Java static Keyword


is used for memory management mainly. The static keyword belongs to the class than an
instance of the class.
Static can be:
Variable (also known as a class variable)
Method (also known as a class method)
Block Nested class

1.. Java static variable


Any variable, declared as static, known as astatic variable. The static variable can be used to refer to
the common property of all objects(which is not unique for each object).
For Eg:- the company name of employees, college name of students, etc. The static variable gets
memory only once in the class area at the time of class loading.
Advantages :-
It makes your program memory efficient (i.e., it saves memory).
Java static property is shared to all objects.

2.. Java static method I


It belongs to the class rather than the object of a class. It can be invoked without the need
for creating an instance of a class. It can access static data member and can change the
value of it.

Java static Keyword....


Restrictions for the static method
The static method can not use non static data member or call non-static method directly.
this and super cannot be used in static context.

3.. Java static block


Is used to initialize the static data member. It is executed before the main method at the time
of class loading.

Java this Keyword


There can be a lot of usage of java this keyword. In java, this is a reference variable that
refers to the current object.
This can be used to refer current class instance variable.
This can be used to invoke current class method (implicitly)this() can be used to invoke
current class constructor.
This can be passed as an argument in the method call.
This can be passed as argument in the constructor call.
This can be used to return the current class instance from the method.

Java Modifiers
Modifiers are keywords used to change their meanings. Java has a wide variety of modifiers,
including the following:
Access Modifiers : to set access levels for classes, variables, methods and constructors.
The four access levels are :
default: Visible to the package
private: Visible to the class only
public: Visible to the world.
protected: Visible to the package and all subclasses.
Non Access Modifiers :static: used for creating class methods and variables.
final: used for finalizing the implementations of classes, methods, variables.
abstract: used for creating abstract classes and methods.
Synchronized and volatile modifiers, which are used for threads.

private Access Modifier


The private access modifier is accessible only within the class.
If you make any class constructor private, you cannot create the instance ofthat class from
outside the class.
default Access Modifier
If you don’t use any modifier, it is treated as default by default .It is accessible only within package. It
cannot be accessed from outside the package. It provides more accessibility than private. But, it is
more restrictive than protected, and public.

protected Access Modifier


Created the two packages pack and my pack. The A class of pack package is public, so can be
accessed from outside the package. But msg() of this package is declared as protected, so it can be
accessed from outside the class only through inheritance.

public Access Modifier


It is accessible everywhere. It has the widest scope among all other modifiers.

Java Abstraction
It is the quality of dealing with ideas rather than events.
For Example : Consider E-mail, complex details such as what happens as soon as you send
an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore, to send
an e-mail, just need to type the content, mention the address of the receiver, and click send.
In OOP :
Abstraction is a process of hiding the implementation details from the user, only the
functionality will be provided to the user. In other words, User will have the information on
what the object does instead of how it does it. In Java, Abstraction is achieved using
Abstract classes and Interfaces.

Abstract Class
A class which contains the abstract keyword in its declaration is known as Abstract class.
Abstract classes may or may not contain abstract methods, i.e., methods without body
( public void get(); )But, if a class has at least one abstract method, then the class must be
declared abstract. If a class is declared abstract, it cannot be instantiated. To use an
abstract class, you have to inherit it from another class, provide implementations to the
abstract methods in it. If you inherit an abstract class, you have to provide implementations
to all the abstract methods in it. The abstract keyword is anon-access modifier, used for
classes and methods

Java Interfaces
Another way to achieve abstraction in Java, is with interfaces. An interface s a completely
"abstract class" that is used to group related methods with empty bodies
An interface is a reference type. It is similar to class. It is a collection of abstract methods. A
class implements an interface, thereby inheriting the abstract methods of the interface. It is
similar to writing a class. But a class describes the attributes and behaviors of an object. And
an interface contains behaviors that a class implements.

You might also like