Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Introduction to Java

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

Parent Class Child Class (Subclass) Example


(Superclass) Inherits properties and methods A "Dog" class could inherit from
Defines general characteristics from the parent class, adding its a "Animal" class, inheriting
and behaviors common to a group own specific characteristics and properties like name and age,
of related objects. behaviors. while adding its own specific
behaviors like barking and
wagging its tail.
Polymorphism in Java
Compile-Time Polymorphism Runtime Polymorphism
Method overloading allows multiple Method overriding allows subclasses to
methods with the same name but different provide their own implementations of
parameters within the same class. The methods inherited from parent classes. The
compiler chooses the appropriate method actual method called depends on the
based on the provided parameters. object's runtime type.

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.

2 Getter and Setter Methods


Public methods provide controlled access to private data members. Getters retrieve
data, and setters modify data.

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

Abstract Classes Interfaces Example


Define common methods that Specify methods that classes must An "Animal" abstract class could
subclasses must implement, implement, but without providing define an abstract "speak" method,
providing a template for related concrete implementations. They which subclasses like "Dog" and
objects. enforce a standard for implementing "Cat" would need to implement
certain behaviors. according to their specific sounds.
Java OOP Example and Syntax
Class Declaration public class Animal {

Data Members private String name;

Constructor public Animal(String name) { this.name = name; }

Method public void speak() { System.out.println("Animal


sound"); }

Getter Method public String getName() { return name; }

Setter Method public void setName(String name) { this.name =


name; }

Main Method public static void main(String[] args) { Animal dog =


new Animal("Buddy"); dog.speak(); }

You might also like