SlideShare a Scribd company logo
1 of 29
By :-
Shivanshu Purwar
shivpurwar@gmail.com
Object Oriented Programming
 Object Oriented Programming (OOP) is a programming
method that based from object. Small think from OOP is
an object collection that interact with each other and
exchange an information
Class
 In the real world, you'll often find many individual objects all
of the same kind. There may be thousands of other bicycles in
existence, all of the same make and model. Each bicycle was
built from the same set of blueprints and therefore contains the
same components. In object-oriented terms, we say that your
bicycle is an instance of the class of objects known as bicycles.
A class is the blueprint from which individual objects are
created.
 Syntax : class Bicycles{
// field, constructor, and
// method declarations
}
Object
 Real-world objects share two characteristics: They all
have state and behavior .Bicycles have state (current gear,
current pedal cadence, current speed) and behavior
(changing gear, changing pedal cadence, applying brakes).
Identifying the state and behavior for real-world objects is
a great way to begin thinking in terms of object-oriented
programming
Reference
 A reference is an address that indicates where an object's
variables and methods are stored.
Object Life Cycle
Garbage Collector
 Garbage Collection is process of reclaiming the runtime
unused memory automatically. In other words, it is a way to
destroy the unused objects.
 It makes java memory efficient because garbage collector
removes the unreferenced objects from heap memory.
 It is automatically done by the garbage collector(a part of
JVM) so we don't need to make extra efforts.
How to create Object..?
Constructor
 A class contains constructors that are invoked to create objects
from the class blueprint. Constructor declarations look like
method declarations except that they use the name of the class
and have no return type. For example, Bicycle has one
constructor:
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
 To create a new Bicycle object called myBike, a constructor is
called by the new operator:
Bicycle myBike = new Bicycle(30, 0, 8);
 You don't have to provide any constructors for your class, but you
must be careful when doing this. The compiler automatically
provides a no-argument, default constructor for any class without
constructors. This default constructor will call the no-argument
constructor of the super Class. In this situation, the compiler will
complain if the super Class doesn't have a no-argument
constructor so you must verify that it does. If your class has no
explicit super Class, then it has an implicit super Class of Object,
which does have a no-argument constructor.
Initialize Block
 Initializer block contains the code that is always executed
whenever an instance is created. It is used to declare/initialize the
common part of various constructors of a class.
public class Prog12 {
{
System.out.println("Inside Initialization block");
}
public static void main(String[] args) {
Prog12 p = new Prog12(); //initialize block executed
Prog12 p1 = new Prog12(); //initialize block executed
}
}
This Keyword
 Within an instance method or a constructor, this is a reference
to the current object — the object whose method or
constructor is being called. You can refer to any member of
the current object from within an instance method or a
constructor by using this.
class Prog12 {
public int x = 0;
public int y = 0;
public Prog12(int x, int y) {
x = x; // 0
this.y = y; // 2
}
Prog12 p = new Prog12(1,2);
Access Modifier
Nested Class
 The Java programming language allows you to define a class
within another class. Such a class is called a nested class and is
illustrated here:
class OuterClass {
...
class NestedClass {
...
}
}
 A nested class is a member of its enclosing class. Non-static
nested classes (inner classes) have access to other members of the
enclosing class, even if they are declared private. Static nested
classes do not have access to other members of the enclosing
class. As a member of the OuterClass, a nested class can be
declared private, public, protected, or package private. (Recall
that outer classes can only be declared public or package private.)
Inner Class
 As with instance methods and variables, an inner class is associated with
an instance of its enclosing class and has direct access to that object's
methods and fields. Also, because an inner class is associated with an
instance, it cannot define any static members itself.
 Objects that are instances of an inner class exist within an instance of the
outer class. Consider the following classes:
 class OuterClass {
...
class InnerClass {
...
}
}
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Anonymous Class
 Anonymous classes enable you to make your code more concise.
They enable you to declare and instantiate a class at the same
time. They are like local classes except that they do not have a
name. Use them if you need to use a local class only once.
Syntax :-
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
greetSomeone("tout le monde");
} public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
Abstract Class
 An abstract class is a class that is declared abstract—it may or
may not include abstract methods. Abstract classes cannot be
instantiated, but they can be subclasses.
 If a class includes abstract methods, then the class itself must be
declared abstract, as in:
public abstract class GraphicObject {
// declare fields
// declare non abstract methods
abstract void draw(); }
 When an abstract class is subclassed, the subclass usually provides
implementations for all of the abstract methods in its parent class.
However, if it does not, then the subclass must also be
declared abstract.
Difference b/w Abstract Class and Interface
 Abstract classes are similar to interfaces. You cannot
instantiate them, and they may contain a mix of methods
declared with or without an implementation. However, with
abstract classes, you can declare fields that are not static and
final, and define public, protected, and private concrete
methods. With interfaces, all fields are automatically public,
static, and final, and all methods that you declare or define
(as default methods) are public. In addition, you can extend
only one class, whether or not it is abstract, whereas you can
implement any number of interfaces.
Argument Passing
 You can use any data type for a parameter of a method or a
