SlideShare a Scribd company logo
1 of 78
Download to read offline
© People Strategists - Duplication is strictly prohibited -
www.peoplestrategists.com
1
Object Oriented
Programming
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
2
DAY 2
Object Oriented Programming
Objects And Classes
Encapsulation
Polymorphism
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
3
Introduction to Object-Oriented Programming
• The object-oriented is a programming paradigm where the program
logic and data are weaved.
• OOPs is a way of conceptualizing a program's data into discrete
"things" referred to as objects, each having its own properties and
methods.
• For example, your friend is a bank manager and he wants you to help
improving systems. The first object you might design is the
general-purpose Account. The Account object has properties and
methods. For each client your friend's bank have, you would have to
create an Account object.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
4
Introduction to Object-Oriented Programming
(Contd.)
Object
Characteristics of OOP
Class
Encapsulation
Inheritance
Polymorphism
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
5
Introduction to Object-Oriented Programming
(Contd.)
• Benefits of OOP:
• Re-usability: You can write a program using a previous developed code.
• Code Sharing: You are able to standardize the way your are programming with
your colleagues.
• Rapid Modeling: You can prototype the classes and their interaction through
a diagram.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
6
Objects
• An entity that has state and behavior is known as an object, such as chair,
bike, marker, pen, table, and car.
• It can be physical or logical (tangible and intangible).
• The example of intangible object is banking system.
• An object has three characteristics:
• State: Represents data (value) of an object.
• Behavior: Represents the behavior (functionality or action) of an object, such as
deposit and withdraw.
• Identity: Object identity is typically implemented via a unique ID. The value of the ID
is not visible to the external user.
However, it is used internally by the JVM to identify each object uniquely.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
7
Objects (Contd.)
• At any point in time, an object can be described from the data it
contains.
• An object is an instance of a class.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
8
Objects (Contd.)
• Example:
Car Objet
States:
Brand
Model
Price
Color
Behaviors:
Starting car
Stopping car
Changing gear
Applying brake
Get km/liter
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
9
Classes
• A class is a collection of objects (or values) and a corresponding set of
methods.
• Objects contain data and methods, but to use objects in a program, a class
needs to be created.
• A class lets us define what data is going to be stored in the object, how it
can be accessed and modified, and what actions can be performed on it.
• Thus, a class is a template for an object.
• Example:
A set of cars with operations, such as starting, stopping, changing gear,
applying brake, and get km/liter.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
10
Encapsulation and Access Control
• Encapsulation is the mechanism that binds together code and the
data it manipulates, and keeps both safe from outside interference
and misuse.
• A protective wrapper that prevents the code and data from being
arbitrarily accessed by other code defined outside the wrapper.
• Access to the code and data inside the wrapper is tightly controlled
through a well-defined interface.
• The basis of encapsulation is the class.
• When you create a class, you will specify the code and data that
constitute that class.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
11
Encapsulation and Access Control(Contd.)
• The data defined by the class are referred to as member variables or
instance variables.
• The code that operates on that data is referred to as member
methods.
• The complexity of the implementation inside the class can be hided.
• Each method or variable in a class may be marked private or public.
• The private methods and data can only be accessed by code that is a
member of the class.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
12
Encapsulation and Access Control(Contd.)
• The following figure describes conceptual representation of
encapsulation concept:
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
13
Inheritance
• Inheritance is the process by which one object acquires the
properties of another object.
• It supports the concept of hierarchical classification.
• For example, a Golden Retriever is part of the classification dog,
which in turn is part of the mammal class, which is under the larger
class animal.
• Without the use of hierarchies, each object would need to define all
of its characteristics explicitly.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
14
Inheritance (Contd.)
• By use of inheritance, an object need only define those qualities that
make it unique within its class. It can inherit its general attributes
from its parent.
• It is the inheritance mechanism that makes it possible for one object
to be a specific instance of a more general case.
• Inheritance interacts with encapsulation as well.
• If a given class encapsulates some attributes, then any subclass will
have the same attributes plus any that it adds as part of its
specialization.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
15
Inheritance (Contd.)
• A new subclass inherits all of the attributes of all of its ancestors.
• The following figure describes conceptual representation of
inheritance concept:
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
16
Polymorphism
• Derived from two Latin words - Poly, which means many, and
morph, which means forms.
• Polymorphism is the capability of an action or method to do different
things based on the object that it is acting upon.
• In object-oriented programming, polymorphism refers to a
programming language's ability to process objects differently
depending on their data type or class.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
17
Polymorphism, Encapsulation, and
Inheritance Work Together (Contd.)
• The following figure describes conceptual representation of
polymorphism, encapsulation, and inheritance working together:
Shape
Draw()
Triangle
Draw()
Rectangle
Draw()
Circle
Draw()
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
18
Objects And Classes
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
19
Defining a Class
• The syntax to define a class:
class <class_name>
{
……
}
• An example to define a class:
class SimpleCalculator {
}
• In the preceding code snippet is used to define SimpleCalulator class.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
20
Creating an Object
• The syntax to create an object:
<classname> <object ref variable> = new <classname>();
• An example to create an object:
SimpleCalculator obj=new SimpleCalculator();
• In the preceding code snippet, a object reference variable named obj
of type SimpleCalculator is created. The new operator creates a new
SimpleCalculator object on the heap memory and assigns this object
to the reference variable, obj.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
21
Creating an Object (Contd.)
• You can also assign null to an object reference variable as shown in
the following code snippet:
SimpleCalculator obj=null;
• The preceding code snippet creates an object reference variable but it
does not refers any object. Here, only a space for the object reference
variable is created but an actual SimpleCalculator object is not
created.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
22
Instance Variable
• Instance variable is a variable defined inside the class, but outside any
method.
• Each instantiated object has a separate copy of instance variable.
• If the instance variables are not initialized, they will have the default value.
• An example to create instance variable:
class SimpleCalculator {
int operand1, operand2,result;
}
• In the preceding code snippet, operand1,operand2, and result are the
instance variables of SimpleCalculator class.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
23
Method
• A method is a block of statements that has a name and can be
executed by calling.
• A method is defined to preform certain task.
• The syntax to define a method:
<returntype> <methodname>(parameterlist){
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
24
Method (Contd.)
• The different types of methods are:
• Method without return type and without parameter
• Method without return type and with parameter
• Method with return type and without parameter
• Method with return type and with parameter.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
25
Method (Contd.)
• An example to define a method without return type and without
parameter:
void sum(){
int a=10;
int b=20;
int result=a+b;
System.out.println(“The sum:”+result);
}
• If a method does not return any value, then the return type should be
set to void.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
26
Method (Contd.)
• An example to define a method without return type and with
parameter:
void sum(int a, int b){
int result=a+b;
System.out.println(“The sum :”+result);
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
27
Method (Contd.)
• An example to define a method with return type and without
parameter:
int sum(){
int a=10;
int b=20;
int result=a+b;
return result;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
28
Method (Contd.)
• An example to define a method with return type and with parameter:
int sum(int a, int b){
int result=a+b;
return result;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
29
Method (Contd.)
• An example to define a instance method and call the method using object:
public class SimpleCalculator {
int sum(int a, int b){
int result=a+b;
return result;
}
public static void main(String args[]){
SimpleCalculator obj=new SimpleCalculator();
int rVal=obj.sum(10,20);
System.out.println("The sum:"+rVal);
}
}
• In the preceding code, the dot operator is used to invoke the
instance method.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
30
Method (Contd.)
• You can also pass reference variables as arguments to a method as
shown in the following code:
class Points{
int x=10,y=-5;
}
public class MethodPORDemo {
void changePoints(Points p){
System.out.println("x="+p.x);
System.out.println("y="+p.y);
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
31
Method (Contd.)
p.x=-5;
p.y=10;
System.out.println("x="+p.x);
System.out.println("y="+p.y);
}
public static void main(String args[]){
Points pObj=new Points();
MethodPORDemo mObj=new MethodPORDemo();
mObj.changePoints(pObj);
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
32
Method (Contd.)
• The preceding code output will be:
x=10
y=-5
x=-5
y=10
• A method can also return a refernce type as shown in the following
code:
class Circle {
int radius=10;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
33
Method (Contd.)
public class MethodRODemo {
Circle getCircleObject(Circle cObj) {
cObj.radius=20;
return cObj;
}
public static void main(String args[]){
MethodRODemo mObj=new MethodRODemo();
Circle c1=new Circle();
System.out.println("Radius="+c1.radius);
Circle c2=mObj.getCircleObject(c1);
System.out.println("Radius="+c2.radius);
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
34
Method (Contd.)
• The preceding code output will be:
Radius=10
Radius=20
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
35
Constructor
• Constructor is a special type of method that has the same name of
the class and does not has any return type.
• The constructor is automatically called when an object is created.
• The constructor is used to initialize the instance variable of a class.
• An example to define a constructor without parameter:
class SimpleCalculator {
int operand1,operand2;
SimpleCalculator(){
operand1=5;
operand2=6;
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
36
Constructor (Contd.)
• An example to define a parameterized constructor:
class SimpleCalculator {
int operand1,operand2;
SimpleCalculator(int oprd1,int oprd2){
operand1=oprd1;
operand2=oprd2;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
37
Constructor (Contd.)
public static void main(String args[]){
SimpleCalculator obj1=new
SimpleCalculator(10,20);
SimpleCalculator obj2=new
SimpleCalculator(1,2);
System.out.println(obj1.operand1);
System.out.println(obj2.operand1);
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
38
Constructor (Contd.)
• The preceding code output will be:
10
1
• If a constructor is not defined explicitly, the compiler adds
an default constructor.
• A default constructor is a constructor without arguments.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
39
Constructor (Contd.)
• Consider the following code:
public class ConstructorDemo {
int x;
public ConstructorDemo(int a) {
x=a;
}
public static void main(String args[]){
ConstructorDemo cdObj=new ConstructorDemo();
}
}
• The preceding code will generate a compilation error, as the class
does not defines a no argument constructor.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
40
Encapsulation
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
41
Package
• A package is a namespace that organizes a set of related types, such as
classes and interfaces.
• The syntax to create a package is:
package <package name>;
• An example to create package:
package test;
class PackageDemo
{
}
• In the preceding code snippet, the PackageDemo class belongs to test
package.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
42
Package
• A class can access the classes in another package by importing the
required package.
• The syntax to import a package is:
import <package name>.*;
• An example to import a package:
import test.*;
class Demo{
}
• In the preceding code snippet, the Demo class can access the classes
inside test package.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
43
Access Modifiers
• Access level modifiers determine whether other classes can use a
particular field or invoke a particular method.
• Java has four access control levels, public, protected, default, and
private.
• But there are only three access modifiers, public, protected, and
private.
• When any of the three access modifier is not specified then the
default access control level is specified.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
44
Access Modifiers (Contd.)
• The following table shows the access to members permitted by each
modifier:
Modifier Class Package Subclass
public Y Y Y
protected Y Y Y
no modifier Y Y N
private Y N N
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
45
Access Modifiers (Contd.)
• All the four access modifiers can be used with the instance variables.
• The top level class can use only public and default modifier.
• The constructor can use all the four access modifiers.
• An example to work with access modifier:
package pack1;
public class Test {
public int testA;
private int testB;
protected int testC;
int testD;
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
46
Access Modifiers (Contd.)
Test(){
testA=1;
testB=2;
testC=3;
testD=4;
}
public Test(int a,int b, int c, int d){
testA=a;
testB=b;
testC=c;
testD=d;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
47
Access Modifiers (Contd.)
public void testPrint(){
System.out.println("public method testPrint");
}
private void testDisplay(){
System.out.println("private method testDisplay");
}
protected void testPut(){
System.out.println("protected method testPut");
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
48
Access Modifiers (Contd.)
package pack1;
class Sample {
public int sampleA;
private int sampleB;
protected int sampleC;
public Sample(int a,int b, int c){
sampleA=a;
sampleB=b;
sampleC=c;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
49
Access Modifiers (Contd.)
public void samplePrint(){
System.out.println("public method samplePrint");
}
private void sampleDisplay(){
System.out.println("private method sampleDisplay");
}
protected void samplePut(){
System.out.println("protected method samplePut");
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
50
Access Modifiers (Contd.)
package pack2;
import pack1.*;
public class Demo {
public static void main(String args[]){
Test testObj=new Test(10,20,30,40);
System.out.println(testObj.testA);
testObj.testPrint();
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
51
Access Modifiers (Contd.)
• The preceding code output will be:
10
public method testPrint
• In the preceding code, inside the Demo class Sample class cannot be
accessed because the Sample class has default access control.
• Similarly, the variable, testB, testC, and testD are not accessible as
they have private, protected, and default access control.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
52
Encapsulation Implementation
• To create a Java program that supports maintainability, flexibility, and
extensibility, then it must include encapsulation.
• Encapsulation is implemented by:
• Protecting the instance variable.
• Make public accessor methods to access the instance variable.
• An example to implement encapsulation:
class Rectangle {
private int length;
private int breadth;
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
53
Encapsulation Implementation (Contd.)
public int getLength() {
return length;
}
public void setLength(int l) {
length = l;
}
public int getBreadth() {
return breadth;
}
public void setBreadth(int b) {
breadth = b;
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
54
Encapsulation Implementation (Contd.)
public class EncapsulationDemo {
public static void main(String args[]){
Rectangle r=new Rectangle();
r.setLength(10);
r.setBreadth(20);
}
}
• The preceding code forces to access the instance variable to be
accessed using methods. This methodology allows to modify the
implementation later without affecting the other classes which uses
the instance variables.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
55
Polymorphism
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
56
Method Overloading
• If two or more method in a class have same name but different
parameters, it is known as method overloading.
• Method overloading is one of the ways through which Java supports
polymorphism.
• An example to implement method overloading:
public class SimpleCalculator {
void add(int a, int b){
int result=a+b;
System.out.println("The sum:"+result);
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
57
Method Overloading (Contd.)
void add(int a, int b,int c){
int result=a+b+c;
System.out.println("The sum:"+result);
}
}
void add(double a, double b){
double result=a+b;
System.out.println("The sum:"+result);
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
58
Method Overloading (Contd.)
public static void main(String args[]){
SimpleCalculator obj=new SimpleCalculator();
obj.add(1,2);
obj.add(1,2,3);
obj.add(23.5,34.7);
}
}
• The preceding code output will be:
The sum:3
The sum:6
The sum:58.2
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
59
Method Overloading (Contd.)
• The following code snippet is a non-example of method overloading:
public void print(){
}
private void print(){
}
• The following code snippet is an example of method overloading:
public void print(){
}
private void print(int x){
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
60
Method Overloading (Contd.)
• The following code snippet is a non-example of method overloading:
public void print(){
}
private int print(){
}
• The following code snippet is an example of method overloading:
public void print(){
}
private int print(int x){
return 1;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
61
Constructor Overloading
• When more than one constructor is defined in a class, it is known as
constructor overloading.
• An example to implement constructor overloading:
public class SimpleCalculator {
int operand1,operand2,result;
SimpleCalculator(){
operand1=1;
operand2=2;
}
SimpleCalculator(int oprd1, int oprd2){
operand1=oprd1;
operand2=oprd2;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
62
Constructor Overloading (Contd.)
void sum(){
result=operand1+operand2;
System.out.println("The sum: "+result);
}
public static void main(String args[]){
SimpleCalculator obj1=new SimpleCalculator();
obj1.sum();
SimpleCalculator obj2=new SimpleCalculator(12,45);
obj2.sum();
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
63
Constructor Overloading (Contd.)
• The preceding code output will be:
The sum: 3
The sum: 57
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
64
Variable Argument
• Variable argument allows the method to accept zero or multiple
arguments.
• The syntax to define a variable argument method:
<return type> <method name>(<data type>…
<variablename>){
}
• An example to define a variable argument method:
void display(int…a){
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
65
Variable Argument (Contd.)
• An example to implement variable argument:
public class Demo{
void display(int...a){
int count=0;
for(int x:a){
count++;
}
System.out.println("The number arguments:"+count);
}
public static void main(String args[]){
Demo obj=new Demo();
obj.display();
obj.display(1);
obj.display(1,2);
obj.display(1,2,3);
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
66
Variable Argument (Contd.)
• The preceding code output will be:
The number arguments:0
The number arguments:1
The number arguments:2
The number arguments:3
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
67
Variable Argument (Contd.)
• Rules to work with variable argument:
• A method can have only one variable argument parameter.
• The variable argument must be the last parameter of the method.
• The following code snippet is a non-example of variable argument
implementation:
void display(int...a, int...b){ }
void display(int...a, int b){ }
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
68
Variable Argument and Overloading
• Consider a scenario, where you need to pass many instance of same
object to a method. In such situation, the programmer has two
choices:
• Create an overloaded method
• Create a method that accepts array or other collections
• However, at compile time if you do not know how many instance will
be used creating an overloaded method becomes difficult.
• Working with arrays and other collections makes the code lengthy
and complex.
• Therefore, variable arguments were introduced to simplify the task of
the programmer.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
69
Use of this Keyword
• The this keyword is used to refer the current object in the code.
• Consider the following code:
public class SimpleCalculator {
SimpleCalculator(int operand1, int operand2){
}
• In the preceding code snippet, the instance variable and the
parameter variable of the constructor have the same name.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
70
Use of this Keyword (Contd.)
• In such situation, to use the instance variable inside the constructor
the this keyword is used as shown in the following code snippet:
SimpleCalculator(int operand1, int operand2){
this.operand1=operand1;
this.operand2=operand2;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
71
Use of this Keyword (Contd.)
• An example to work with this keyword:
public class SimpleCalculator {
int operand1,operand2,result;
SimpleCalculator(){
operand1=10;
operand2=20;
}
SimpleCalculator(int operand1, int operand2){
this.operand1=operand1;
this.operand2=operand2;
}
int sum(){
result=operand1+operand2;
return result;
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
72
Use of this Keyword (Contd.)
public static void main(String args[]){
SimpleCalculator obj=new SimpleCalculator(102,52);
int rVal=obj.sum();
System.out.println("The sum:"+rVal);
}
}
• The preceding code output will be:
The sum:154
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
73
Use of this Keyword (Contd.)
• An example to work with constructor chaining using this keyword:
class Flower{
String flowerName;
Flower(){
this("Rose");
}
Flower(String flowerName){
this.flowerName=flowerName;
}
}
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
74
Use of this Keyword (Contd.)
public class ConstructorDemo {
public static void main(String args[]){
Flower fObj=new Flower();
System.out.println(fObj.flowerName);
}
}
• The preceding code output will be:
Rose
• The constructor chaining avoids code duplication.
• Note: The this() statement should be the first line in the constructor
definition.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
75
Summary
• You have learnt that:
• The object-oriented is a programming paradigm where the program logic and
data are weaved.
• The characteristic of OOPs are class, object, polymorphism, encapsulation,
and inheritance.
• An entity that has state and behavior is known as an object.
• A class is a collection of objects and a corresponding set of methods.
• Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse.
• Inheritance is the process by which one object acquires the properties of
another object.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
76
Summary (Contd.)
• Polymorphism is the capability of an action or method to do different things
based on the object that it is acting upon.
• Define class, constructor and method.
• Create object and instance variable.
• A package is a namespace that organizes a set of related types, such as classes
and interfaces.
• Access level modifiers determine whether other classes can use a particular
field or invoke a particular method.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
77
Summary
• If two or more method in a class have same name but different parameters, it
is known as method overloading.
• When more than one constructor is defined in a class, it is known as
constructor overloading.
• Variable argument allows the method to accept zero or multiple arguments.
© People Strategists - Distribution is strictly prohibited -
www.peoplestrategists.com
78

More Related Content

What's hot

Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3PawanMM
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceOUM SAOKOSAL
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedPawanMM
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Sagar Verma
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Exception Handling - Continued
Exception Handling - Continued Exception Handling - Continued
Exception Handling - Continued Hitesh-Java
 
3java Advanced Oop
3java Advanced Oop3java Advanced Oop
3java Advanced OopAdil Jafri
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectOUM SAOKOSAL
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Sagar Verma
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2Mudasir Syed
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Session 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsSession 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsPawanMM
 
Interactive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and ArchitectureInteractive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and ArchitectureJavaDayUA
 

What's hot (19)

Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continued
 
Java oop
Java oopJava oop
Java oop
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
 
04inherit
04inherit04inherit
04inherit
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Exception Handling - Continued
Exception Handling - Continued Exception Handling - Continued
Exception Handling - Continued
 
3java Advanced Oop
3java Advanced Oop3java Advanced Oop
3java Advanced Oop
 
Java Programming - 05 access control in java
Java Programming - 05 access control in javaJava Programming - 05 access control in java
Java Programming - 05 access control in java
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Cfphp Zce 01 Basics
Cfphp Zce 01 BasicsCfphp Zce 01 Basics
Cfphp Zce 01 Basics
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Session 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java InterviewsSession 18 - Review Session and Attending Java Interviews
Session 18 - Review Session and Attending Java Interviews
 
Interactive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and ArchitectureInteractive Java Support to your tool -- The JShell API and Architecture
Interactive Java Support to your tool -- The JShell API and Architecture
 

Viewers also liked

Java data structures for principled programmer
Java data structures for principled programmerJava data structures for principled programmer
Java data structures for principled programmerspnr15z
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)Arvind Devaraj
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercisesagorolabs
 
Introduction to Agile
Introduction to AgileIntroduction to Agile
Introduction to Agileagorolabs
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5Bianca Teşilă
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)Waheed Khalid
 
Java 101 Intro to Java Programming
Java 101 Intro to Java ProgrammingJava 101 Intro to Java Programming
Java 101 Intro to Java Programmingagorolabs
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Javaagorolabs
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 
Computer Programming Overview
Computer Programming OverviewComputer Programming Overview
Computer Programming Overviewagorolabs
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questionsrithustutorials
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2jomerson remorosa
 
Ebook En Sams Data Structures & Algorithms In Java
Ebook   En  Sams   Data Structures & Algorithms In JavaEbook   En  Sams   Data Structures & Algorithms In Java
Ebook En Sams Data Structures & Algorithms In JavaAldo Quelopana
 
Java 101 Intro to Java Programming - Exercises
Java 101   Intro to Java Programming - ExercisesJava 101   Intro to Java Programming - Exercises
Java 101 Intro to Java Programming - Exercisesagorolabs
 
Objects & OO Thinking for Java
Objects & OO Thinking for JavaObjects & OO Thinking for Java
Objects & OO Thinking for JavaJeff Sonstein
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
 

Viewers also liked (20)

Enum Report
Enum ReportEnum Report
Enum Report
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Java data structures for principled programmer
Java data structures for principled programmerJava data structures for principled programmer
Java data structures for principled programmer
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercises
 
Introduction to Agile
Introduction to AgileIntroduction to Agile
Introduction to Agile
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Java 101 Intro to Java Programming
Java 101 Intro to Java ProgrammingJava 101 Intro to Java Programming
Java 101 Intro to Java Programming
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Java
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Computer Programming Overview
Computer Programming OverviewComputer Programming Overview
Computer Programming Overview
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 
Ebook En Sams Data Structures & Algorithms In Java
Ebook   En  Sams   Data Structures & Algorithms In JavaEbook   En  Sams   Data Structures & Algorithms In Java
Ebook En Sams Data Structures & Algorithms In Java
 
Java 101 Intro to Java Programming - Exercises
Java 101   Intro to Java Programming - ExercisesJava 101   Intro to Java Programming - Exercises
Java 101 Intro to Java Programming - Exercises
 
Objects & OO Thinking for Java
Objects & OO Thinking for JavaObjects & OO Thinking for Java
Objects & OO Thinking for Java
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 

Similar to Java Day-2

Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Software Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-iSoftware Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-iTaymoor Nazmy
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP conceptsAhmed Farag
 
Handout on Object orienetd Analysis and Design
Handout on Object orienetd Analysis and DesignHandout on Object orienetd Analysis and Design
Handout on Object orienetd Analysis and DesignSAFAD ISMAIL
 
1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptxumarAnjum6
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
OOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptxOOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptxuzairrrfr
 
M01_OO_Intro.ppt
M01_OO_Intro.pptM01_OO_Intro.ppt
M01_OO_Intro.pptRojaPogul1
 
Design Pattern lecture 4
Design Pattern lecture 4Design Pattern lecture 4
Design Pattern lecture 4Julie Iskander
 
Oo concepts and class modeling
Oo concepts and class modelingOo concepts and class modeling
Oo concepts and class modelingPreeti Mishra
 
Object Modelling Technique " ooad "
Object Modelling Technique  " ooad "Object Modelling Technique  " ooad "
Object Modelling Technique " ooad "AchrafJbr
 

Similar to Java Day-2 (20)

Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Ooad ch 1_2
Ooad ch 1_2Ooad ch 1_2
Ooad ch 1_2
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
80410172053.pdf
80410172053.pdf80410172053.pdf
80410172053.pdf
 
Software Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-iSoftware Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-i
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
 
Cs2305 programming paradigms lecturer notes
Cs2305   programming paradigms lecturer notesCs2305   programming paradigms lecturer notes
Cs2305 programming paradigms lecturer notes
 
Software Design principales
Software Design principalesSoftware Design principales
Software Design principales
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Handout on Object orienetd Analysis and Design
Handout on Object orienetd Analysis and DesignHandout on Object orienetd Analysis and Design
Handout on Object orienetd Analysis and Design
 
1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
OOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptxOOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptx
 
M01_OO_Intro.ppt
M01_OO_Intro.pptM01_OO_Intro.ppt
M01_OO_Intro.ppt
 
Design Pattern lecture 4
Design Pattern lecture 4Design Pattern lecture 4
Design Pattern lecture 4
 
Lecture 1.pptx
Lecture 1.pptxLecture 1.pptx
Lecture 1.pptx
 
Oo concepts and class modeling
Oo concepts and class modelingOo concepts and class modeling
Oo concepts and class modeling
 
Ooad ch 2
Ooad ch 2Ooad ch 2
Ooad ch 2
 
Object Modelling Technique " ooad "
Object Modelling Technique  " ooad "Object Modelling Technique  " ooad "
Object Modelling Technique " ooad "
 
1. oose
1. oose1. oose
1. oose
 

More from People Strategists (20)

MongoDB Session 3
MongoDB Session 3MongoDB Session 3
MongoDB Session 3
 
MongoDB Session 2
MongoDB Session 2MongoDB Session 2
MongoDB Session 2
 
MongoDB Session 1
MongoDB Session 1MongoDB Session 1
MongoDB Session 1
 
Android - Day 1
Android - Day 1Android - Day 1
Android - Day 1
 
Android - Day 2
Android - Day 2Android - Day 2
Android - Day 2
 
Overview of web services
Overview of web servicesOverview of web services
Overview of web services
 
Spring Framework - III
Spring Framework - IIISpring Framework - III
Spring Framework - III
 
Spring Framework-II
Spring Framework-IISpring Framework-II
Spring Framework-II
 
Spring Framework -I
Spring Framework -ISpring Framework -I
Spring Framework -I
 
Hibernate II
Hibernate IIHibernate II
Hibernate II
 
Hibernate III
Hibernate IIIHibernate III
Hibernate III
 
Hibernate I
Hibernate IHibernate I
Hibernate I
 
Identifing Listeners and Filters
Identifing Listeners and FiltersIdentifing Listeners and Filters
Identifing Listeners and Filters
 
Exploring Maven SVN GIT
Exploring Maven SVN GITExploring Maven SVN GIT
Exploring Maven SVN GIT
 
Agile Dev. II
Agile Dev. IIAgile Dev. II
Agile Dev. II
 
Agile Dev. I
Agile Dev. IAgile Dev. I
Agile Dev. I
 
Working with Servlets
Working with ServletsWorking with Servlets
Working with Servlets
 
Overview of JEE Technology
Overview of JEE TechnologyOverview of JEE Technology
Overview of JEE Technology
 
JSP Technology II
JSP Technology IIJSP Technology II
JSP Technology II
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 

Recently uploaded

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

Java Day-2

  • 1. © People Strategists - Duplication is strictly prohibited - www.peoplestrategists.com 1
  • 2. Object Oriented Programming © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 2
  • 3. DAY 2 Object Oriented Programming Objects And Classes Encapsulation Polymorphism © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 3
  • 4. Introduction to Object-Oriented Programming • The object-oriented is a programming paradigm where the program logic and data are weaved. • OOPs is a way of conceptualizing a program's data into discrete "things" referred to as objects, each having its own properties and methods. • For example, your friend is a bank manager and he wants you to help improving systems. The first object you might design is the general-purpose Account. The Account object has properties and methods. For each client your friend's bank have, you would have to create an Account object. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 4
  • 5. Introduction to Object-Oriented Programming (Contd.) Object Characteristics of OOP Class Encapsulation Inheritance Polymorphism © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 5
  • 6. Introduction to Object-Oriented Programming (Contd.) • Benefits of OOP: • Re-usability: You can write a program using a previous developed code. • Code Sharing: You are able to standardize the way your are programming with your colleagues. • Rapid Modeling: You can prototype the classes and their interaction through a diagram. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 6
  • 7. Objects • An entity that has state and behavior is known as an object, such as chair, bike, marker, pen, table, and car. • It can be physical or logical (tangible and intangible). • The example of intangible object is banking system. • An object has three characteristics: • State: Represents data (value) of an object. • Behavior: Represents the behavior (functionality or action) of an object, such as deposit and withdraw. • Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 7
  • 8. Objects (Contd.) • At any point in time, an object can be described from the data it contains. • An object is an instance of a class. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 8
  • 9. Objects (Contd.) • Example: Car Objet States: Brand Model Price Color Behaviors: Starting car Stopping car Changing gear Applying brake Get km/liter © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 9
  • 10. Classes • A class is a collection of objects (or values) and a corresponding set of methods. • Objects contain data and methods, but to use objects in a program, a class needs to be created. • A class lets us define what data is going to be stored in the object, how it can be accessed and modified, and what actions can be performed on it. • Thus, a class is a template for an object. • Example: A set of cars with operations, such as starting, stopping, changing gear, applying brake, and get km/liter. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 10
  • 11. Encapsulation and Access Control • Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. • A protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. • Access to the code and data inside the wrapper is tightly controlled through a well-defined interface. • The basis of encapsulation is the class. • When you create a class, you will specify the code and data that constitute that class. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 11
  • 12. Encapsulation and Access Control(Contd.) • The data defined by the class are referred to as member variables or instance variables. • The code that operates on that data is referred to as member methods. • The complexity of the implementation inside the class can be hided. • Each method or variable in a class may be marked private or public. • The private methods and data can only be accessed by code that is a member of the class. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 12
  • 13. Encapsulation and Access Control(Contd.) • The following figure describes conceptual representation of encapsulation concept: © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 13
  • 14. Inheritance • Inheritance is the process by which one object acquires the properties of another object. • It supports the concept of hierarchical classification. • For example, a Golden Retriever is part of the classification dog, which in turn is part of the mammal class, which is under the larger class animal. • Without the use of hierarchies, each object would need to define all of its characteristics explicitly. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 14
  • 15. Inheritance (Contd.) • By use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. • It is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case. • Inheritance interacts with encapsulation as well. • If a given class encapsulates some attributes, then any subclass will have the same attributes plus any that it adds as part of its specialization. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 15
  • 16. Inheritance (Contd.) • A new subclass inherits all of the attributes of all of its ancestors. • The following figure describes conceptual representation of inheritance concept: © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 16
  • 17. Polymorphism • Derived from two Latin words - Poly, which means many, and morph, which means forms. • Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. • In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 17
  • 18. Polymorphism, Encapsulation, and Inheritance Work Together (Contd.) • The following figure describes conceptual representation of polymorphism, encapsulation, and inheritance working together: Shape Draw() Triangle Draw() Rectangle Draw() Circle Draw() © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 18
  • 19. Objects And Classes © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 19
  • 20. Defining a Class • The syntax to define a class: class <class_name> { …… } • An example to define a class: class SimpleCalculator { } • In the preceding code snippet is used to define SimpleCalulator class. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 20
  • 21. Creating an Object • The syntax to create an object: <classname> <object ref variable> = new <classname>(); • An example to create an object: SimpleCalculator obj=new SimpleCalculator(); • In the preceding code snippet, a object reference variable named obj of type SimpleCalculator is created. The new operator creates a new SimpleCalculator object on the heap memory and assigns this object to the reference variable, obj. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 21
  • 22. Creating an Object (Contd.) • You can also assign null to an object reference variable as shown in the following code snippet: SimpleCalculator obj=null; • The preceding code snippet creates an object reference variable but it does not refers any object. Here, only a space for the object reference variable is created but an actual SimpleCalculator object is not created. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 22
  • 23. Instance Variable • Instance variable is a variable defined inside the class, but outside any method. • Each instantiated object has a separate copy of instance variable. • If the instance variables are not initialized, they will have the default value. • An example to create instance variable: class SimpleCalculator { int operand1, operand2,result; } • In the preceding code snippet, operand1,operand2, and result are the instance variables of SimpleCalculator class. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 23
  • 24. Method • A method is a block of statements that has a name and can be executed by calling. • A method is defined to preform certain task. • The syntax to define a method: <returntype> <methodname>(parameterlist){ } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 24
  • 25. Method (Contd.) • The different types of methods are: • Method without return type and without parameter • Method without return type and with parameter • Method with return type and without parameter • Method with return type and with parameter. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 25
  • 26. Method (Contd.) • An example to define a method without return type and without parameter: void sum(){ int a=10; int b=20; int result=a+b; System.out.println(“The sum:”+result); } • If a method does not return any value, then the return type should be set to void. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 26
  • 27. Method (Contd.) • An example to define a method without return type and with parameter: void sum(int a, int b){ int result=a+b; System.out.println(“The sum :”+result); } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 27
  • 28. Method (Contd.) • An example to define a method with return type and without parameter: int sum(){ int a=10; int b=20; int result=a+b; return result; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 28
  • 29. Method (Contd.) • An example to define a method with return type and with parameter: int sum(int a, int b){ int result=a+b; return result; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 29
  • 30. Method (Contd.) • An example to define a instance method and call the method using object: public class SimpleCalculator { int sum(int a, int b){ int result=a+b; return result; } public static void main(String args[]){ SimpleCalculator obj=new SimpleCalculator(); int rVal=obj.sum(10,20); System.out.println("The sum:"+rVal); } } • In the preceding code, the dot operator is used to invoke the instance method. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 30
  • 31. Method (Contd.) • You can also pass reference variables as arguments to a method as shown in the following code: class Points{ int x=10,y=-5; } public class MethodPORDemo { void changePoints(Points p){ System.out.println("x="+p.x); System.out.println("y="+p.y); © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 31
  • 32. Method (Contd.) p.x=-5; p.y=10; System.out.println("x="+p.x); System.out.println("y="+p.y); } public static void main(String args[]){ Points pObj=new Points(); MethodPORDemo mObj=new MethodPORDemo(); mObj.changePoints(pObj); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 32
  • 33. Method (Contd.) • The preceding code output will be: x=10 y=-5 x=-5 y=10 • A method can also return a refernce type as shown in the following code: class Circle { int radius=10; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 33
  • 34. Method (Contd.) public class MethodRODemo { Circle getCircleObject(Circle cObj) { cObj.radius=20; return cObj; } public static void main(String args[]){ MethodRODemo mObj=new MethodRODemo(); Circle c1=new Circle(); System.out.println("Radius="+c1.radius); Circle c2=mObj.getCircleObject(c1); System.out.println("Radius="+c2.radius); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 34
  • 35. Method (Contd.) • The preceding code output will be: Radius=10 Radius=20 © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 35
  • 36. Constructor • Constructor is a special type of method that has the same name of the class and does not has any return type. • The constructor is automatically called when an object is created. • The constructor is used to initialize the instance variable of a class. • An example to define a constructor without parameter: class SimpleCalculator { int operand1,operand2; SimpleCalculator(){ operand1=5; operand2=6; } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 36
  • 37. Constructor (Contd.) • An example to define a parameterized constructor: class SimpleCalculator { int operand1,operand2; SimpleCalculator(int oprd1,int oprd2){ operand1=oprd1; operand2=oprd2; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 37
  • 38. Constructor (Contd.) public static void main(String args[]){ SimpleCalculator obj1=new SimpleCalculator(10,20); SimpleCalculator obj2=new SimpleCalculator(1,2); System.out.println(obj1.operand1); System.out.println(obj2.operand1); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 38
  • 39. Constructor (Contd.) • The preceding code output will be: 10 1 • If a constructor is not defined explicitly, the compiler adds an default constructor. • A default constructor is a constructor without arguments. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 39
  • 40. Constructor (Contd.) • Consider the following code: public class ConstructorDemo { int x; public ConstructorDemo(int a) { x=a; } public static void main(String args[]){ ConstructorDemo cdObj=new ConstructorDemo(); } } • The preceding code will generate a compilation error, as the class does not defines a no argument constructor. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 40
  • 41. Encapsulation © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 41
  • 42. Package • A package is a namespace that organizes a set of related types, such as classes and interfaces. • The syntax to create a package is: package <package name>; • An example to create package: package test; class PackageDemo { } • In the preceding code snippet, the PackageDemo class belongs to test package. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 42
  • 43. Package • A class can access the classes in another package by importing the required package. • The syntax to import a package is: import <package name>.*; • An example to import a package: import test.*; class Demo{ } • In the preceding code snippet, the Demo class can access the classes inside test package. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 43
  • 44. Access Modifiers • Access level modifiers determine whether other classes can use a particular field or invoke a particular method. • Java has four access control levels, public, protected, default, and private. • But there are only three access modifiers, public, protected, and private. • When any of the three access modifier is not specified then the default access control level is specified. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 44
  • 45. Access Modifiers (Contd.) • The following table shows the access to members permitted by each modifier: Modifier Class Package Subclass public Y Y Y protected Y Y Y no modifier Y Y N private Y N N © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 45
  • 46. Access Modifiers (Contd.) • All the four access modifiers can be used with the instance variables. • The top level class can use only public and default modifier. • The constructor can use all the four access modifiers. • An example to work with access modifier: package pack1; public class Test { public int testA; private int testB; protected int testC; int testD; © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 46
  • 47. Access Modifiers (Contd.) Test(){ testA=1; testB=2; testC=3; testD=4; } public Test(int a,int b, int c, int d){ testA=a; testB=b; testC=c; testD=d; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 47
  • 48. Access Modifiers (Contd.) public void testPrint(){ System.out.println("public method testPrint"); } private void testDisplay(){ System.out.println("private method testDisplay"); } protected void testPut(){ System.out.println("protected method testPut"); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 48
  • 49. Access Modifiers (Contd.) package pack1; class Sample { public int sampleA; private int sampleB; protected int sampleC; public Sample(int a,int b, int c){ sampleA=a; sampleB=b; sampleC=c; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 49
  • 50. Access Modifiers (Contd.) public void samplePrint(){ System.out.println("public method samplePrint"); } private void sampleDisplay(){ System.out.println("private method sampleDisplay"); } protected void samplePut(){ System.out.println("protected method samplePut"); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 50
  • 51. Access Modifiers (Contd.) package pack2; import pack1.*; public class Demo { public static void main(String args[]){ Test testObj=new Test(10,20,30,40); System.out.println(testObj.testA); testObj.testPrint(); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 51
  • 52. Access Modifiers (Contd.) • The preceding code output will be: 10 public method testPrint • In the preceding code, inside the Demo class Sample class cannot be accessed because the Sample class has default access control. • Similarly, the variable, testB, testC, and testD are not accessible as they have private, protected, and default access control. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 52
  • 53. Encapsulation Implementation • To create a Java program that supports maintainability, flexibility, and extensibility, then it must include encapsulation. • Encapsulation is implemented by: • Protecting the instance variable. • Make public accessor methods to access the instance variable. • An example to implement encapsulation: class Rectangle { private int length; private int breadth; © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 53
  • 54. Encapsulation Implementation (Contd.) public int getLength() { return length; } public void setLength(int l) { length = l; } public int getBreadth() { return breadth; } public void setBreadth(int b) { breadth = b; } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 54
  • 55. Encapsulation Implementation (Contd.) public class EncapsulationDemo { public static void main(String args[]){ Rectangle r=new Rectangle(); r.setLength(10); r.setBreadth(20); } } • The preceding code forces to access the instance variable to be accessed using methods. This methodology allows to modify the implementation later without affecting the other classes which uses the instance variables. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 55
  • 56. Polymorphism © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 56
  • 57. Method Overloading • If two or more method in a class have same name but different parameters, it is known as method overloading. • Method overloading is one of the ways through which Java supports polymorphism. • An example to implement method overloading: public class SimpleCalculator { void add(int a, int b){ int result=a+b; System.out.println("The sum:"+result); } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 57
  • 58. Method Overloading (Contd.) void add(int a, int b,int c){ int result=a+b+c; System.out.println("The sum:"+result); } } void add(double a, double b){ double result=a+b; System.out.println("The sum:"+result); } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 58
  • 59. Method Overloading (Contd.) public static void main(String args[]){ SimpleCalculator obj=new SimpleCalculator(); obj.add(1,2); obj.add(1,2,3); obj.add(23.5,34.7); } } • The preceding code output will be: The sum:3 The sum:6 The sum:58.2 © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 59
  • 60. Method Overloading (Contd.) • The following code snippet is a non-example of method overloading: public void print(){ } private void print(){ } • The following code snippet is an example of method overloading: public void print(){ } private void print(int x){ } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 60
  • 61. Method Overloading (Contd.) • The following code snippet is a non-example of method overloading: public void print(){ } private int print(){ } • The following code snippet is an example of method overloading: public void print(){ } private int print(int x){ return 1; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 61
  • 62. Constructor Overloading • When more than one constructor is defined in a class, it is known as constructor overloading. • An example to implement constructor overloading: public class SimpleCalculator { int operand1,operand2,result; SimpleCalculator(){ operand1=1; operand2=2; } SimpleCalculator(int oprd1, int oprd2){ operand1=oprd1; operand2=oprd2; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 62
  • 63. Constructor Overloading (Contd.) void sum(){ result=operand1+operand2; System.out.println("The sum: "+result); } public static void main(String args[]){ SimpleCalculator obj1=new SimpleCalculator(); obj1.sum(); SimpleCalculator obj2=new SimpleCalculator(12,45); obj2.sum(); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 63
  • 64. Constructor Overloading (Contd.) • The preceding code output will be: The sum: 3 The sum: 57 © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 64
  • 65. Variable Argument • Variable argument allows the method to accept zero or multiple arguments. • The syntax to define a variable argument method: <return type> <method name>(<data type>… <variablename>){ } • An example to define a variable argument method: void display(int…a){ } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 65
  • 66. Variable Argument (Contd.) • An example to implement variable argument: public class Demo{ void display(int...a){ int count=0; for(int x:a){ count++; } System.out.println("The number arguments:"+count); } public static void main(String args[]){ Demo obj=new Demo(); obj.display(); obj.display(1); obj.display(1,2); obj.display(1,2,3); } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 66
  • 67. Variable Argument (Contd.) • The preceding code output will be: The number arguments:0 The number arguments:1 The number arguments:2 The number arguments:3 © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 67
  • 68. Variable Argument (Contd.) • Rules to work with variable argument: • A method can have only one variable argument parameter. • The variable argument must be the last parameter of the method. • The following code snippet is a non-example of variable argument implementation: void display(int...a, int...b){ } void display(int...a, int b){ } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 68
  • 69. Variable Argument and Overloading • Consider a scenario, where you need to pass many instance of same object to a method. In such situation, the programmer has two choices: • Create an overloaded method • Create a method that accepts array or other collections • However, at compile time if you do not know how many instance will be used creating an overloaded method becomes difficult. • Working with arrays and other collections makes the code lengthy and complex. • Therefore, variable arguments were introduced to simplify the task of the programmer. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 69
  • 70. Use of this Keyword • The this keyword is used to refer the current object in the code. • Consider the following code: public class SimpleCalculator { SimpleCalculator(int operand1, int operand2){ } • In the preceding code snippet, the instance variable and the parameter variable of the constructor have the same name. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 70
  • 71. Use of this Keyword (Contd.) • In such situation, to use the instance variable inside the constructor the this keyword is used as shown in the following code snippet: SimpleCalculator(int operand1, int operand2){ this.operand1=operand1; this.operand2=operand2; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 71
  • 72. Use of this Keyword (Contd.) • An example to work with this keyword: public class SimpleCalculator { int operand1,operand2,result; SimpleCalculator(){ operand1=10; operand2=20; } SimpleCalculator(int operand1, int operand2){ this.operand1=operand1; this.operand2=operand2; } int sum(){ result=operand1+operand2; return result; } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 72
  • 73. Use of this Keyword (Contd.) public static void main(String args[]){ SimpleCalculator obj=new SimpleCalculator(102,52); int rVal=obj.sum(); System.out.println("The sum:"+rVal); } } • The preceding code output will be: The sum:154 © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 73
  • 74. Use of this Keyword (Contd.) • An example to work with constructor chaining using this keyword: class Flower{ String flowerName; Flower(){ this("Rose"); } Flower(String flowerName){ this.flowerName=flowerName; } } © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 74
  • 75. Use of this Keyword (Contd.) public class ConstructorDemo { public static void main(String args[]){ Flower fObj=new Flower(); System.out.println(fObj.flowerName); } } • The preceding code output will be: Rose • The constructor chaining avoids code duplication. • Note: The this() statement should be the first line in the constructor definition. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 75
  • 76. Summary • You have learnt that: • The object-oriented is a programming paradigm where the program logic and data are weaved. • The characteristic of OOPs are class, object, polymorphism, encapsulation, and inheritance. • An entity that has state and behavior is known as an object. • A class is a collection of objects and a corresponding set of methods. • Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. • Inheritance is the process by which one object acquires the properties of another object. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 76
  • 77. Summary (Contd.) • Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. • Define class, constructor and method. • Create object and instance variable. • A package is a namespace that organizes a set of related types, such as classes and interfaces. • Access level modifiers determine whether other classes can use a particular field or invoke a particular method. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 77
  • 78. Summary • If two or more method in a class have same name but different parameters, it is known as method overloading. • When more than one constructor is defined in a class, it is known as constructor overloading. • Variable argument allows the method to accept zero or multiple arguments. © People Strategists - Distribution is strictly prohibited - www.peoplestrategists.com 78