Java Training Sauron
Java Training Sauron
OOP Concepts
(Object Oriented Programming)
● Encapsulation
● Abstraction
● Inheritance
● Polymorphism
Abstraction
− Abstraction is a process of identifying the relevant qualities and behaviors an
object should possess.
Static Binding –
• Also known as “Early Binding” or Overloading
• Resolved at compile time.
• Resolution based on static type of the object(s).
Dynamic Binding –
• Also known as “Late Binding” or Overriding
• Resolved at run-time.
• Resolution based on the dynamic type of the object(s).
Cntd…
class Calculation{
int z;
public void addition(int x, int y){
z = x+y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x,int y){
z = x-y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation{
public void multiplication(int x, int y){
z = x*y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]){
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
Overloading
• It can be done in two ways
– Based on scope
Class SomeCards {
Draw() {…} // Paint the face of the card
}
Class SomeGame {
Draw() {…} // Remove a card from the deck of cards
}
– Based on type signatures
Class Example {
Add(int a) { return a; }
Add(int a, int b) { return a + b; }
Add(int a, int b, int c) { return a + b + c; }
}
• Methods cannot be overloaded based on differences in their return
types alone.
Overriding
• A method in child class overrides a method in parent class if they
have the same name and type signature.
• classes in which methods are defined must be in a parent-child
relationship.
• Type signatures must match.
• The benefit of overriding is: ability to define a behaviour that's
specific to the subclass type which means a subclass can implement
a parent class method based on its requirement.
• When invoking a superclass version of an overridden method
the super keyword is used.
• The access level cannot be more restrictive than the overridden
method's access level. For example: if the superclass method is
declared public then the overridding method in the sub class cannot
be either private or protected.
…
class Calculation{
int z;
public void addition(int x, int y){
z = x+y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x,int y){
z = x-y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation{
public void multiplication(int x, int y){
z = x*y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]){
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
Final keyword
• What is final
– The final keyword is used in several different contexts to define an
entity that can only be assigned once.
}
return getAccountStatusCodeFromResultSet(resultSet); AFTER
}
Suppressed Exception
Try It yourself
● https://programming.guide/java/try-finally.html
● https://howtodoinjava.com/java7/java-suppressed-exceptions/
Threads
Threads
Threads (States)
Threads...contd
● How to implement a thread :
− Runnable Interface
− Callable Interface
− Thread class
● Methods in Thread :
− sleep
− wait
− join
− yield
● synchronized keyword
Threads...contd
● Futures
Threads...contd
● ExecutorServices
○ newFixedThreadPool(int nThreads)
○ newScheduledThreadPool(int corePoolSize)
○ newCachedThreadPool()
● Methods
○ submit(Callable<T> task)
○ submit(Runnable task)
○ invokeAll(Collection<? extends Callable<T>> tasks)
Threads...contd
● System.gc()
● finalize()
Garbage Collection...contd
● Step 1: Marking
● The first step in the process is called marking. This is where the
garbage collector identifies which pieces of memory are in use and
which are not.
Garbage Collection...contd
● Step 2: Normal Deletion
● Normal deletion removes unreferenced objects leaving referenced
objects and pointers to free space.
Garbage Collection...contd
● Step 2a: Deletion with Compacting
● By moving referenced object together, this makes new memory
allocation much easier and faster.
Object-Life
Garbage Collection...contd
● Young Generation
● Old Generation
● Permanent Generation
● Minor GC
● Major GC
● Stop the World Event
GC Common Params
doWork(); }
} }).start();
new Thread(w).start();
Functional Interfaces
● You can supply a lambda expression whenever an object @FunctionalInterface
of an interface with a single abstract method is
expected. Such an interface is called a functional public interface Runnable {
interface.
● Lambda should confirm to public abstract void run();
○ Input arguments type
○ Output arguments type }
○ Checked exceptions
● Functional interface can have other functions but only one @FunctionalInterface
function should be abstract. Checkout java Function in
java.util.function public interface Function<T, R> {
● @FunctionalInterface
○ It is optional R apply(T t);
○ If it is specified, compiler checks for
interface being functional. }
○ It is also added in Java Documentation.
Runnable sleeper = () -> {
System.out.println("Zzz");}
T get();
● Method References
List<Runner> runners = new ArrayList<>();
runners.forEach(System.out::println);
● Constructor References
Button[] buttons = stream.toArray(Button[]::new)
}
With streams
Map Reduce
● Perform a function on individual values ● Combine values in a data set to create a
in a data set to create a new list of values single new value
● Example: square x => x * x ● Example: sum => (each elem in arr, total +=)
map square [1,2,3,4,5] reduce [1,2,3,4,5]
returns [1,4,9,16,25] returns 15 (the sum of the elements)
There is more
Filter
● Perform a predicate test on individual values in a data set and create new list of from those who pass the test
● Example: even x = x % 2 == 0
filter even [1,2,3,4,5]
returns [2,4]
Flat map
● Perform an operation on individual values in a data set, where each operation results multiple values, and flattening
the result.
● Example: split x = spliting x by spaces
map split ["two birds", "three green peas"] => [[“two birds”],[“three green peas”]]
flatmap split ["two birds", "three green peas"] => [“two”, “birds”, “three”, “green”, “peas”]
Let’s get into details
● Creating streams ●
○ IntStream.of(1, 2, 3, 4)
○ IntStream.range(1, 4)
○ DoubleStream.of(2.0, 3.0)
○ Stream.of("A", "B");
○ Arrays.stream(new int[] {1, 2, 3, 4})
○ new ArrayList<>().stream();
○ Stream.empty()
○ Many more ways.
Collect stream
List<String> marvelKids = Arrays.asList("Black Widow", "Thor", "Captain America", "Iron Man");
.collect(Collectors.toList());
Stream filter
List<String> marvelKids = Arrays.asList("Black Widow", "Thor", "Batman", "Iron Man");
List<String> onlyMarvel = marvelKids.stream()
.filter(name -> !name.equals("Batman"))
.collect(Collectors.toList());
Stream reduce
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
.collect(Collectors.toList());
References
∙
● http://javarevisited.blogspot.in/2014/07/top-50-java-multithr
eading-interview-questions-answers.html
● http://chiragkanzariya.blogspot.in/2012/11/oops-concept-wi
th-real-world-example.html#sthash.aeApu0VT.dpuf
● http://tutorials.jenkov.com/java-concurrency/volatile.html
● https://dzone.com/articles/java-volatile-keyword-0
● http://javarevisited.blogspot.com/2011/04/synchronization-i
n-java-synchronized.html#ixzz4EwzOJ4LL
● http://tutorials.jenkov.com/java-concurrency/synchronized.
html