constructor. This includes primitive data types, such as doubles,
floats, and integers and reference data types, such as objects
and arrays.
 Primitive arguments, such as an int or a double, are passed into
methods by value. This means that any changes to the values of
the parameters exist only within the scope of the method. When
the method returns, the parameters are gone and any changes to
them are lost.
 Reference data type parameters, such as objects, are also
passed into methods by value. This means that when the
method returns, the passed-in reference still references the
same object as before. However, the values of the object's
fields can be changed in the method, if they have the proper
access level.
Method Overloading
 If a class has multiple methods having same name but
different in parameters, it is known as Method Overloading.
 There are two ways to overload the method in java
By changing number of arguments
By changing the data type
Static method
 Variables and methods marked static belong to the class rather than
to any particular instance of the class. These can be used without
having any instances of that class at all. Only the class is sufficient
to invoke a static method or access a static variable. A static
variable is shared by all the instances of that class. Only one copy
of the static variable is created.
 A static method cannot access non-static/instance variables,
because a static method is never associated with any instance. The
same applies with the non-static methods as well, a static method
can’t directly invoke a non-static method. But static method can
access non-static methods by means of declaring instances and
using them.
 static methods can’t be overridden. They can be redefined in a
subclass, but redefining and overriding aren’t the same thing. Its
called as Hiding.
Finalize()
 Called by the garbage collector on an object when garbage
collection determines that there are no more references to the
object. A subclass overrides the finalize method to dispose of
system resources or to perform other cleanup.
 The Java programming language does not guarantee which
thread will invoke the finalize method for any given object.
 The finalize method is never invoked more than once by a
Java virtual machine for any given object.
protected void finalize() throws Throwable
Native
 While you can write applications entirely in Java, there are situations
where Java alone does not meet the needs of your application.
Programmers use the JNI to write Java native methods to handle those
situations when an application cannot be written entirely in Java.
 The following examples illustrate when you need to use Java native
methods:
 standard Java class library does not support the platform-dependent
features needed by the application.
 You already have a library written in another language, and wish
to make it accessible to Java code through the JNI.
 You want to implement a small portion of time-critical code in a
lower-level language such as assembly.
Generics
 Java Generic methods and generic classes enable
programmers to specify, with a single method declaration, a
set of related methods, or with a single class declaration, a set
of related types, respectively.
 Generics also provide compile-time type safety that allows
programmers to catch invalid types at compile time.
Generic Method
public class Prog12 {
public static < E > void printArray( E[] inputArray ) {
for(E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
public static void main(String args[]) {
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
System.out.println("Array integerArray contains:");
printArray(intArray);
System.out.println("nArray doubleArray contains:");
printArray(doubleArray);
System.out.println("nArray characterArray contains:");
printArray(charArray);
}
}
Generic Class
 public class Box<T> {
private T t;
public void add(T t) {
this.t = t; }
public T get() {
return t; }
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
Box<String> stringBox = new Box<String>();
integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));
System.out.printf("Integer Value :%dnn", integerBox.get());
System.out.printf("String Value :%sn", stringBox.get());
}
}
Shallow Vs Deep Cloning
 The process of creating just duplicate reference variable but
not duplicate object is called shallow cloning. Whereas the
process of creating exactly independent duplicate object is
called deep cloning.
Shallow Cloning
Test t1 = new Test();
Test t2 = t1;
Deep Cloning
Test t1 = new Test();
Test t2 = (Test)t1.clone();
Java basics

More Related Content

What's hot

Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Uzair Salman
 
Java assignment help
Java assignment helpJava assignment help
Java assignment helpJacob William
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentalsjavaease
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examplesbindur87
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 

What's hot (20)

Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
 
Java Notes
Java NotesJava Notes
Java Notes
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Oops in java
Oops in javaOops in java
Oops in java
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Introduction to OOP(in java) BY Govind Singh
Introduction to OOP(in java)  BY Govind SinghIntroduction to OOP(in java)  BY Govind Singh
Introduction to OOP(in java) BY Govind Singh
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 

Similar to Java basics

Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core ParcticalGaurav Mehta
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptxSajidTk2
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and oodthan sare
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2Usman Mehmood
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
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
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 

Similar to Java basics (20)

Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Classes2
Classes2Classes2
Classes2
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Java mcq
Java mcqJava mcq
Java mcq
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
Java 06
Java 06Java 06
Java 06
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Only oop
Only oopOnly oop
Only oop
 
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...
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 

Recently uploaded

Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesSHIVANANDaRV
 

Recently uploaded (20)

Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 

