0% found this document useful (0 votes)
6 views

Java Training Sauron

The document provides an introduction to Object-Oriented Programming (OOP) concepts in Java, including encapsulation, abstraction, inheritance, and polymorphism. It explains key features such as access modifiers, constructors, method overloading and overriding, and the use of the final keyword. Additionally, it covers advanced topics like exception handling, threading, garbage collection, and functional programming with lambda expressions and functional interfaces.

Uploaded by

jwyxhwzbqz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Training Sauron

The document provides an introduction to Object-Oriented Programming (OOP) concepts in Java, including encapsulation, abstraction, inheritance, and polymorphism. It explains key features such as access modifiers, constructors, method overloading and overriding, and the use of the final keyword. Additionally, it covers advanced topics like exception handling, threading, garbage collection, and functional programming with lambda expressions and functional interfaces.

Uploaded by

jwyxhwzbqz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Introduction to Java

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.

− Objects in an OOP language provide an abstraction that hides the internal


implementation details. Similar to the coffee machine in your kitchen, you just
need to know which methods of the object are available to call and which input
parameters are needed to trigger a specific operation. But you don’t need to
understand how this method is implemented and which kinds of actions it has to
perform to create the expected result.
Abstraction Cntd…
Encapsulation
− Encapsulation is a method for grouping/hiding data to
hide implementation details.
− For example, the Laptop is an object that encapsulates
many technologies/hardwares that might not be
understood clearly by most people who use it.
− Class : A template/blue print that defines the properties
and behaviors that object of its type support.
Encapsulation cntd...
public class Computer {
private Memory ram;
private Processor processor;
public void start() {
}
public void shutdown() {
}
}

− Object : Objects exhibit the properties and behaviors


defined by its class. Instance of class.
Computer myPC = new Computer();
Access modifiers
• Four access modifiers
– public
– protected
– default
– private
• Access modifiers helps to implement the concept of encapsulation in OOPs.
• Encapsulation helps to hide the data and behavior.
• Access modifiers affect the accessibility at two levels


examples
Inheritance
Inheritance Cntd…
• Inheritance is the process wherein characteristics are inherited from
ancestors.
• In Java, classes may inherit or acquire the properties and methods of
other classes.
• A class derived from another class is called a subclass, whereas the
class from which a subclass is derived is called a superclass. A
subclass can have only one superclass, whereas a superclass may
have one or more subclasses.
• There are three types of inheritance supported in Java
– Single Inheritance
– Multilevel Inheritance
– Hierarchical Inheritance
– Multiple and Hybrid Inheritance is not supported in java
Inheritance Cntd…
• Diamond Problem ?? Not a problem in java bcz

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);
}
}
Constructors
● Default Constructor
● Parameterized Constructor
● super() - always the first statement in a constructor.
● All classes extend java.lang.Object implicitly in java, if not
inherited specifically.
● public class Computer {
Computer() {
System.out.println(“Instantiating computer”);
}
}
Constructors Cntd.
public class Laptop extends Computer {
Laptop() {
//super(); implicit
System.out.println(“Instantiating Laptop”);
}
Laptop(String username) {
//super(); implicit
System.out.println(“Instantiating Laptop with username ” +
username);
}
}
Polymorphism is the capability of a method to do different things based on
the object that it is acting upon

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.

• Variations of Final keyword


– A final class cannot be inherited.
– A final method cannot be overridden.
– A final variable.
Method call semantics(pass by
value, pass by reference)
• Call by Value
– While Passing Parameters using call by value , xerox copy of original
parameter is created and passed to the called function.
– Any update made inside method will not affect the original value of variable
in calling function.
– As their scope is limited to only function so they cannot alter the values
inside calling function.
• Call by Reference
– While passing parameter using call by address scheme , we are passing the
actual address of the variable to the called function.
– Any updates made inside the called function will modify the original
copy since we are directly modifying the content of the exact memory location.
So what do you think how java works,
pass-by-value or pass-by-reference??
Pass-by-value
• Let’s take one example and understand
public class Main{
public static void main(String[] args){
Foo f = new Foo("f");
changeReference(f); // It won't change the reference!
modifyReference(f); // It will modify the object that the reference variable "f" refers to!
}
public static void changeReference(Foo a){
Foo b = new Foo("b");
a = b;
}
public static void modifyReference(Foo c){
c.setAttribute("c");
}
}
Static keyword
• What is static
– The static keyword is used when a member variable of a class has to be
shared between all the instances of the class.
– All static variables and methods belong to the class and not to any
instance of the class
• When can we access static variable
– When a class is loaded by the virtual machine all the static variables and
methods are available for use.
– Hence we don’t need to create any instance of the class for using the
static variables or methods.
– Variables which don’t have static keyword in the definition are implicitly
non static.
• The static can be variable, method, block, nested class
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);
}
}
String
● Immutable
● String literal: JVM checks the string constant pool first. If the string
already exists in the pool, a reference to the pooled instance is
returned.
− String s1="Welcome";
● Using New Keyword :
− String str1 = new String("Welcome");
● StringBuffer
● StringBuilder
Source : Wiki
Source : Wiki
Exceptions
Try - Catch - Finally
Throwing Exceptions
➔ You can throw exceptions from your own methods
➔ To throw an exception, create an instance of the exception
class and "throw" it.
➔ If you throw checked exceptions, you must indicate which
exceptions your method throws by using the throws
keyword
public static int getAccountStatusCodeFromDataStore_traditional(String accountId) throws SQLException {
String accountStatusCodeQuery = getAccountStatusCodeQuery(accountId);
Statement statement = null;
ResultSet resultSet = null;
try {
statement = createStatementFromConnection();
resultSet = statement.executeQuery(accountStatusCodeQuery);
return getAccountStatusCodeFromResultSet(resultSet);
} finally { BEFORE
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
}
}

