SlideShare a Scribd company logo
Java OOPs
Concepts
Java OOPs Concepts
Simula is considered as the first object-oriented programming
language.
Smalltalk is considered as the first truly object-oriented programming
language.
OOPs (Object Oriented
Programming System)
Object means a real word entity such as pen, chair, table etc. Object-
Oriented Programming is a methodology or paradigm to design a
program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
OOPs
Object
Any entity 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 a logical entity.
Inheritance
When one object acquires all the properties and behaviors of parent
object i.e. known as inheritance. It provides code reusability. It is used
to achieve runtime polymorphism.
OOPs
Polymorphism
When one task is performed by
different ways i.e. known as
polymorphism. For example: to
convense the customer differently, to
draw something e.g. shape or rectangle
etc.
In java, we use method overloading and
method overriding to achieve
polymorphism.
Another example can be to speak
something e.g. cat speaks meaw, dog
barks woof etc.
OOPs
Abstraction
Hiding internal details and showing
functionality is known as abstraction. For
example: phone call, we don't know the
internal processing.
In java, we use abstract class and interface
to achieve abstraction.
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.
A java class is the example of
encapsulation.
Advantage of OOPs over Procedure-
oriented programming language
1)OOPs makes development and maintenance easier where as
in Procedure-oriented programming language it is not easy to
manage if code grows as project size grows.
2)OOPs provides data hiding whereas in Procedure-oriented
prgramming language a global data can be accessed from
anywhere.
3)OOPs provides ability to simulate real-world event much
more effectively. We can provide the solution of real word
problem if we are using the Object-Oriented Programming
language.
What is difference between object-oriented
programming language and object-based
programming language?
Object based programming language follows all the features of OOPs
except Inheritance.
JavaScript and VBScript are examples of object based programming
languages.
Java Naming conventions
Java naming convention is a rule to follow as you decide what to name
your identifiers such as class, package, variable, constant, method etc.
Name Convention
class name should start with uppercase letter and be a noun e.g. String, Color, Button,
System, Thread etc.
interface name should start with uppercase letter and be an adjective e.g. Runnable,
Remote, ActionListener etc.
method name should start with lowercase letter and be a verb e.g. actionPerformed(),
main(), print(), println() etc.
variable name should start 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 in Java
An entity that has state and behavior is known as an object e.g. chair, bike,
marker, pen, table, car etc. It can be physical or logical (tengible and
intengible). The example of integible object is banking system.
An object has three characteristics:
state: represents data (value) of an object.
behavior: represents the behavior (functionality) of an object such as
deposit, withdraw etc.
identity: Object identity is typically implemented via a unique ID. The value
of the ID is not visible to the external user. But,it is used internally by the
JVM to identify each object uniquely.
For Example: Pen is an object. Its name is Reynolds, color is white etc.
known as its state. It is used to write, so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from which
objects are created. So object is the instance(result) of a class.
Class in Java
A class is a group of objects that has common properties. It is a
template or blueprint from which objects are created.
A class in java can contain:
data member
method
constructor
block
class and interface
Method in Java
In java, a method is like function i.e. used to expose behaviour of an
object.
Advantage of Method
Code Reusability
Code Optimization
new keyword
The new keyword is used to allocate memory at runtime.
What are the different
ways to create an object
in Java?
By new keyword
By newInstance() method
By clone() method
By factory method etc.
public static void main(String args[]){
new Calculation().fact(5);//calling method with annonymous object
}
Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objec
ts
Method Overloading in
Java
If a class have multiple methods by same name but different
parameters, it is known as Method Overloading.
Advantage of method overloading?
Method overloading increases the readability of the program.
Different ways to overload the method
By changing number of arguments
By changing the data type
1)Example of Method
Overloading by changing the
no. of arguments
class Calculation{
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
2)Example of Method Overloading
by changing data type of argument
class Calculation2{
void sum(int a,int b){System.out.println(a+b);}
void sum(double a,double b){System.out.println(a+b);}
public static void main(String args[]){
Calculation2 obj=new Calculation2();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}
class Calculation3{
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[]){
Calculation3 obj=new Calculation3();
int result=obj.sum(20,20);
}
}
Can we overload main()
method?
class Overloading1{
public static void main(int a){
System.out.println(a);
}
public static void main(String args[]){
System.out.println("main() method invoked");
main(10);
}
}
Method Overloading and
TypePromotion
One type is promoted to another implicitly if no matching datatype is
found.
Example of Method
Overloading with
TypePromotion
class OverloadingCalculation1{
void sum(int a,long b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
public static void main(String args[]){
OverloadingCalculation1 obj=new OverloadingCalculation1();
obj.sum(20,20);//now second int literal will be promoted to long
obj.sum(20,20,20);
}
}
Example of Method Overloading with
TypePromotion if matching found
class OverloadingCalculation2{
void sum(int a,int b){System.out.println("int arg method invoked");}
void sum(long a,long b)
{System.out.println("long arg method invoked");}
public static void main(String args[]){
OverloadingCalculation2 obj=new OverloadingCalculation2();
obj.sum(20,20);
}
}
Example of Method Overloading with
TypePromotion in case ambiguity
class OverloadingCalculation3{
void sum(int a,long b){System.out.println("a method invoked");}
void sum(long a,int b){System.out.println("b method invoked");}
public static void main(String args[]){
OverloadingCalculation3 obj=new OverloadingCalculation3();
obj.sum(20,20);//now ambiguity
}
}
Constructor in Java
Constructor in java is a special type of method that is used to initialize
the object.
Rules for creating java constructor
Constructor name must be same as its class name
Constructor must have no explicit return type
Types of java constructors
Default constructor (no-arg constructor)
Parameterized constructor
Example of default
constructor
class Bike1{
Bike1(){System.out.println("Bike is created");}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
If there is no constructor in a class, compiler automatically creates a
default 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.
Java parameterized
constructor
Why use parameterized constructor?
Parameterized constructor is used to provide different values to the
distinct objects
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
Constructor Overloading in Java
class Student5{
int id;
String name;
int age;
Student5(int i,String n){
id = i;
name = n; }
Student5(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[]){
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
Difference between
constructor and method in
java
Java Constructor Java Method
Constructor is used 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 invoked implicitly. 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 same
as the class name.
Method name may or may not be
same as class name.
Java Copy Constructor
There is no copy constructor in java. But, we can copy the values of one
object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in
java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
class Student6{
int id;
String name;
Student6(int i,String n){
id = i;
name = n;
}
Student6(Student s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student6 s1 = new Student6(111,"Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
Copying values without
constructor
public static void main(String args[]){
Student7 s1 = new Student7(111,"Karan");
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1.name;
s1.display();
s2.display();
}
}
Does constructor return
any value?
yes, that is current class instance (You cannot use return type yet it
returns a value).
Can constructor perform other tasks instead of initialization?
Yes, like object creation, starting a thread, calling method etc. You can
perform any operation in the constructor as you perform in the
method.
Java static keyword
The static keyword in java is used for memory management mainly. We
can apply java static keyword with variables, methods, blocks and
nested class.
The static can be:
variable (also known as class variable)
method (also known as class method)
block
nested class
Java static variable
If you declare any variable as static, it is known static variable.
The static variable can be used to refer the common property of all
objects (that is not unique for each object) e.g. company name of
employees, college name of students etc.
The static variable gets memory only once in class area at the time of
class loading.
Advantage of static variable
It makes your program memory efficient (i.e it saves memory).
class Student8{
int rollno;
String name;
static String college ="ITS";
Student8(int r,String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
Student8 s1 = new Student8(111,"Karan");
Student8 s2 = new Student8(222,"Aryan");
s1.display();
s2.display();
}
}
class Counter{
int count=0;//will get memory when instance is created
Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Java static method
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an
instance of a class.
static method can access static data member and can change the value
of it.
class Student9{
int rollno;
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student9(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
Student9.change();
Student9 s1 = new Student9 (111,"Karan");
Student9 s2 = new Student9 (222,"Aryan");
Student9 s3 = new Student9 (333,"Sonoo");
s1.display();
s2.display();
s3.display();
}
Ad

More Related Content

What's hot (20)

Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
Kurapati Vishwak
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
Spring Core
Spring CoreSpring Core
Spring Core
Pushan Bhattacharya
 
Java 10 New Features
Java 10 New FeaturesJava 10 New Features
Java 10 New Features
Ali BAKAN
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
Math-Circle
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Srinivas Reddy
 
Java Basics
Java BasicsJava Basics
Java Basics
Sunil OS
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
Akshay Mathur
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Edureka!
 
Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample application
Antoine Rey
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
Dallington Asingwire
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
Hitesh-Java
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
C# Basics
C# BasicsC# Basics
C# Basics
Sunil OS
 
Java features
Java featuresJava features
Java features
Prashant Gajendra
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
Kurapati Vishwak
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
Java 10 New Features
Java 10 New FeaturesJava 10 New Features
Java 10 New Features
Ali BAKAN
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
Math-Circle
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Java Basics
Java BasicsJava Basics
Java Basics
Sunil OS
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
Akshay Mathur
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Edureka!
 
Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample application
Antoine Rey
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
Hitesh-Java
 

Viewers also liked (20)

oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
atozknowledge .com
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
Jussi Pohjolainen
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
Raja Sekhar
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
OOPs in Java
OOPs in JavaOOPs in Java
OOPs in Java
Ranjith Sekar
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
javaease
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
wiradikusuma
 
Java programming course for beginners
Java programming course for beginnersJava programming course for beginners
Java programming course for beginners
Eduonix Learning Solutions
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
Ravi Kant Sahu
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
jaimefrozr
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
Vijay A Raj
 
Java basic
Java basicJava basic
Java basic
Sonam Sharma
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Veerabadra Badra
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
Intelligo Technologies
 
Java basic
Java basicJava basic
Java basic
Pooja Thakur
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
Mavoori Soshmitha
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
Jussi Pohjolainen
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
Raja Sekhar
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
javaease
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
wiradikusuma
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
Ravi Kant Sahu
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
jaimefrozr
 
Ad

Similar to Oop java (20)

javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
Nikhil Agrawal
 
Java PPT OOPS prepared by Abhinav J.pptx
Java PPT OOPS prepared by Abhinav J.pptxJava PPT OOPS prepared by Abhinav J.pptx
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
1_JavIntro
1_JavIntro1_JavIntro
1_JavIntro
SARJERAO Sarju
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit Notes
BalamuruganV28
 
11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf
Export Promotion Bureau
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
ilias ahmed
 
JAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, InheritanceJAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, Inheritance
Usha P
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Core_java_ppt.ppt
Core_java_ppt.pptCore_java_ppt.ppt
Core_java_ppt.ppt
SHIBDASDUTTA
 
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptxCore-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
NagarathnaRajur2
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
Epsiba1
 
JAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, InheritanceJAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, Inheritance
Usha P
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
Java training materials for computer engineering.pdf
Java training materials for computer engineering.pdfJava training materials for computer engineering.pdf
Java training materials for computer engineering.pdf
SkvKirupasri
 
Qb it1301
Qb   it1301Qb   it1301
Qb it1301
ArthyR3
 
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
 
C# Summer course - Lecture 1
C# Summer course - Lecture 1C# Summer course - Lecture 1
C# Summer course - Lecture 1
mohamedsamyali
 
Java notes jkuat it
Java notes jkuat itJava notes jkuat it
Java notes jkuat it
Arc Keepers Solutions
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
Arc Keepers Solutions
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
VasanthiMuniasamy2
 
Java PPT OOPS prepared by Abhinav J.pptx
Java PPT OOPS prepared by Abhinav J.pptxJava PPT OOPS prepared by Abhinav J.pptx
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit Notes
BalamuruganV28
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
ilias ahmed
 
JAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, InheritanceJAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, Inheritance
Usha P
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptxCore-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
NagarathnaRajur2
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
Epsiba1
 
JAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, InheritanceJAVA_INTRODUCTION- History, Constructor, Inheritance
JAVA_INTRODUCTION- History, Constructor, Inheritance
Usha P
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
Java training materials for computer engineering.pdf
Java training materials for computer engineering.pdfJava training materials for computer engineering.pdf
Java training materials for computer engineering.pdf
SkvKirupasri
 
Qb it1301
Qb   it1301Qb   it1301
Qb it1301
ArthyR3
 
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
 
C# Summer course - Lecture 1
C# Summer course - Lecture 1C# Summer course - Lecture 1
C# Summer course - Lecture 1
mohamedsamyali
 
Ad

More from Minal Maniar (15)

Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
Java ce241
Java ce241Java ce241
Java ce241
Minal Maniar
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
Minal Maniar
 
Java8 features
Java8 featuresJava8 features
Java8 features
Minal Maniar
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
Minal Maniar
 
Io
IoIo
Io
Minal Maniar
 
Class method object
Class method objectClass method object
Class method object
Minal Maniar
 
Object oriented thinking
Object oriented thinkingObject oriented thinking
Object oriented thinking
Minal Maniar
 
2 class use case
2 class use case2 class use case
2 class use case
Minal Maniar
 
1 modeling concepts
1 modeling concepts1 modeling concepts
1 modeling concepts
Minal Maniar
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
Minal Maniar
 
4 sdlc
4 sdlc4 sdlc
4 sdlc
Minal Maniar
 
3 interaction and_state_modeling
3 interaction and_state_modeling3 interaction and_state_modeling
3 interaction and_state_modeling
Minal Maniar
 
modeling concepts
modeling conceptsmodeling concepts
modeling concepts
Minal Maniar
 
modeling concepts
modeling conceptsmodeling concepts
modeling concepts
Minal Maniar
 

Recently uploaded (20)

Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 

Oop java

  • 2. Java OOPs Concepts Simula is considered as the first object-oriented programming language. Smalltalk is considered as the first truly object-oriented programming language.
  • 3. OOPs (Object Oriented Programming System) Object means a real word entity such as pen, chair, table etc. Object- Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object Class Inheritance Polymorphism Abstraction Encapsulation
  • 4. OOPs Object Any entity 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 a logical entity. Inheritance When one object acquires all the properties and behaviors of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
  • 5. OOPs Polymorphism When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc. In java, we use method overloading and method overriding to achieve polymorphism. Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.
  • 6. OOPs Abstraction Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing. In java, we use abstract class and interface to achieve abstraction. 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. A java class is the example of encapsulation.
  • 7. Advantage of OOPs over Procedure- oriented programming language 1)OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grows. 2)OOPs provides data hiding whereas in Procedure-oriented prgramming language a global data can be accessed from anywhere. 3)OOPs provides ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.
  • 8. What is difference between object-oriented programming language and object-based programming language? Object based programming language follows all the features of OOPs except Inheritance. JavaScript and VBScript are examples of object based programming languages.
  • 9. Java Naming conventions Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method etc. Name Convention class name should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc. interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc. method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc. variable name should start 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.
  • 10. Object in Java An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tengible and intengible). The example of integible object is banking system. An object has three characteristics: state: represents data (value) of an object. behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc. identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely. For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior. Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.
  • 11. Class in Java A class is a group of objects that has common properties. It is a template or blueprint from which objects are created. A class in java can contain: data member method constructor block class and interface
  • 12. Method in Java In java, a method is like function i.e. used to expose behaviour of an object. Advantage of Method Code Reusability Code Optimization new keyword The new keyword is used to allocate memory at runtime.
  • 13. What are the different ways to create an object in Java? By new keyword By newInstance() method By clone() method By factory method etc. public static void main(String args[]){ new Calculation().fact(5);//calling method with annonymous object } Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objec ts
  • 14. Method Overloading in Java If a class have multiple methods by same name but different parameters, it is known as Method Overloading. Advantage of method overloading? Method overloading increases the readability of the program. Different ways to overload the method By changing number of arguments By changing the data type
  • 15. 1)Example of Method Overloading by changing the no. of arguments class Calculation{ void sum(int a,int b){System.out.println(a+b);} void sum(int a,int b,int c){System.out.println(a+b+c);} public static void main(String args[]){ Calculation obj=new Calculation(); obj.sum(10,10,10); obj.sum(20,20); } }
  • 16. 2)Example of Method Overloading by changing data type of argument class Calculation2{ void sum(int a,int b){System.out.println(a+b);} void sum(double a,double b){System.out.println(a+b);} public static void main(String args[]){ Calculation2 obj=new Calculation2(); obj.sum(10.5,10.5); obj.sum(20,20); } }
  • 17. class Calculation3{ 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[]){ Calculation3 obj=new Calculation3(); int result=obj.sum(20,20); } }
  • 18. Can we overload main() method? class Overloading1{ public static void main(int a){ System.out.println(a); } public static void main(String args[]){ System.out.println("main() method invoked"); main(10); } }
  • 19. Method Overloading and TypePromotion One type is promoted to another implicitly if no matching datatype is found.
  • 20. Example of Method Overloading with TypePromotion class OverloadingCalculation1{ void sum(int a,long b){System.out.println(a+b);} void sum(int a,int b,int c){System.out.println(a+b+c);} public static void main(String args[]){ OverloadingCalculation1 obj=new OverloadingCalculation1(); obj.sum(20,20);//now second int literal will be promoted to long obj.sum(20,20,20); } }
  • 21. Example of Method Overloading with TypePromotion if matching found class OverloadingCalculation2{ void sum(int a,int b){System.out.println("int arg method invoked");} void sum(long a,long b) {System.out.println("long arg method invoked");} public static void main(String args[]){ OverloadingCalculation2 obj=new OverloadingCalculation2(); obj.sum(20,20); } }
  • 22. Example of Method Overloading with TypePromotion in case ambiguity class OverloadingCalculation3{ void sum(int a,long b){System.out.println("a method invoked");} void sum(long a,int b){System.out.println("b method invoked");} public static void main(String args[]){ OverloadingCalculation3 obj=new OverloadingCalculation3(); obj.sum(20,20);//now ambiguity } }
  • 23. Constructor in Java Constructor in java is a special type of method that is used to initialize the object. Rules for creating java constructor Constructor name must be same as its class name Constructor must have no explicit return type
  • 24. Types of java constructors Default constructor (no-arg constructor) Parameterized constructor
  • 25. Example of default constructor class Bike1{ Bike1(){System.out.println("Bike is created");} public static void main(String args[]){ Bike1 b=new Bike1(); } } If there is no constructor in a class, compiler automatically creates a default constructor.
  • 26. What is the purpose of default constructor? Default constructor provides the default values to the object like 0, null etc. depending on the type.
  • 27. Java parameterized constructor Why use parameterized constructor? Parameterized constructor is used to provide different values to the distinct objects
  • 28. class Student4{ int id; String name; Student4(int i,String n){ id = i; name = n; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student4 s1 = new Student4(111,"Karan"); Student4 s2 = new Student4(222,"Aryan"); s1.display(); s2.display(); } }
  • 29. Constructor Overloading in Java class Student5{ int id; String name; int age; Student5(int i,String n){ id = i; name = n; } Student5(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[]){ Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25); s1.display(); s2.display(); }
  • 30. Difference between constructor and method in java Java Constructor Java Method Constructor is used 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 invoked implicitly. 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 same as the class name. Method name may or may not be same as class name.
  • 31. Java Copy Constructor There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++. There are many ways to copy the values of one object into another in java. They are: By constructor By assigning the values of one object into another By clone() method of Object class
  • 32. class Student6{ int id; String name; Student6(int i,String n){ id = i; name = n; } Student6(Student s){ id = s.id; name =s.name; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student6 s1 = new Student6(111,"Karan"); Student6 s2 = new Student6(s1); s1.display(); s2.display(); } }
  • 33. Copying values without constructor public static void main(String args[]){ Student7 s1 = new Student7(111,"Karan"); Student7 s2 = new Student7(); s2.id=s1.id; s2.name=s1.name; s1.display(); s2.display(); } }
  • 34. Does constructor return any value? yes, that is current class instance (You cannot use return type yet it returns a value). Can constructor perform other tasks instead of initialization? Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor as you perform in the method.
  • 35. Java static keyword The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static can be: variable (also known as class variable) method (also known as class method) block nested class
  • 36. Java static variable If you declare any variable as static, it is known static variable. The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students etc. The static variable gets memory only once in class area at the time of class loading. Advantage of static variable It makes your program memory efficient (i.e it saves memory).
  • 37. class Student8{ int rollno; String name; static String college ="ITS"; Student8(int r,String n){ rollno = r; name = n; } void display (){System.out.println(rollno+" "+name+" "+college);} public static void main(String args[]){ Student8 s1 = new Student8(111,"Karan"); Student8 s2 = new Student8(222,"Aryan"); s1.display(); s2.display(); } }
  • 38. class Counter{ int count=0;//will get memory when instance is created Counter(){ count++; System.out.println(count); } public static void main(String args[]){ Counter c1=new Counter(); Counter c2=new Counter(); Counter c3=new Counter(); } }
  • 39. Java static method A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. static method can access static data member and can change the value of it.
  • 40. class Student9{ int rollno; String name; static String college = "ITS"; static void change(){ college = "BBDIT"; } Student9(int r, String n){ rollno = r; name = n; } void display (){System.out.println(rollno+" "+name+" "+college);} public static void main(String args[]){ Student9.change(); Student9 s1 = new Student9 (111,"Karan"); Student9 s2 = new Student9 (222,"Aryan"); Student9 s3 = new Student9 (333,"Sonoo"); s1.display(); s2.display(); s3.display(); }