Professional Documents
Culture Documents
DR. R LAWRANCE
Director / MCA / ANJAC
lawrancer@yahoo.com
Java is pure object oriented programming or not?
2
Object Oriented Programming Paradigm
3
What does the term Paradigm mean?
4
Classes, Instances and objects
Class
Objects
5
• A typical Java program creates many objects.
An object consists of :
• State : It is represented by attributes of an object. It
also reflects the properties of an object.
• Behavior : It is represented by methods of an object.
It also reflects the response of an object with other
objects.
• Identity : It gives a unique name to an object and
enables one object to interact with other objects.
6
Example of an object : dog
7
Declaring Objects (Also called instantiating a
class)
• When an object of a class is created, the class is said to
be instantiated.
• All the instances share the attributes and the behavior
of the class. But the values of those attributes, i.e. the
state are unique for each object.
• A single class may have any number of instances.
8
Example
9
Initializing an object
• The new operator instantiates a class by allocating
memory for a new object and returning a reference to
that memory.
• The new operator also invokes the class constructor.
10
Getter and Setter
• The get method returns the variable value, and the set
method sets the value.
public class SimpleGetterAndSetter {
private int number;
The class declares a private variable, number. Since number is private, code
from outside this class cannot access.
11
SimpleGetterAndSetter obj = new SimpleGetterAndSetter();
obj.number = 10;
obj.setNumber(10); // OK
12
Why getter and setter?
obj.number = 3;
13
A getter method is the only way for the outside world reads the
variable’s value:
14
• When a variable is hidden by private modifier and can be accessed
only through getter and setter, it is encapsulated.
• Encapsulation is one of the fundamental principles in object-
oriented programming (OOP), thus implementing getter and setter is
one of ways to enforce encapsulation in program’s code.
• Some frameworks such as Hiberate, Spring, Struts… can inspect
information or inject their utility code through getter and setter.
• So providing getter and setter is necessary when integrating your
code with such frameworks.
15
Naming convention for getter and setter
16
Naming Convention
Variable Getter method Setter method
declaration
int quantity int getQuantity() void setQuantity(int
qty)
string firstName String getFirstName() void setFirstName(String fname)
Date birthday Date getBirthday() void setBirthday(Date bornDate)
boolean rich boolean isRich() void setRich(Boolean rich)
boolean getRich()
17
public String firstName;
18
public int getAge()
public class Dog
{
{ return age;
// Instance Variables }
private String name; public String getColor()
private String breed; {
private int age; return color;
private String color; }
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
} 19
public void setName(String
name) public static void main(String[] args)
{ {
this.name = name;
} Dog tuffy = new Dog();
public void setBreed(String tuffy.setName(“Tuffy”);
breed)
tuffy.setBreed(“Rajapalyam”);
{
this.breed=breeda tuffy.setAge(5);
} tuffy.setColor(“White”);
public void setAge( int age)
{ System.out.println(“Hi My Name is:
this.age = age; “+tuffy.getName()+” My Breed is: “+
}
tuffy.getBreed()+” My age is: “+getAge()
public void setColor(String
color) +”My Color is: “+getColor());
{ }
this.color=color;
}
}
} 20
21
Challenge - 1
• Write a program to show the student details
using setter and getter methods.
22
Few of the pillars of object oriented
approach
Keeping the Ability of a class to
design and inherit some data
ABSTRACTION INHERITANCE and behavior from
execution
isolated other related
classes
The property by
Controlling and
which different
managing the INFORMATION POLYMORPHISM objects respond
accessibility and HIDING
differently to the
visibility
same method
23
Message Passing
24
What does a procedure call do?
• answer = sum(a,b)
Procedure name
Procedure body or
Code fragment for
sum
Even though the object sends the message to Object A, the ultimate
receiver of message is not known until runtime
So, the Method to invoke for execution will not be known till runtime.
This is called “late binding”, since the actual code of the method to be
executed is known only at runtime.
To place in or as if in a capsule
Keeping the
data and
operations
together
27
Tightly encapsulated
Class A Class A
{ {
public int x = 10; Private int x = 10;
} }
Class b extends A Class b extends A
{ {
private int y ; Public int y ;
} }
Class C extends A Class C extends A
{ {
Private int z=30; Private int z=30;
} }
28
Encapsulation in context of OO Approach
Invisible implementation
Visible Interface
Data Items
PUSH
Implementation of
Push, Pop
POP
29
Information Hiding
30
Data/Information Hiding
• Our internal data should not go out directly or others could not access our
data
• How do we achieve this?
• Every data member should provide as private we can do this
Recommended
public class Account modifier
{
private double balance;
…
public double getBalance()
{
}
• In Java default modifier is default
Advantage
• Security
31
Access Modifiers In Java
32
Encapsulation & Information Hiding
33
Inheritance
34
IS-A Relationship(inheritance)
Example:
class Parent
{
public void methodOne()
{ }
}
class Child extends Parent
{
public void methodTwo()
{ }
}
35
Example:
class Parent
{
public void methodOne()
{ }
}
class Child extends Parent
{
public void methodTwo()
{ }
}
36
37
Conclusion
• Whatever the parent has by default available to the child but
whatever the child has by default not available to the parent.
• Hence on the child reference we can call both parent and child
class methods. But on the parent reference we can call only
methods available in the parent class and we can't call child
specific methods.
• Parent class reference can be used to hold child class object but
by using that reference we can call only methods available in
parent class and child specific methods we can't call.
• Child class reference cannot be used to hold parent class object.
38
• Example:
• The common methods which are required for housing
loan, vehicle loan, personal loan and education loan we
can define into a separate class in parent class loan.
• So that automatically these methods are available to
every child loan class.
39
• For all java classes the most commonly required functionality
is define inside object class hence object class acts as a root for
all java classes.
• For all java exceptions and errors the most common required
functionality defines inside Throwable class hence Throwable
class acts as a root for exception hierarchy.
40
class Employee
{
float sal=60000;
}
class Main extends Employee
{
float b=1500;
float temp= sal + b;
public static void main(String args[])
{
Main ob=new Main();
System.out.println("Salary amount is:"+ob.sal);
System.out.println(" Extra Bonous is:"+ob.temp);
}
}
41
public class Main extends Calc {
class Calc{
int mul(int xx , int yy)
int sum(int i , int j)
{
{
return xx*yy;
return i+j;
}
}
int divide(int xx , int yy)
int subract(int i , int j)
{
{
return xx/yy;
return i-j;
}
}
}
public static void main(String args[]) {
Main c= new Main();
System.out.println(c.sum(2,2));
System.out.println(c.subract(2,6));
System.out.println(c.mul(8,9));
System.out.println(c.divide(2,2));
}
}
42
Multiple inheritance
• Having more than one Parent class at the same level is
called multiple inheritance.
• Example:
43
• Any class can extends only one class at a time and can't
extends more than one class simultaneously hence java
won't provide support for multiple inheritance.
• Example:
44
• But an interface can extends any no. of interfaces at a time
hence java provides support for multiple inheritance through
interfaces.
• Example:
45
• If our class doesn't extends any other class then only our
class is the direct child class of object.
• Example:
46
• If our class extends any other class then our class is not
direct child class of object, It is indirect child class of
object , which forms multilevel inheritance.
47
Why java won't provide support for multiple
inheritance?
48
Why ambiguity problem won't be there in interfaces?
• Interfaces having dummy declarations and they won't have
implementations hence no ambiguity problem.
49
Cyclic inheritance
• Example 2:
50
HAS-A relationship
• HAS-A relationship
• HAS-A relationship is also known as composition (or)
aggregation.
• There is no specific keyword to implement HAS-A
relationship but mostly we can use new operator.
• The main advantage of HAS-A relationship is reusability.
Example:
class Engine
{
//engine specific functionality
}
class Car
{
Engine e=new Engine();
//........................;
}
51
Abstraction
Keeping the design and execution isolated
MAP is an abstraction or
simplified representation of
what it models
52
Abstraction focuses the outside view of an
object
The outside The Inside
view view
Visible Interface
As a bounded Array
PUSH
POP
Abstraction Encapsulation
Focuses on the Focuses on the
observable implementation
behavior of an behavior of an
object object
54
Abstraction..
AKA – Central Point, outline, Top level information , Not
detailed information
Hide internal information – Only services are highlighted
Ex: ATM card – enter PIN – withdraw amount
(Internal process is not known like query, server, db, lang)
Advantages
Security (outsiders do not aware about internal process)
(Wherever hiding there is security)
Enhancement – without affecting the services we can perform
any changes in the internal system
Eazyness – without knowing the internal code we can use
services
By using abstract classes & interfaces we can implement
abstraction
55
56
Example
abstract class A{}
abstract void printStatus();//no method body and abstract
57
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class TMB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new TMB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
58
For any abstract java class we are not allowed to create an object i.e., for abstract
class instantiation is not possible.
Output:
59
An abstract class can contain constructors in Java. And a constructor of
abstract class is called when an instance of an inherited class is created.
class Main {
public static void main(String args[])
{
Derived d = new Derived(); Output
d.fun();
}
} Base fun() called
61
Interface
• An interface in Java is a blueprint of a class.
It has static constants and abstract methods.
62
How to declare an interface?
interface <interface_name>{
63
Internal addition by the compiler
• The Java compiler adds public and abstract keywords before
the interface method. Moreover, it adds public, static and
final keywords before data members.
64
Polymorphism
• Same name with different forms is the concept of
polymorphism.
Example
In java we can take multiple methods with the same name and different
argument types.
Example: