Object Orineted Programming
Object Orineted Programming
Class
Abstraction
Encapsulation
Polymorphism
OO Analysis
Access Modifiers
Creating Objects
this Reference
Recap of day 1
Constructors
Communication Team
A constructor is a special method that is
called to create a new object Calling the
constructor
PolicyHolder policyHolder = new PolicyHolder();
A constructor method
◦ will have the same name as that of the class
◦ will not have any return type, not even void
Constructors (1/5)
The coder can write a constructor in a class, if
required
If a user defined constructor is available, it is
called just after the memory is allocated for the
object
If no user defined constructor is provided for a
class, the implicit constructor initializes the
member variables to its default values
◦ numeric data types are set to 0
◦ char data types are set to null character(‘\0’)
◦ boolean data types are set to false
◦ reference variables are set to null
Constructors (2/5)
public class PolicyHolder{
//Data Members
public PolicyHolder(){
bonus = 100; User
} defined
//Other Methods Constructor
}
Constructors (3/5)
Two or more methods in a Java class can
have the same name, if their argument
lists are different
Argument list could differ in
◦ No of parameters
◦ Data type of parameters
◦ Sequence of parameters
Thisfeature is known as Method
Overloading
Constructors (5/5)
Data members of the
class are stored in the
heap along with the
object. Their lifetime
depends on the lifetime
public class PolicyHolder{ of the object
private int policyNo;
private double bonus;
//Other Data Members
public void sample(int x){ Local variables x and y
int y; are stored in the Stack
//More Statements
}
//More Methods
} Local variable
class Test{
public static void main(String [] args){ policyHolder is stored
PolicyHolder policyHolder; in the Stack
policyHolder = new PolicyHolder();
}
}
Dynamic objects will
be stored in the heap
◦ References: 2 2
policyNo
◦ Objects: 2 bonus
◦ References: 3 policy1
◦ Objects: 2 1
policyNo
2
policy3 bonus
policyNo
bonus heap
policy2
Lifetime of objects (1 of 2)
policy1
policy3 = policy1;
◦ References: 3 1
policy3 2 heap
policyNo
bonus This object can be
garbage collected
(Can be Removed
from memory)
Null
reference policy2
Lifetime of objects (2 of 2)
In programming languages like C and C++, the
programmer has to de-allocate all dynamic memory
An object that is not referred by any reference variable will
be removed from the memory by the garbage collector
◦ Automatic Garbage Collection
◦ If a reference variable is declared within a function, the
reference is invalidated soon as the function call ends
◦ Programmer can explicitly set the reference variable to
null to indicate that the referred object is no longer in
use
Primitive types are not objects and they cannot be
assigned null
Garbage Collection
The static keyword can be used in 3
scenarios:
◦ For class variables
◦ For methods
◦ For a block of code
System.out.println(PolicyHolder.getTotal());
//Prints 0
//A static method can be invoked without creating an object
PolicyHolder policyHolder1 = new PolicyHolder ();
System.out.println(PolicyHolder.getTotal());
//Prints 1
PolicyHolder policyHolder2 = new PolicyHolder ();
System.out.println(PolicyHolder.getTotal());
//Prints 2
Static methods can access only other static data and methods
class Test{
static{
//Code goes here
}
}
A static block helps to initialize the static data members
just like constructors help to initialize instance members
System.out.println(“Hello World”);
String s = “Java”;
Inheritance (1/3)
The class Policy is known as the
◦ super class
◦ parent class
The class TermInsurancePolicy is known
as
◦ sub class
◦ derived class
◦ child class
Inheritance (2/3)
public class Policy{
private double premium;
private double maturityValue;
//Other Data
public void setPremium(double premium){
this.premium = premium;
}
public double getPremium(){return premium;}
public void setMaturityValue(double maturityValue){
this.maturityValue = maturityValue;
}
public double getMaturityValue(){return
maturityValue;}
//Other Methods
}
TermInsurancePolicy
-term : int
+setTerm(in term : int) : void
+getTerm() : int
+getBenifit() : double
Inheritance
A class can be further inherited from a
derived class
Multi-Level Inheritance
Concept of a class inheriting from more
than one base class
◦ A Hybrid car can inherit from FuelCar and
BatteryCar
◦ Multiple inheritance is rarely used because of
the complexities it brings in
Modern OO languages like Java and C#
don’t support Multiple Inheritance
Multiple Inheritance
Any number of sub classes can be created
from a base class
Consider a class EndowmentPolicy
◦ EndowmentPolicy is a Policy; EndowmentPolicy
is extended from Policy
◦ Extra data members and methods are added
More on Inheritance
What is a protected access?
What is multilevel inheritance?