public static int getAccountStatusCodeFromDataStore(String accountId) throws SQLException {


String accountStatusCodeQuery = getAccountStatusCodeQuery(accountId);
try (Statement statement = createStatementFromConnection();
ResultSet resultSet = statement.executeQuery(accountStatusCodeQuery)) {

}
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

● volatile keyword : In concurrent Java


programs, changes made by multiple threads
on instance variables is not visible to other in
absence of any synchronizers e.g. synchronized
keyword or locks. Volatile variable guarantees
that a write will happen before any subsequent
read
Garbage Collection
Garbage Collection...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

● -Xms<size> ->to set initial heap size


● -Xmx<size> -> to set max heap size
● -Xmn<size> -> to set size of the young generation
● -XX:PermGen<size> -> to set the starting size of permgen
Java Streams and
Functional Interfaces
Code like a boss, declare it. Don’t do it.
Lambda Expressions λ

● A “lambda expression” is a block of code that you can pass around so


it can be executed later, once or multiple time.

class Worker implements Runnable { new Thread(() -> {

public void run() { for(int i = 0;i < 1000;++i) {

for (int i = 0; i < 1000; i++) doWork();

doWork(); }

} }).start();

Worker w = new Worker();

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");}

Function<Integer, String> asString = x ->


String.valueOf(x);
Supplier Interface (java.util.function)
@FunctionalInterface
● Expects in nothing
● Supplies a value of type T public interface Supplier<T> {

T get();

Supplier<String> stringSupplier = () -> "hello";

Supplier<Set<String>> setSupplier = () -> new


HashSet<>();
Consumer Interface (java.util.function)
@FunctionalInterface
● Expects in nothing
● Consumes a value of type T public interface Consumer<T> {

void accept(T t);

List<String> stringList = new ArrayList<>();

Consumer<String> stringConsumer = x ->


stringList.add(x);
Predicate Interface (java.util.function)
● Represents a predicate (boolean-valued @FunctionalInterface

function) of one argument


public interface Predicate<T> {

boolean test(T t);

Predicate<Integer> evenPredicate = x -> x % 2 == 0;


There is more

● Check them out.


Curious - Read for this

● Method References
List<Runner> runners = new ArrayList<>();
runners.forEach(System.out::println);

● Constructor References
Button[] buttons = stream.toArray(Button[]::new)

● Variable Scope and Closure


String[] names = { "Peter", "Paul", "Mary" };
List<Runnable> runners = new ArrayList<>();
for (String name : names)
runners.add(() -> System.out.println(name));
Life without streams

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

for(int i = 0;i < numbers.size();++i) {

numbers.set(i, numbers.get(i) * 2);

}
With streams

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

numbers = numbers.stream().map(x -> x * 2).collect(Collectors.toList());


Why streams API ?

1. Mapreduce transformations on collections


2. Functional-style operations
3. Declarative Syntax
4. Streams can be created on Collections, Arrays, or I/O resources
There is more

● Simply put, streams are wrappers around a data source, allowing us to


operate with that data source and making bulk processing convenient and
fast.
● A stream does not store data and, in that sense, is not a data structure. It
also never modifies the underlying data source.
Map - Reduce as a concept

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");

List<String> avengers = marvelKids.stream().collect(Collectors.toList());


Stream map
List<String> marvelKids = Arrays.asList("Black Widow", "Thor", "Captain America", "Iron Man");

List<Integer> marvelKidsNameLengths = marvelKids.stream()

.map(name -> name.length())

.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);

Optional<Integer> sum = numbers.stream()

.reduce((x, y) -> x + y);


Flat map
List<String> words = Arrays.asList("two birds", "three green peas");

List<String> estrangedWords = words.stream().flatMap(e -> Arrays.stream(e.split(" ")))

.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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy