Prepared by
AbhinavJain
NehaDubey
Department Of CSE
JAVA PROGRAMMING
OOPs (Object Oriented Programming System)
⚫Object meansa real word entity suchas pen, chair, table
etc. Object-Oriented Programming is amethodology or
paradigm to designaprogram usingclasses and objects. It
simplifies the software development and maintenance by
providing some concepts:
⚫Object
⚫Class
⚫Inheritance
⚫Polymorphism
⚫Abstraction
⚫Encapsulation
Characteristics of OOP
Object
⚫Anyentity that has state and behavior is known as an object.
For example: chair, pen, table, keyboard, bike etc. It can be
physical and logical.
Class
⚫Collection of objects is called class.It is alogical entity.
Inheritance
⚫When one object acquires all the properties and
behaviours of parent object i.e. known asinheritance. It
provides code reusability.It is used to achieveruntime
polymorphism.
Characteristics of OOP
Polymorphism
⚫When one task is performed by different ways i.e.
knownas polymorphism. For example:to convense the
customer differently,to draw something e.g.shapeor
rectangle etc.
⚫In java,we use method overloading and method overriding to
achieve polymorphism.
Abstraction
⚫Hiding internal details and showing functionality is
knownasabstraction. For example:phone call, we don't know
the internal processing.
⚫In java,we use abstract class and interface to achieve
abstraction.
Characteristics of OOP
Encapsulation
⚫Binding (or wrapping) code and data together into
a single unit is known as encapsulation. For example:
capsule, it is wrapped with different medicines.
⚫Ajavaclass is the example of encapsulation.Javabean is the
fully encapsulated class because all the data members are
private here.
What is difference between object-oriented
programming language and object-based
programminglanguage?
⚫Object based programming language
follows all the features of OOPs except
Inheritance. JavaScript andVBScript are
examples of object based programming
languages.
Naming convention
Name Convention
class name Should begin with uppercase letter and be a noun e.g. String
,System,Thread etc
Interface
name
Should begin with uppercase letter and be an
adjective(wherever possible) e.g.
Runnable(),ActionListener() etc
method name Should begin with lowercase letter and be a verb.
e.g. main(), print(), println(),
actionListenerPerformed()
V
ariable name Should begin with lowercase letter e.g. firstName,orderNumber
etc
Package name Should be in lowercase letter e.g. java,lang,sql,util etc
Constants
name
Should be in uppercase letter. E.g.
RED,YELLOW
,MAX_PRIORITY etc
Object
• An entity that has state and behavior
is known as an object
• An object is an instance of a class. A
class is a template or blueprint from
which objects are created. So, an
object is the instance(result) of a
class.
• ForExample:Penisanobject.Its nameis
Reynolds,color iswhite etc. knownasits
state.It isusedto write,sowriting isits
behaviour.
• Object is an instance of a class. Class
is a template or blueprint from which
objects are created. So object is the
instance(result) ofa class.
Class
⚫ Aclass is agroup of objects that have common property. It is a template or
blueprint from which objects are created.Aclass in javacan contain:
⚫ data member
⚫ method
⚫ constructor
⚫ block
⚫ Avariable that is created inside the class but outside the method, is known as
instance variable.Instance variable doesn't get memory at compile time.It
gets memory at runtime when object(instance) is created.That is why,it is
known as instance variable.
⚫ Method
⚫ In java,a method is like function i.e. used to expose behaviour of an object.
⚫ Advantage of Method
⚫ Code Reusability
⚫ Code Optimization
Class
• A class is a group of
objects which have
common properties.
• A class in Java can
contain:
– Fields
– Methods
– Constructors
– Blocks
What are the different waysto create an object in Java?
⚫There are many waysto create an object in java.They are:
⚫Bynew keyword
⚫BynewInstance() method
⚫Byclone() method
⚫Byfactorymethod etc.
⚫new keyword
⚫The new keyword is used to allocate memory at runtime.
Annonymous object
⚫ Annonymous simplymeans nameless.An object that have no reference is
known asannonymous object.If you haveto use an object only once,
annonymous object is agood approach.
class Calculation{
void fact(int n){
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
System.out.println("factorial is "+fact);
}
public static void main(String args[]){
new Calculation().fact(5);/ / calling method with annonymous object
}
}
Creating multiple objects by one type only
⚫W
e can create multiple objectsbyone type onlyas we do in
case of primitives.
⚫Rectangle r1=new Rectangle(),r2=new Rectangle();//cre
ating two objects
Method overloading
⚫Ifa class havemultiple methods bysame name but different
parameters, it is known asMethod Overloading.
⚫Ifwehaveto perform only one operation,havingsame name
of the methods increasesthe readability of the program.
⚫Advantage of method overloading?
⚫Method overloading increases the readability of the
program.
⚫Different ways to overload the method
1. There are two waysto overload the method in javaBy
changing number of arguments
2. Bychanging the data type
WhyMethodOverloadingisnot possible bychangingthe
return type of method?
⚫In java,method overloading is not possible bychanging the
return type of the method becausethere mayoccur ambiguity
.
Let's seehowambiguitymayoccur: becausethere was
problem:
⚫ class Calculation{
int sum(int a,int b){System.out.println(a+b);}
double sum(int a,int b){System.out.println(a+b);}
public static void main(String args[]){
Calculation obj=new Calculation();
int result=obj.sum(20,20);//CompileTime Error
}
}
Can we overload main() method?
⚫Yes, by method overloading.You can have any number of main
methods in aclass by method overloading. Let's see the simple
example:
class Simple{
public static void main(int a){
System.out.println(a);
}
public static void main(String args[]){
System.out.println("main() method invoked");
main(10);
}
}
Constructor
⚫ Constructor isaspecial type of method that isused to
initialize the object.
⚫ Constructor isinvoked at the time of object creation. It
constructs the values i.e. provides data for the object that is why it
is known asconstructor.
⚫ There are basicallytwo rules defined for the constructor.
⚫ Constructor name must be same asits classname
⚫ Constructor must haveno explicit return type
Constructor
⚫What is the purpose of default constructor?
⚫Default constructor provides the default values to the object
like 0, null etc. depending on the type.
⚫Aconstructor that haveparameters is known as
parameterized constructor.
⚫Why use parameterized constructor?
⚫Parameterized constructor is used to provide different values
to the distinct objects.
Constructor Overloading
class Student{
int id;
String name;
int age;
Student(int i,String n){
id = i;
name = n;
}
Student(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "
+age);}
public static void main(String args[]){
Student s1 = new Student(111,“T
arif");
Student s2 = new Student(222,“Soheab",25);
s1.display();
s2.display();
}
}
Constructor overloadingisatechnique in Javain whichaclasscanhaveanynumber of
constructors that differ in parameter lists.The compiler differentiatesthese constructors by
taking into account the number of parametersin the list and their type.
What isthe difference between constructor and method ?
Constructor Method
Constructor isused to initialize the state of
an object.
Method is used to expose behaviour
of an object.
Constructor must not have return type. Method must have return type.
Constructor is invokedimplicitly. Method is invoked explicitly.
The java compiler provides a
default constructor if you don’t
have any constructor.
Method is not provided by compiler in
any case.
Constructor name must be sameasthe class
name.
Method name may or may not be
same as class name.

Java PPT OOPS prepared by Abhinav J.pptx

  • 1.
  • 2.
    OOPs (Object OrientedProgramming System) ⚫Object meansa real word entity suchas pen, chair, table etc. Object-Oriented Programming is amethodology or paradigm to designaprogram usingclasses and objects. It simplifies the software development and maintenance by providing some concepts: ⚫Object ⚫Class ⚫Inheritance ⚫Polymorphism ⚫Abstraction ⚫Encapsulation
  • 3.
    Characteristics of OOP Object ⚫Anyentitythat has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical. Class ⚫Collection of objects is called class.It is alogical entity. Inheritance ⚫When one object acquires all the properties and behaviours of parent object i.e. known asinheritance. It provides code reusability.It is used to achieveruntime polymorphism.
  • 4.
    Characteristics of OOP Polymorphism ⚫Whenone task is performed by different ways i.e. knownas polymorphism. For example:to convense the customer differently,to draw something e.g.shapeor rectangle etc. ⚫In java,we use method overloading and method overriding to achieve polymorphism. Abstraction ⚫Hiding internal details and showing functionality is knownasabstraction. For example:phone call, we don't know the internal processing. ⚫In java,we use abstract class and interface to achieve abstraction.
  • 5.
    Characteristics of OOP Encapsulation ⚫Binding(or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines. ⚫Ajavaclass is the example of encapsulation.Javabean is the fully encapsulated class because all the data members are private here.
  • 6.
    What is differencebetween object-oriented programming language and object-based programminglanguage? ⚫Object based programming language follows all the features of OOPs except Inheritance. JavaScript andVBScript are examples of object based programming languages.
  • 7.
    Naming convention Name Convention classname Should begin with uppercase letter and be a noun e.g. String ,System,Thread etc Interface name Should begin with uppercase letter and be an adjective(wherever possible) e.g. Runnable(),ActionListener() etc method name Should begin with lowercase letter and be a verb. e.g. main(), print(), println(), actionListenerPerformed() V ariable name Should begin with lowercase letter e.g. firstName,orderNumber etc Package name Should be in lowercase letter e.g. java,lang,sql,util etc Constants name Should be in uppercase letter. E.g. RED,YELLOW ,MAX_PRIORITY etc
  • 8.
    Object • An entitythat has state and behavior is known as an object • An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class. • ForExample:Penisanobject.Its nameis Reynolds,color iswhite etc. knownasits state.It isusedto write,sowriting isits behaviour. • Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) ofa class.
  • 9.
    Class ⚫ Aclass isagroup of objects that have common property. It is a template or blueprint from which objects are created.Aclass in javacan contain: ⚫ data member ⚫ method ⚫ constructor ⚫ block ⚫ Avariable that is created inside the class but outside the method, is known as instance variable.Instance variable doesn't get memory at compile time.It gets memory at runtime when object(instance) is created.That is why,it is known as instance variable. ⚫ Method ⚫ In java,a method is like function i.e. used to expose behaviour of an object. ⚫ Advantage of Method ⚫ Code Reusability ⚫ Code Optimization
  • 10.
    Class • A classis a group of objects which have common properties. • A class in Java can contain: – Fields – Methods – Constructors – Blocks
  • 11.
    What are thedifferent waysto create an object in Java? ⚫There are many waysto create an object in java.They are: ⚫Bynew keyword ⚫BynewInstance() method ⚫Byclone() method ⚫Byfactorymethod etc. ⚫new keyword ⚫The new keyword is used to allocate memory at runtime.
  • 12.
    Annonymous object ⚫ Annonymoussimplymeans nameless.An object that have no reference is known asannonymous object.If you haveto use an object only once, annonymous object is agood approach. class Calculation{ void fact(int n){ int fact=1; for(int i=1;i<=n;i++){ fact=fact*i; } System.out.println("factorial is "+fact); } public static void main(String args[]){ new Calculation().fact(5);/ / calling method with annonymous object } }
  • 13.
    Creating multiple objectsby one type only ⚫W e can create multiple objectsbyone type onlyas we do in case of primitives. ⚫Rectangle r1=new Rectangle(),r2=new Rectangle();//cre ating two objects
  • 14.
    Method overloading ⚫Ifa classhavemultiple methods bysame name but different parameters, it is known asMethod Overloading. ⚫Ifwehaveto perform only one operation,havingsame name of the methods increasesthe readability of the program. ⚫Advantage of method overloading? ⚫Method overloading increases the readability of the program. ⚫Different ways to overload the method 1. There are two waysto overload the method in javaBy changing number of arguments 2. Bychanging the data type
  • 15.
    WhyMethodOverloadingisnot possible bychangingthe returntype of method? ⚫In java,method overloading is not possible bychanging the return type of the method becausethere mayoccur ambiguity . Let's seehowambiguitymayoccur: becausethere was problem: ⚫ class Calculation{ int sum(int a,int b){System.out.println(a+b);} double sum(int a,int b){System.out.println(a+b);} public static void main(String args[]){ Calculation obj=new Calculation(); int result=obj.sum(20,20);//CompileTime Error } }
  • 16.
    Can we overloadmain() method? ⚫Yes, by method overloading.You can have any number of main methods in aclass by method overloading. Let's see the simple example: class Simple{ public static void main(int a){ System.out.println(a); } public static void main(String args[]){ System.out.println("main() method invoked"); main(10); } }
  • 17.
    Constructor ⚫ Constructor isaspecialtype of method that isused to initialize the object. ⚫ Constructor isinvoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known asconstructor. ⚫ There are basicallytwo rules defined for the constructor. ⚫ Constructor name must be same asits classname ⚫ Constructor must haveno explicit return type
  • 18.
    Constructor ⚫What is thepurpose of default constructor? ⚫Default constructor provides the default values to the object like 0, null etc. depending on the type. ⚫Aconstructor that haveparameters is known as parameterized constructor. ⚫Why use parameterized constructor? ⚫Parameterized constructor is used to provide different values to the distinct objects.
  • 19.
    Constructor Overloading class Student{ intid; String name; int age; Student(int i,String n){ id = i; name = n; } Student(int i,String n,int a){ id = i; name = n; age=a; } void display(){System.out.println(id+" "+name+" " +age);} public static void main(String args[]){ Student s1 = new Student(111,“T arif"); Student s2 = new Student(222,“Soheab",25); s1.display(); s2.display(); } } Constructor overloadingisatechnique in Javain whichaclasscanhaveanynumber of constructors that differ in parameter lists.The compiler differentiatesthese constructors by taking into account the number of parametersin the list and their type.
  • 20.
    What isthe differencebetween constructor and method ? Constructor Method Constructor isused to initialize the state of an object. Method is used to expose behaviour of an object. Constructor must not have return type. Method must have return type. Constructor is invokedimplicitly. Method is invoked explicitly. The java compiler provides a default constructor if you don’t have any constructor. Method is not provided by compiler in any case. Constructor name must be sameasthe class name. Method name may or may not be same as class name.