1) Object:
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen,
table, car, etc.
Object Definitions:
• An object is a real-world entity.
• An object is a runtime entity.
• The object is an entity which has state and behavior.
• The object is an instance of a class.
2) Class It is a template or blueprint from which objects are created. A class is a group
of objects which have common properties. It is a logical entity. It can't be physical.
A class in Java can contain:
• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
Syntax to declare a class:
class <class_name>
{
field;
method;
3) Encapsulation:
Encapsulation is a process of wrapping code and data together into a single unit called Class.
We can create a fully encapsulated class in Java by making all the data members of the
class as private. Now we can use setter and getter methods to set and get the data in it.
It is a way to achieve data hiding in Java because other class will not be able to access the
data through the private data members.
The encapsulate class is easy to test. So, it is better for unit testing.
Example
public class Student
//private data member
private String name;
//getter method for name
public String getName()
return name;
//setter method for name
public void setName(String name)
this.name=name
//A Java class to test the encapsulated class.
class Test
public static void main(String[] args)
{
//creating instance of the encapsulated class
Student s=new Student();
//setting value in the name member
s.setName("vijay");
//getting value of the name member
System.out.println(s.getName());
4) Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
Another way, it shows only essential things to the user and hides the internal details,
for example, sending SMS where you type the text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
a)Abstract class
A class declared as abstract is known as an abstract class. It can have abstract and
non-abstract methods. It needs to be extended and its method implemented. It cannot be
instantiated.
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to change the body of the
method.
b)Interface:
An interface is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and multiple
inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It
cannot have a method body
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an interface.
Since Java 9, we can have private methods in an interface.
Why use Java interface?
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
Interface declaration
interface Bank
float rateOfInterest();
Sub classes SBI, PNB implements Bank interface
class SBI implements Bank
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank
public float rateOfInterest(){return 9.7f;}
Creating object for sub class SBI & calling method with the subclass object
class TestInterface2
public static void main(String[] args)
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
Output:
ROI: 9.15
5) Polymorphism
Using Polymorphism, we can perform a single action in different ways
There are two types of polymorphism in Java: compile-time polymorphism and
runtime polymorphism. We can perform polymorphism in java by method overloading and
method overriding.
a)Method overloading (OR) Static Polymorphism:
Method overloading in Java allows a class to have more than one method with the
same name, but with different parameter lists. This enables you to define multiple versions
of a method, each handling different types or numbers of parameters.
Key Points of Method Overloading:
Different Parameters: Methods must differ in the number or type of parameters (or
both).
Same Method Name: All overloaded methods must have the same name.
b)Method overriding (OR) run time Polymorphism:
class Bike
void run() { System.out.println("running"); }
class Splendor extends Bike
void run()
System.out.println("running safely with 60km");
public static void main(String args[])
Bike b = new Splendor();//upcasting
b.run();
Output:
running safely with 60km.
Method overriding allows a subclass to provide a specific implementation of a
method that is already defined in its superclass,
6) Inheritance is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields
of the parent class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship, also known as a parent-child relationship.
Why use inheritance in java
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
class Employee
float salary=40000;
class Programmer extends Employee
int bonus=10000;
public static void main(String args[])
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
Types of inheritance in java
-- A class inherits another class, it is known as a single inheritance
-- When there is a chain of inheritance, it is known as multilevel inheritance.
-- When two or more classes inherits a single class, it is known as hierarchical inheritance
Association
Association represents the relationship between the objects. Here, one object can be
associated with one object or many objects. There can be four types of association between
the objects:
• One to One
• One to Many
• Many to One, and
• Many to Many
Let's understand the relationship with real-time examples. For example, One country can
have one prime minister (one to one), and a prime minister can have many ministers (one to
many). Also, many MP's can have one prime minister (many to one), and many ministers can
have many departments (many to many).
Association can be undirectional or bidirectional.
Aggregation
Aggregation is a way to achieve Association. Aggregation represents the relationship
where one object contains other objects as a part of its state. It represents the weak
relationship between objects. It is also termed as a has-a relationship in Java. Like,
inheritance represents the is-a relationship. It is another way to reuse objects.
Composition
The composition is also a way to achieve Association. The composition represents the
relationship where one object contains other objects as a part of its state. There is a strong
relationship between the containing object and the dependent object. It is the state where
containing objects do not have an independent existence. If you delete the parent object, all
the child objects will be deleted automatically.