Java basics

  • 2. Object Oriented Programming  Object Oriented Programming (OOP) is a programming method that based from object. Small think from OOP is an object collection that interact with each other and exchange an information
  • 3. Class  In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.  Syntax : class Bicycles{ // field, constructor, and // method declarations }
  • 4. Object  Real-world objects share two characteristics: They all have state and behavior .Bicycles have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming
  • 5. Reference  A reference is an address that indicates where an object's variables and methods are stored.
  • 7. Garbage Collector  Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.  It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.  It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
  • 8. How to create Object..?
  • 9. Constructor  A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations except that they use the name of the class and have no return type. For example, Bicycle has one constructor: public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; }  To create a new Bicycle object called myBike, a constructor is called by the new operator: Bicycle myBike = new Bicycle(30, 0, 8);
  • 10.  You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the super Class. In this situation, the compiler will complain if the super Class doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit super Class, then it has an implicit super Class of Object, which does have a no-argument constructor.
  • 11. Initialize Block  Initializer block contains the code that is always executed whenever an instance is created. It is used to declare/initialize the common part of various constructors of a class. public class Prog12 { { System.out.println("Inside Initialization block"); } public static void main(String[] args) { Prog12 p = new Prog12(); //initialize block executed Prog12 p1 = new Prog12(); //initialize block executed } }
  • 12. This Keyword  Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. class Prog12 { public int x = 0; public int y = 0; public Prog12(int x, int y) { x = x; // 0 this.y = y; // 2 } Prog12 p = new Prog12(1,2);
  • 14. Nested Class  The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here: class OuterClass { ... class NestedClass { ... } }  A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)
  • 15. Inner Class  As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.  Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:  class OuterClass { ... class InnerClass { ... } } OuterClass.InnerClass innerObject = outerObject.new InnerClass();
  • 16. Anonymous Class  Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once. Syntax :- HelloWorld frenchGreeting = new HelloWorld() { String name = "tout le monde"; public void greet() { greetSomeone("tout le monde"); } public void greetSomeone(String someone) { name = someone; System.out.println("Salut " + name); } };
  • 17. Abstract Class  An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclasses.  If a class includes abstract methods, then the class itself must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare non abstract methods abstract void draw(); }  When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
  • 18. Difference b/w Abstract Class and Interface  Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
  • 19. Argument Passing  You can use any data type for a parameter of a method or a constructor. This includes primitive data types, such as doubles, floats, and integers and reference data types, such as objects and arrays.  Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.  Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.
  • 20. Method Overloading  If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.  There are two ways to overload the method in java By changing number of arguments By changing the data type
  • 21. Static method  Variables and methods marked static belong to the class rather than to any particular instance of the class. These can be used without having any instances of that class at all. Only the class is sufficient to invoke a static method or access a static variable. A static variable is shared by all the instances of that class. Only one copy of the static variable is created.  A static method cannot access non-static/instance variables, because a static method is never associated with any instance. The same applies with the non-static methods as well, a static method can’t directly invoke a non-static method. But static method can access non-static methods by means of declaring instances and using them.  static methods can’t be overridden. They can be redefined in a subclass, but redefining and overriding aren’t the same thing. Its called as Hiding.
  • 22.
  • 23. Finalize()  Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.  The Java programming language does not guarantee which thread will invoke the finalize method for any given object.  The finalize method is never invoked more than once by a Java virtual machine for any given object. protected void finalize() throws Throwable
  • 24. Native  While you can write applications entirely in Java, there are situations where Java alone does not meet the needs of your application. Programmers use the JNI to write Java native methods to handle those situations when an application cannot be written entirely in Java.  The following examples illustrate when you need to use Java native methods:  standard Java class library does not support the platform-dependent features needed by the application.  You already have a library written in another language, and wish to make it accessible to Java code through the JNI.  You want to implement a small portion of time-critical code in a lower-level language such as assembly.
  • 25. Generics  Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively.  Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.
  • 26. Generic Method public class Prog12 { public static < E > void printArray( E[] inputArray ) { for(E element : inputArray) { System.out.printf("%s ", element); } System.out.println(); } public static void main(String args[]) { Integer[] intArray = { 1, 2, 3, 4, 5 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 }; Character[] charArray = { 'H', 'E', 'L', 'L', 'O' }; System.out.println("Array integerArray contains:"); printArray(intArray); System.out.println("nArray doubleArray contains:"); printArray(doubleArray); System.out.println("nArray characterArray contains:"); printArray(charArray); } }
  • 27. Generic Class  public class Box<T> { private T t; public void add(T t) { this.t = t; } public T get() { return t; } public static void main(String[] args) { Box<Integer> integerBox = new Box<Integer>(); Box<String> stringBox = new Box<String>(); integerBox.add(new Integer(10)); stringBox.add(new String("Hello World")); System.out.printf("Integer Value :%dnn", integerBox.get()); System.out.printf("String Value :%sn", stringBox.get()); } }
  • 28. Shallow Vs Deep Cloning  The process of creating just duplicate reference variable but not duplicate object is called shallow cloning. Whereas the process of creating exactly independent duplicate object is called deep cloning. Shallow Cloning Test t1 = new Test(); Test t2 = t1; Deep Cloning Test t1 = new Test(); Test t2 = (Test)t1.clone();