0% found this document useful (0 votes)
16 views

Java Notes - OOPs Concepts

The document discusses fundamental object-oriented programming concepts including objects, classes, encapsulation, polymorphism, inheritance, and abstraction. It provides definitions and examples of each concept.

Uploaded by

prashanth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java Notes - OOPs Concepts

The document discusses fundamental object-oriented programming concepts including objects, classes, encapsulation, polymorphism, inheritance, and abstraction. It provides definitions and examples of each concept.

Uploaded by

prashanth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Fundamental OOP concepts

POP vs OOP

POP(Procedure Oriented Programming ) :

 Large program is divided into smaller programs known as functions or procedures


 Most of the functions share global data
 Data move openly around the system from function to function.
 New Data and functions added whenever necessary is complicative process

OOP(Object Oriented Programming ) :

 Programs are divided into objects


 Functions(methods) that operate on data of an object are tied together in data strucrure
 Data is hidden and cannot be accessed by external methods
 New data and functions can be easily added whenever necesssary

“Simula” is considered as the first object oriented programming language

“Smalltalk” is onsidered as the first truly object oriented programming language .

OOP concepts

Object oriented programing is a methodology or paradigm to design a program using classed and
objects. It implies the software development and maintenance by providing some concepts.
 Object
 Class
 Encapsulation
 Polymorphism
 Inheritance
 Abstraction

Object : An Object is anything that really exists in the world and that can be distinguished from
others. This definition specifies that everything in this world is an Object.

e.g : Pen ,Table , Dog, Car , Person etc ….

An Object has three characteristics

State : Represents the data of an object

Behavior: Represents the operation / tasks of an Object

Identity: Object identity is typically implemented via a unique ID .The value of the ID is not
visible to the external user,but is used internally by JVM to identify each Object uniquely.

For e.g: Pen is an Object .It's name is Reynolds , color is white etc know as its State.

It is used to write , so writing is behavior.

Class : A class is nothing but a blueprint or a template for creating different objects which
defines its properties and behaviors. Java class objects exhibit the properties and behaviors
defined by its class. A class can contain fields and methods to describe the behavior of an object.

Note : Object is physical entity where as class is logical entity.


Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the
data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden
from other classes, and can be accessed only through the methods of their current class.
Therefore, it is also known as data hiding.

To achieve encapsulation in Java −

 Declare the variables of a class as private.


 Provide public setter and getter methods to modify and view the variables values.

Following is an example that demonstrates how to achieve Encapsulation in Java −

The public setXXX() and getXXX() methods are the access points of the instance variables of
the EncapTest class. Normally, these methods are referred as getters and setters. Therefore, any
class that wants to access the variables should access them through these getters and setters.

Adavantages of Encapsulation :

 Hides the implementation details of a class.


 Forces the user to use an interface to access data

 Makes the code more maintainable.

this Keyword

this keyword in Java

 this is a keyword cum operator in Java.


 this is a predefined non static variable
 this represents current calling object reference.\

uses of this keyword :

 this keyword can be used to refer current class or object instance varaiables (syntax :
this.varibale)
 this() can be used to invoke current class constructor

(syntax: this() or this(args)

 this keyword can be used to invoke current class instance method (implicitly)

(syntax:this.method())

 this can be passed as an argument in method call or in the constructor call


 this keyword can also be used to return the current class instance.
Polymorphism in java is a concept by which we can perform a single action by different
ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means
many and "morphs" means forms. So polymorphism means many forms.

Two types of polymorphism :

1.Static/Compile time/Early Binding

2.Dynamic/Run time/Late Binding

why names compile time and run time ?

Binding - Linking Caller and Called Methods

How to achieve static polymorphism ?

1. Method Overloading
2. Constructor Overloading
3. Operator Overloading (Not supported in Java)

Note : Java has an implicit operator overloaded (i.e “+”)

Method Overloading :

Writing number of methods with the same name but signature(means Method-name(arguments))
is different.

Method overloading can take in below three forms

1. Show variance in no of arguments


2. difference in data types
3. Changing order of arguments(sometimes works .so , not encouraged)
Constructor : A constructor in Java is a block of code similar to a method having same name as
Class and is called when an instance of an object is created.

 A constructor doesn’t have a return type.


 The name of the constructor must be the same as the name of the class.
 Unlike methods, constructors are not considered members of a class
 A constructor is called automatically when a new instance of an object is created.
 Manual/Explicit calling of constructor is not possible.

Rules or Precautions :

 Constructor name must match with the name of the class


 Constructors take arguments but doesn’t return any value (can't specify return type)

If we specify return type , JVM treats constructor as method without giving error.

 Constructors have no modifiers , error occurs when modifier is specified.


 Constructors access specifier should be either public or protected. (NO private or default)
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors
of parent object.

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 parent
class, and you can add new methods and fields also.

Inheritance represents the IS-A relationship, also known as parent-child relationship.

Why use inheritance in java

 For Method Overriding (so runtime polymorphism can be achieved).


 For Code Reusability.

Syntax :

class Subclass-name extends Superclass-name

//methods and fields

Types of inheritance in java


On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only.

When a class extends multiple classes i.e. known as multiple inheritance. For Example:

You might also like