4. • An object is any entity that
has a state and behavior.
• State: It is represented by
attributes of an object. It
also shows properties of
an object.
• Behavior: It is represented
by methods of an object. It
shows response of an
object with other objects.
3/29/2024 Object-Oriented Programming 4
5. Creating object
ClassName objectName=new className
3/29/2024 Object-Oriented Programming 5
Accessing class members: to
access the class members we
use the dot operator.
Object
• objectName.variableName;
• objectName.methodName();
7. • Java access modifier often called access specifier.
• An access modifier is a keyword in oop languages used to
set the accessibility(visibility) of a variables, methods,
constructors classes and interfaces.
3/29/2024 Object-Oriented Programming 7
9. Constructors in java
• It is a special type of method which is used to initialize
the instance variable of a class.
• Constructor name must be the same name as its class
name.
• A constructor must have no explicit return type.
• A java constructor cannot be abstract, static, and final.
• We can have private , protected, public or default
constructor in java.
3/29/2024 Object-Oriented Programming 9
10. • There are two types of constructors in java.
3/29/2024 Object-Oriented Programming 10
Default Parameterized
Has no any parameter Has at least one parameter
Generated automatically by the
compiler if the programmer
does not defined it.
Created by the programmer with
one or more parameters.
Known as implicit constructor Known as explicit constructor
Initializes 0 for integrals, null for
strings and false for Boolean
type instance variables.
The programmer provides an
initial value for the instance
variables.
13. Encapsulation
• Encapsulation: is the process of binding/enclosing the
instance variables and methods under a single entity/unit.
• Encapsulating or wrapping up of data is called
Encapsulation.
3/29/2024 Object-Oriented Programming 13
14. • One way to implement encapsulation in Java is to use
‘private’ access specifiers on all the variables inside the
class so that no other class can access it.
• These variables can access only by the methods in which
declared in the same class. It is also known as data
hiding.
• So we have to declare the methods as public to access the
private variables.
3/29/2024 Object-Oriented Programming 14
15. • Rules for encapsulation in Java
1. Class Variable should be private
2. To access the values of the variables, create public
setter and getter methods.
3/29/2024 Object-Oriented Programming 15
Setter Methods:
• Public methods that set a value for the private instance variables .
• Returns void (i.e nothing).
• Also called “mutator”methods.
Getter methods:
• Public method that displays value of private private variables.
• Also called “accessor” methods
19. Inheritance
• Inheritance in java: is a mechanism in which one object
acquires all the properties/variables and
behaviors/methods of parent object.
• Inheritance means creating new classes from existing
classes.
• When you inherit from an existing class , you can reuse
methods and variables of parent class, you can add new
methods and variables also.
• Parent class is known as base class/super class.
• Child class is known as derived class/sub class.
• We use a java keyword extends to make inheritance
relationships. The derived class extends from superclass.
3/29/2024 Object-Oriented Programming 19
20. Types of Inheritance
3/29/2024 Object-Oriented Programming 20
Is-A: concept of inheritance. So to
make inheritance the classes should
have this relationships. Example:
vehicle is a car.
Has-A: is concept of composition.
The object of one class created in
another class as a member.
Example: car has engine.
Relationships b/n classes in
java
21. Examples of types of inheritance
3/29/2024 Object-Oriented Programming 21
2. Multilevel
1. Single 3. Hierarchical
22. Abstraction
• The process of hiding complex internal implementation
details from users and providing only necessary
functionality is called abstraction.
• It removes all non-essential things and shows only
important things to users.
3/29/2024 Object-Oriented Programming 22
23. 3/29/2024 Object-Oriented Programming 23
We all use an ATM machine for cash withdrawal,
money transfer, retrieve min-statement, etc. in
our daily life.
But we don’t know internally what things are
happening inside ATM machine when you insert
an ATM card for performing any kind of
operation.
When you need to send SMS from your mobile,
you only type the text and send the message.
But you don’t know the internal processing of
the message delivery.
24. • Similarly, happens in Java OOPs. You only need to call the
specific classes or methods to implement specific
program logic, but you don’t know how these classes or
methods function. This concept is known as abstraction
in Java.
• There are two ways to achieve or implement abstraction
in Java program. They are as follows:
1. Abstract class (50%)
2. Interface (100%)
3/29/2024 Object-Oriented Programming 24
25. Rules of abstract class
1. Class must be declared with abstract keyword to make an abstract class.
2. We can not create an object for an abstract class.
3. If any method is abstract in a class, the class must be declared as abstract.
4. To use methods declared in an abstract class, the abstract class must be extended by an ordinary class and
must implement (override) all abstract methods in that ordinary class.
5. Inside the abstract class, we can create any number of constructors. If you do not create a constructor, the
compiler will create a default constructor.
3/29/2024 Object-Oriented Programming 25
26. Rules of abstract method
Abstract method can only be declared in an abstract class. The implementation is defined in the subclass.
A non-abstract class cannot have an abstract method, whether it is inherited or declared in Java.
It must not provide a method body/implementation in the abstract class for which it is defined.
Method name and signature must be the same as in the abstract class.
Abstract method cannot be static or final.
It cannot be private because the abstract method must be implemented in the subclass. If we declare it
private, we cannot implement it from outside the class.
3/29/2024 Object-Oriented Programming 26
29. • Interfaces in Java:
• In Java, an interface is a blueprint or template of a class.
• It is much similar to the Java class but the only difference
is that it has abstract methods and static constants.
• There can be only abstract methods in an interface, that is
there is no method body inside these abstract methods.
• Unlike a class, you cannot instantiate or create an object
of an interface.
3/29/2024 Object-Oriented Programming 29
30. • An interface does not contain any constructors, but a
class can.
• An interface cannot contain instance fields. It can only
contain the fields that are declared as both static and
final.
• An interface can not be extended or inherited by a class;
it is implemented by a class.
• Syntax of declaring Interfaces in Java:
3/29/2024 Object-Oriented Programming 30
31. Advantages of an Interface in Java
• Use interfaces to achieve data abstraction.
• We also use them to support the functionality of multiple
inheritances in Java.
3/29/2024 Object-Oriented Programming 31
35. Polymorphism in Java
• Polymorphism: is implementing the same thing in
different ways.
• A single object behaves in different ways.
3/29/2024 Object-Oriented Programming 35
36. • In java, polymorphism is achieved in two different ways.
1. Method overloading(compile time polymorphism)
2. Method overriding(run time polymorphism)
3/29/2024 Object-Oriented Programming 36