SlideShare a Scribd company logo
1 of 28
 Keep instance variables protected (with an
access modifier, often private).
 Make public accessor methods, and force
calling code to use those methods rather
than directly accessing the instance
variable.
 For the methods, use the JavaBeans naming
convention of set and get.
Abstraction refers to the ability to make a class abstract in
OOP. An abstract class is one that cannot be instantiated. All
other functionality of the class still exists, and its fields,
methods, and constructors are all accessed in the same manner.
You just cannot create an instance of the abstract class.
public abstract class Shape{
public abstract void abstract display();
}
public class Rectangle extends Shape{
public void display(){
System.out.print(“Rectangle”);
}
}
public class Circle extends Shape{
public void display(){
System.out.print(“Circle”);
}
}
 Inheritance can be defined as the process
where one object acquires the properties of
another.
 most commonly used keyword would
be extends and implements.
public class Maruti{
private Engine engine;
}
public class Engine{}
 HAS-A relationships are based on usage,
rather than inheritance. In other words,
class A HAS-A B if code in class A has a
reference to an instance of class B.
 HAS-A relationships allow you to design
classes that follow good OO practices by
not having monolithic classes that do a
gazillion different things. Classes (and
their resulting
 objects) should be specialists.
Runtime
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){
System.out.println("running safely with 60
km");
}
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
}
}
 Polymorphism is the ability of an object to
take on many forms. The most common use
of polymorphism in OOP occurs when a parent
class reference is used to refer to a child
class object. Any Java object that can pass
more than one IS-A test is considered to be
polymorphic.
 Polymorphic method invocations apply only
to instance methods. You can always refer
to an object with a more general reference
variable type (a superclass or interface),
but at runtime, the ONLY things that are
dynamically selected based on the actual
object (rather than the reference type) are
instance methods. Not static methods. Not
variables. Only overridden instance methods
are dynamically invoked based on the real
object's type.
Overriding
 Any time you have a class that inherits a
method from a superclass, you have the
opportunity to override the method
 The key benefit of overriding is the
