Introduction To Java Object Oriented Programming OOP
Introduction To Java Object Oriented Programming OOP
Object-Oriented
Programming
(OOP)
Java is an object-oriented programming language. This means that it is based on
the concept of objects. An object is a self-contained unit of data and code.
Object-oriented programming is a popular paradigm for designing software.
by Sahil Kshirsagar
Fundamental Concepts of Java OOP
1 Encapsulation 2 Abstraction
Hides data and methods within a class, allowing Simplifies complex functionality by exposing only
access only through defined interfaces. This essential details. This enhances code readability
promotes code organization and protects data and reduces complexity for users.
integrity.
3 Inheritance 4 Polymorphism
Allows classes to inherit properties and methods Enables objects to take on multiple forms,
from parent classes, promoting code reusability allowing different classes to respond to the same
and creating hierarchical relationships between message in their own way, promoting flexibility
objects. and extensibility.
Classes and Objects in Java
Class Object Example
A blueprint or template for An instance of a class. It is a real- The "Car" class would define
creating objects. Defines the data world entity that has its own attributes like make, model, and
(attributes) and methods (behavior) unique set of data and methods. year, and methods like accelerate
that objects of that class will have. For example, a "Dog" class might and brake. A specific car like a
have attributes like name, breed, "Toyota Camry" would be an
and age, and methods like bark and object of the "Car" class.
wagTail.
Inheritance in Java
Example
A "Cat" class might override the "speak" method inherited from the "Animal" class to make a
"meow" sound instead of the general "animal sound" from the parent class.
Encapsulation in Java
1 Data Hiding
Data members are declared as private, preventing direct access from outside the
class.
3 Example
A "Bank Account" class might have private attributes like balance and account
number, with public methods like getBalance and deposit to access and modify
these data members.
Abstraction in Java