Object-Oriented Programming
• Object-Oriented Programming
• Class and Objects
• Access Modifiers
• Constructors
Agenda • Inheritance
• Types of Inheritance
• Methods Overloading
• Methods Overriding
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Object-Oriented Programming
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Object-Oriented Programming
• OOP is about creating objects that contain both data and methods.
• Increases the flexibility and maintainability of programs.
• Provides clear structure for the programs.
OOPs
Abstraction Encapsulation Inheritance Polymorphism
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Classes and Objects
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Class
• A group of objects which have common properties.
• A template or blueprint from which objects are created.
• Class can't be physical and it’s a logical entity.
• A class in Java can contain:
▪ Fields
▪ Methods
▪ Constructors
▪ Blocks
▪ Nested class and interface
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Objects
• An entity that has state and behavior is known as an object e.g., chair, bike, etc.
• Can be physical or logical (tangible and intangible).
• An object has three characteristics:
▪ State
▪ Behavior
▪ Identity
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Classes and Objects
Vehicle
Class
Car Truck Cycle
Objects
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Encapsulation
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Encapsulation
• A process of wrapping code and data together into a single unit.
• For example, a capsule which is mixed of several medicines.
• Prevents outer classes from accessing and changing methods of a class.
• This also helps in achieving data hiding.
• Makes the code cleaner and easy to read.
Fields
Class
Methods
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Abstraction
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Abstraction
• Hides the implementation details and showing only functionality to the user.
• Shows only necessary details 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.
• Lets you focus on what the object does instead of how it does it.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Access Modifiers
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Access Modifiers
Default • Accessible only within the package.
Public • Widest scope and is accessible everywhere.
Private • Accessible only within the class.
Protected • Accessible only within and outside the package through
inheritance.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Access Modifiers
Modifier Class Package Subclass World
Public YES YES YES YES
Protected YES YES YES NO
No modifier YES YES NO NO
private YES NO NO NO
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
this keyword
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
this keyword
• A reference variable that refers to the current object.
• Can be used to refer current class instance variable.
• Can also be used to invoke current class method and current class constructor.
• Can be used to return the current class instance from the method.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Constructors
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Constructor
• A block of codes similar to the method.
• Called when an instance of the class is created.
• At the time of calling constructor, memory for the object is allocated in the memory.
• Everytime an object is created using new keyword, atleast one constructor is called.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
How does a constructor work?
The moment an object of a class is created, the constructor of a class is called which
initializes the class attributes.
new
Class Object Constructor
default
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Types of Constructors
Constructors
Default Constructor: Parameterized Constructor:
does not have any having a specific number of
parameter. parameters.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Constructor Overloading
• Refers to the use of more than one constructor in an instance class.
• Each overloaded constructor must have different signatures.
• For the compilation to be successful, each constructor must contain a different list of
arguments.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Constructor Chaining
• Process of calling one constructor from another with respect to the current object.
• The real purpose is to pass parameters through a bunch of different constructors.
• Occurs through inheritance.
• Can be done in two ways:
▪ within same class
▪ from base class
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Inheritance
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Inheritance
• In this, one object acquires all the properties and behaviors of a parent object.
• You can create new classes that are built upon existing classes.
• Represents the IS-A relationship which is also known as a parent-child relationship.
• When you inherit from an existing class, you can reuse methods and fields of the
parent class.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Inheritance
Vehicle
Bikes Cars Buses Trucks
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Type of Inheritance
Single Hierarchical Multilevel
Inheritance Inheritance Inheritance
A
Super
A A B
Class
B Sub
C
Class
A
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
IS-A Relationship
Inheritance is used in Java for:
Method Overriding
(To achieve run time Code Reusability
polymorphism)
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Aggregation (HAS-A Relationship)
• Also known as composition.
• Used for code reusability in Java.
• An instance of one class has a reference to an instance of another class or an other
instance of the same class.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Polymorphism
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Polymorphism
• A concept by which we can perform a single action in different ways.
• We can perform polymorphism in java by method overloading and method overriding.
• There are two types of polymorphism in Java:
compile-time polymorphism and runtime polymorphism.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Method Overloading
• A class has more than one method with same name but having different signature.
• Adds or extends the behavior of an existing method.
• An example of compile time polymorphism since actual method calling is resolved at
compile time.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
Method Overriding
• A derived class have the same methods with exactly the same signature as base class.
• Change the behavior of an existing method in the base class.
• Methods must have same access modifiers:
public, protected and internal and not private
• Example of run time polymorphism since actual method calling is resolved at runtime.
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.
THANKS
Any Questions?
Copyright Dot Net Tricks Innovation Pvt. Ltd. | All rights Reserved.