ability to define behavior that's specific
to a particular subclass type
 public class Animal { public void eat() {
System.out.println("Generic Animal Eating
Generically"); } } class Horse extends
Animal { public void eat() {
System.out.println("Horse eating hay, oats,
" + "and horse treats"); } }
 Overloaded methods let you reuse the same
method name in a class, but with different
arguments (and optionally, a different
return type).
public interface Animal{
void eat();
}
public class Dog implements Animal{
public void eat(){
System.out.println(“Dog eats Pedigree”);
}
}
public class Cat implements Animal{
public void eat(){
System.out.println(“Cat drinks milk”);
}
}
 public interface Animal{
void eat();
}
public interface Mammal extends Animal{
void eat();
}
You can return null in a
method with an object
reference return type.
public Button doStuff() {
return null; }
An array is a perfectly
legal return type. public
String[] go() { return new
String[] {"Fred", "Barney",
"Wilma"}; }
In a method with a
primitive return type,
you can return any
value or variable that
can be implicitly
converted to the
declared return type.
public int foo() { char c
= 'c'; return c; // char is
compatible with int }
In a method with a
primitive return type,
you can return any
value or variable that
can be explicitly cast to
the declared return
type. public int foo () {
float f = 32.5f; return (int)
f; }
You must not return
anything from a method
with a void return type.
public void bar() { return
"this is it"; // Not legal!! }
In a method with an
object reference return
type, you can return any
object type that can be
implicitly cast to the
declared return type.
public Animal
getAnimal() { return new
Horse(); // Assume Horse
extends Animal }
Constructors and
Instantiation
Every class in java has a
constructor including the
abstract class.
Default Constructor:
public class Rectangle{
private int length;
public void
Rectangle(){}
public void
Rectangle(int length){
this.length = length;
}
}
Integer i1 = new Integer(42); Integer i2 = new
Integer("42");
or
Float f1 = new Float(3.14f); Float f2 = new
Float("3.14f");
In summary, the essential method signatures for Wrapper
conversion methods are
primitive xxxValue() - to convert a Wrapper to a primitive
primitive parseXxx(String) - to convert a String to a
primitive Wrapper valueOf(String) - to convert a String to a
Wrapper
The heap is that part of memory where
Java objects live, and it's the one
and only part of memory that is in
any way involved in the garbage
collection process.
All of garbage collection revolves
around making sure that the heap has
as much free space as possible.
When the garbage collector runs, its
purpose is to find and delete objects
that cannot be reached.
A Java program is in a constant cycle
of creating the objects it needs
(which occupy space on the heap), and
then discarding them when they're no
longer needed, creating new objects,
discarding them, and so on, the
missing piece of the puzzle is the
garbage collector. When it runs, it
looks for those discarded objects and
deletes them from memory so that the
cycle of using memory and releasing
it can continue. Ah, the great circle
of life.
 The garbage collector is under the control of the JVM.
 The JVM decides when to run the garbage collector.
 From within your Java program you can ask the JVM to run the garbage collector, but
there are no guarantees, under any circumstances, that the JVM will comply.
 Left to its own devices, the JVM will typically run the garbage collector when it
senses that memory is running low.
 Experience indicates that when your Java program makes a request for garbage
collection, the JVM will usually grant your request in short order, but there are no
guarantees.
 Eden Space: When an instance is created, it is first stored in the
eden space in young generation of heap memory area.
 Survivor Space (S0 and S1): As part of the minor garbage collection
cycle, objects that are live (which is still referenced) are moved
to survivor space S0 from eden space. Similarly the garbage
collector scans S0 and moves the live instances to S1.
 Old Generation: Old or tenured generation is the second logical part
of the heap memory. When the garbage collector does the minor GC
cycle, instances that are still live in the S1 survivor space will
be promoted to the old generation. Objects that are dereferenced in
the S1 space is marked for eviction.
 Major GC: Old generation is the last phase in the instance life
cycle with respect to the Java garbage collection process. Major GC
is the garbage collection process that scans the old generation part
of the heap memory. If instances are dereferenced, then they are
marked for eviction and if not they just continue to stay in the old
generation.
 Memory Fragmentation: Once the instances are deleted from the heap
memory the location becomes empty and becomes available for future
allocation of live instances. These empty spaces will be fragmented
across the memory area. For quicker allocation of the instance it
should be defragmented. Based on the choice of the garbage
collector, the reclaimed memory area will either be compacted on the
go or will be done in a separate pass of the GC.
The Serial GC
The serial collector is the default
for client style machines in Java SE
5 and 6. With the serial collector,
both minor and major garbage
collections are done serially (using
a single virtual CPU). In addition,
it uses a mark-compact collection
method. This method moves older
memory to the beginning of the heap
so that new memory allocations are
made into a single continuous chunk
of memory at the end of the heap.
This compacting of memory makes it
faster to allocate new chunks of
memory to the heap.
Switch Description
-Xms
Sets the initial heap size
for when the JVM starts.
-Xmx
Sets the maximum heap
size.
-Xmn
Sets the size of the Young
Generation.
-XX:PermSize
Sets the starting size of the
Permanent Generation.
-XX:MaxPermSize
Sets the maximum size of
the Permanent
Generation
Switch Description
The simplest way to ask for garbage collection
(remember—just a request) is
System.gc();
or
Runtime.getRuntime().gc()
finalize method in java
For any given object, finalize() will be
called only once (at most) by the garbage
collector.
Calling finalize() can actually result in
saving an object from deletion.

More Related Content

What's hot

What's hot (20)

core java
core javacore java
core java
 
Byte code field report
Byte code field reportByte code field report
Byte code field report
 
Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Java
JavaJava
Java
 
Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template Pattern
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agents
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Java tips
Java tipsJava tips
Java tips
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Serialization Deep Dive
Java Serialization Deep DiveJava Serialization Deep Dive
Java Serialization Deep Dive
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 

Viewers also liked

Eng 400 presentation
Eng 400 presentationEng 400 presentation
Eng 400 presentation
clarkmc
 
Hepatitis acute with prolonged fever
Hepatitis acute with prolonged feverHepatitis acute with prolonged fever
Hepatitis acute with prolonged fever
Sanjeev Kumar
 
Saudi Oil Piece~Final
Saudi Oil Piece~FinalSaudi Oil Piece~Final
Saudi Oil Piece~Final
Jeremy Kraut
 
Chikungunyaoproximodesafio 141117151824-conversion-gate01
Chikungunyaoproximodesafio 141117151824-conversion-gate01Chikungunyaoproximodesafio 141117151824-conversion-gate01
Chikungunyaoproximodesafio 141117151824-conversion-gate01
Josilda Sena
 
Roteiro histórico introdução_sintra_as pedras e o tempo
Roteiro histórico introdução_sintra_as pedras e o tempoRoteiro histórico introdução_sintra_as pedras e o tempo
Roteiro histórico introdução_sintra_as pedras e o tempo
silvartes
 

Viewers also liked (20)

Eng 400 presentation
Eng 400 presentationEng 400 presentation
Eng 400 presentation
 
идея №1
идея №1идея №1
идея №1
 
Hepatitis acute with prolonged fever
Hepatitis acute with prolonged feverHepatitis acute with prolonged fever
Hepatitis acute with prolonged fever
 
Basant Resume1
Basant Resume1Basant Resume1
Basant Resume1
 
Interview on 10 minutes – MA Dragi Zmijanac, Founder&President of the First C...
Interview on 10 minutes – MA Dragi Zmijanac, Founder&President of the First C...Interview on 10 minutes – MA Dragi Zmijanac, Founder&President of the First C...
Interview on 10 minutes – MA Dragi Zmijanac, Founder&President of the First C...
 
www-LATimes-Teo
www-LATimes-Teowww-LATimes-Teo
www-LATimes-Teo
 
Trayectoria educativas
Trayectoria educativasTrayectoria educativas
Trayectoria educativas
 
Saudi Oil Piece~Final
Saudi Oil Piece~FinalSaudi Oil Piece~Final
Saudi Oil Piece~Final
 
Vibração de caminhão gera adicional de insalubridade para caminhoneiro
Vibração de caminhão gera adicional de insalubridade para caminhoneiroVibração de caminhão gera adicional de insalubridade para caminhoneiro
Vibração de caminhão gera adicional de insalubridade para caminhoneiro
 
Changes
ChangesChanges
Changes
 
Um vilão chamado passado
Um vilão chamado passadoUm vilão chamado passado
Um vilão chamado passado
 
Projeto regula transporte rodoviário de cargas
Projeto regula transporte rodoviário de cargasProjeto regula transporte rodoviário de cargas
Projeto regula transporte rodoviário de cargas
 
Chikungunyaoproximodesafio 141117151824-conversion-gate01
Chikungunyaoproximodesafio 141117151824-conversion-gate01Chikungunyaoproximodesafio 141117151824-conversion-gate01
Chikungunyaoproximodesafio 141117151824-conversion-gate01
 
Roteiro histórico introdução_sintra_as pedras e o tempo
Roteiro histórico introdução_sintra_as pedras e o tempoRoteiro histórico introdução_sintra_as pedras e o tempo
Roteiro histórico introdução_sintra_as pedras e o tempo
 
Mpl 1
Mpl 1Mpl 1
Mpl 1
 
"As good as Mother makes?": Food, Family and the Western Front by Rachel Duffett
"As good as Mother makes?": Food, Family and the Western Front by Rachel Duffett"As good as Mother makes?": Food, Family and the Western Front by Rachel Duffett
"As good as Mother makes?": Food, Family and the Western Front by Rachel Duffett
 
292 2015 cantieri stradali cpt lucca
292   2015   cantieri stradali cpt lucca292   2015   cantieri stradali cpt lucca
292 2015 cantieri stradali cpt lucca
 
IGLTA Convention 2012 - UNWTO keynote presentation: 'The Bigger Picture: Glob...
IGLTA Convention 2012 - UNWTO keynote presentation: 'The Bigger Picture: Glob...IGLTA Convention 2012 - UNWTO keynote presentation: 'The Bigger Picture: Glob...
IGLTA Convention 2012 - UNWTO keynote presentation: 'The Bigger Picture: Glob...
 
ColdFusion Features for More Modern Coding
ColdFusion Features for More Modern CodingColdFusion Features for More Modern Coding
ColdFusion Features for More Modern Coding
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 

Similar to Object orientation

1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
venud11
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 

Similar to Object orientation (20)

Java basics
Java basicsJava basics
Java basics
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Java mcq
Java mcqJava mcq
Java mcq
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performance
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
coding assignment help : Java question
coding assignment help : Java questioncoding assignment help : Java question
coding assignment help : Java question
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Oops
OopsOops
Oops
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
Java 06
Java 06Java 06
Java 06
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 

Object orientation

  • 1.
  • 2.
  • 3.  Keep instance variables protected (with an access modifier, often private).  Make public accessor methods, and force calling code to use those methods rather than directly accessing the instance variable.  For the methods, use the JavaBeans naming convention of set and get.
  • 4. Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class. public abstract class Shape{ public abstract void abstract display(); } public class Rectangle extends Shape{ public void display(){ System.out.print(“Rectangle”); } } public class Circle extends Shape{ public void display(){ System.out.print(“Circle”); } }
  • 5.  Inheritance can be defined as the process where one object acquires the properties of another.  most commonly used keyword would be extends and implements.
  • 6.
  • 7.
  • 8. public class Maruti{ private Engine engine; } public class Engine{}  HAS-A relationships are based on usage, rather than inheritance. In other words, class A HAS-A B if code in class A has a reference to an instance of class B.  HAS-A relationships allow you to design classes that follow good OO practices by not having monolithic classes that do a gazillion different things. Classes (and their resulting  objects) should be specialists.
  • 9. Runtime class Bike{ void run(){System.out.println("running");} } class Splender extends Bike{ void run(){ System.out.println("running safely with 60 km"); } public static void main(String args[]){ Bike b = new Splender();//upcasting b.run(); } }  Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.  Polymorphic method invocations apply only to instance methods. You can always refer to an object with a more general reference variable type (a superclass or interface), but at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. Not static methods. Not variables. Only overridden instance methods are dynamically invoked based on the real object's type.
  • 10. Overriding  Any time you have a class that inherits a method from a superclass, you have the opportunity to override the method  The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type  public class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); } } class Horse extends Animal { public void eat() { System.out.println("Horse eating hay, oats, " + "and horse treats"); } }
  • 11.
  • 12.
  • 13.  Overloaded methods let you reuse the same method name in a class, but with different arguments (and optionally, a different return type).
  • 14.
  • 15. public interface Animal{ void eat(); } public class Dog implements Animal{ public void eat(){ System.out.println(“Dog eats Pedigree”); } } public class Cat implements Animal{ public void eat(){ System.out.println(“Cat drinks milk”); } }  public interface Animal{ void eat(); } public interface Mammal extends Animal{ void eat(); }
  • 16. You can return null in a method with an object reference return type. public Button doStuff() { return null; } An array is a perfectly legal return type. public String[] go() { return new String[] {"Fred", "Barney", "Wilma"}; } In a method with a primitive return type, you can return any value or variable that can be implicitly converted to the declared return type. public int foo() { char c = 'c'; return c; // char is compatible with int } In a method with a primitive return type, you can return any value or variable that can be explicitly cast to the declared return type. public int foo () { float f = 32.5f; return (int) f; } You must not return anything from a method with a void return type. public void bar() { return "this is it"; // Not legal!! } In a method with an object reference return type, you can return any object type that can be implicitly cast to the declared return type. public Animal getAnimal() { return new Horse(); // Assume Horse extends Animal }
  • 17. Constructors and Instantiation Every class in java has a constructor including the abstract class. Default Constructor: public class Rectangle{ private int length; public void Rectangle(){} public void Rectangle(int length){ this.length = length; } }
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. Integer i1 = new Integer(42); Integer i2 = new Integer("42"); or Float f1 = new Float(3.14f); Float f2 = new Float("3.14f"); In summary, the essential method signatures for Wrapper conversion methods are primitive xxxValue() - to convert a Wrapper to a primitive primitive parseXxx(String) - to convert a String to a primitive Wrapper valueOf(String) - to convert a String to a Wrapper
  • 23. The heap is that part of memory where Java objects live, and it's the one and only part of memory that is in any way involved in the garbage collection process. All of garbage collection revolves around making sure that the heap has as much free space as possible. When the garbage collector runs, its purpose is to find and delete objects that cannot be reached. A Java program is in a constant cycle of creating the objects it needs (which occupy space on the heap), and then discarding them when they're no longer needed, creating new objects, discarding them, and so on, the missing piece of the puzzle is the garbage collector. When it runs, it looks for those discarded objects and deletes them from memory so that the cycle of using memory and releasing it can continue. Ah, the great circle of life.
  • 24.  The garbage collector is under the control of the JVM.  The JVM decides when to run the garbage collector.  From within your Java program you can ask the JVM to run the garbage collector, but there are no guarantees, under any circumstances, that the JVM will comply.  Left to its own devices, the JVM will typically run the garbage collector when it senses that memory is running low.  Experience indicates that when your Java program makes a request for garbage collection, the JVM will usually grant your request in short order, but there are no guarantees.
  • 25.  Eden Space: When an instance is created, it is first stored in the eden space in young generation of heap memory area.  Survivor Space (S0 and S1): As part of the minor garbage collection cycle, objects that are live (which is still referenced) are moved to survivor space S0 from eden space. Similarly the garbage collector scans S0 and moves the live instances to S1.  Old Generation: Old or tenured generation is the second logical part of the heap memory. When the garbage collector does the minor GC cycle, instances that are still live in the S1 survivor space will be promoted to the old generation. Objects that are dereferenced in the S1 space is marked for eviction.  Major GC: Old generation is the last phase in the instance life cycle with respect to the Java garbage collection process. Major GC is the garbage collection process that scans the old generation part of the heap memory. If instances are dereferenced, then they are marked for eviction and if not they just continue to stay in the old generation.  Memory Fragmentation: Once the instances are deleted from the heap memory the location becomes empty and becomes available for future allocation of live instances. These empty spaces will be fragmented across the memory area. For quicker allocation of the instance it should be defragmented. Based on the choice of the garbage collector, the reclaimed memory area will either be compacted on the go or will be done in a separate pass of the GC.
  • 26. The Serial GC The serial collector is the default for client style machines in Java SE 5 and 6. With the serial collector, both minor and major garbage collections are done serially (using a single virtual CPU). In addition, it uses a mark-compact collection method. This method moves older memory to the beginning of the heap so that new memory allocations are made into a single continuous chunk of memory at the end of the heap. This compacting of memory makes it faster to allocate new chunks of memory to the heap.
  • 27. Switch Description -Xms Sets the initial heap size for when the JVM starts. -Xmx Sets the maximum heap size. -Xmn Sets the size of the Young Generation. -XX:PermSize Sets the starting size of the Permanent Generation. -XX:MaxPermSize Sets the maximum size of the Permanent Generation Switch Description
  • 28. The simplest way to ask for garbage collection (remember—just a request) is System.gc(); or Runtime.getRuntime().gc() finalize method in java For any given object, finalize() will be called only once (at most) by the garbage collector. Calling finalize() can actually result in saving an object from deletion.