JAVA
JAVA
1. Introduction to Java
• JDK, JVM & JRE
• Concept of OOPS
• IO Operators
• Class vs Object
• Private vs Public vs Protected
• Keywords: final, static, new
2. Inheritance
• Define Inheritance
• Types
• Multiple level and Multiple Base Inheritance
• Nesting inside class?
3. Core Concept of Java
• Overloading
• Constructor
• Abstract Class
• Interface Class
• Keywords: this, super
4. More Java
• Strings
• Package
• Exception Handling
• Throw & Throws
• Multi-Threading
5. Programs
• Armstrong
• Factorial
• Matrix
• Pattern
• Big
Introduction to Java
• Java is another object oriented programming language.
• Java is cross-platform, and by cross-platform, i mean, when we compile
our code into byte-code, we can port to any device.
• Java has its own system to execute.
• Java has huge community to work with, which means lots of library and
resources available.
1
• It physically exists. It contains JRE + development tools.
JRE
• JRE is an acronym for Java Runtime Environment.
• It is used to provide the runtime environment.
• It is the implementation of JVM. It physically exists.
• It contains a set of libraries + other files that JVM uses at runtime.
JVM
• JVM (Java Virtual Machine) is an abstract machine.
• It is called a virtual machine because it doesn’t physically exist.
• It is a specification that provides a runtime environment in which Java
bytecode can be executed.
• It can also run those programs which are written in other languages and
compiled to Java bytecode.
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
Concept of OOPS
1. Inheritance:
• Java supports inheritance, allowing a class to inherit properties and
behaviors from another class.
• This promotes code reuse and the creation of a hierarchy of classes.
2. Polymorphism:
• Polymorphism enables objects of different types to be treated as ob-
jects of a common base type.
• It includes features like function overloading and virtual functions.
3. Encapsulation:
• Encapsulation refers to the bundling of data and methods that oper-
ate on the data into a single unit (class).
• It helps in data hiding and protecting the internal state of an object.
4. Abstraction:
• Abstraction involves simplifying complex systems by modeling classes
based on the essential properties and behaviors they share.
• It allows the programmer to focus on the relevant details while ignor-
ing unnecessary complexities.
IO Operators
• System.out.println("Hello, World!");
• With using this line of code, we can print things out on screen.
2
• Like, other languages do, we can print from String to Int or Float, java
can do it too.
1. Basic Hello World Program
/*
* Things to note:
* File name: Main.java
* Class name: Main
* File and Class name must match.
* else, it will throw err.
*
* This is the standard way of writing java code.
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
int a = 10;
int b = 20;
int c = a + b;
3
System.out.println(c);
}
}
Class vs Object
• Classes define the blueprint for objects, encapsulating data and methods.
• Class is similar group of objects.
• Object is a physical entity.
public class Main {
public static void main(String args[]) {
/* OBJECT */
Test ref_variable = new Test();
}
}
/* CLASS */
public class Test {
public int some_variable;
System.out.println(ref_variable.some_variable);
/* output
Main.java:5: error: some_variable has private access in Test
System.out.println(ref_variable.some_variable);
^
1 error
error: compilation failed
*/
4
// But, its own class can use it.
}
}
Public:
public class Main {
public static void main(String args[]) {
Test ref_variable = new Test();
System.out.println(ref_variable.some_variable);
/* output
* 0
*/
Protected:
• Main.java
package UserPackage;
/*
* NOTICE: we cannot use protected with class
5
public class Main {
protected void some_method() {
}
}
• Test.java
import UserPackage.*;
/* OUTPUT:
Test.java:6: error: some_method() has protected access in Main
ref_variable.some_method();
^
1 error
error: compilation failed
*/
}
}
• Solution to above problem.
import UserPackage.*;
6
Keywords: final, static, new
final keyword: variable
public class Test {
public static void main(String args[]) {
final int a = 10;
a = 20;
/*
Test.java:5: error: cannot assign a value to final variable a
a = 20;
^
1 error
error: compilation failed
*/
class A {
final void some_method() {
}
}
class B extends A {
void some_method() {
}
}
/*
Test.java:12: error: some_method() in B cannot override some_
method() in A
void some_method() {
^
overridden method is final
1 error
error: compilation failed
7
*/
final class A {
void some_method() {
}
}
class B extends A {
void some_method() {
}
}
/*
Test.java:11: error: cannot inherit from final A
class B extends A {
^
1 error
error: compilation failed
*/
static keyword
• Setting memebers as static create only one instance of it, which means
other shared object can use it, without having to allocate memory again
and again.
public class Test {
public static void main(String args[]) {
Product prod_a = new Product();
Product prod_b = new Product();
Product prod_c = new Product();
Product prod_d = new Product();
System.out.println(Product.count_number_of_product);
System.out.println(prod_a.count_number_of_product);
System.out.println(prod_b.count_number_of_product);
8
System.out.println(prod_c.count_number_of_product);
System.out.println(prod_d.count_number_of_product);
}
}
class Product {
public static int count_number_of_product;
Product() {
// increment the number of product
count_number_of_product++;
System.out.println("Product has been created.");
}
}
Product has been created.
Product has been created.
Product has been created.
Product has been created.
4
4
4
4
4
Define Inheritance
Types
Multiple level and Multiple Base Inheritance
Nesting inside class?
Function Overloading
• Function Overloading, refers to when more than one function has same
name but with different parameters.
• On runtime, it decides which function to call.
public class Test {
public static void main(String args[]) {
Test ref_variable = new Test();
System.out.println(ref_variable.sum());
System.out.println(ref_variable.sum(40, 50));
System.out.println(ref_variable.sum(40.0055, 20.0044));
}
int sum() {
9
return 10 + 20;
}
Constructor
• Constructor are special function which belongs to class, and they have a
purpose.
• Constructor initialize the object.
• Constructor are called first before any function by default.
• Constructor don’t have any return type, yet they return reference to the
object.
public class Test {
public static void main(String args[]) {
A ref_variable = new A();
}
}
class A {
A() {
System.out.printf("Constructor called.");
}
}
Constructor called.
• Like, other function Constructor are still function so they support over-
loading.
public class Test {
public static void main(String args[]) {
A ref_variable_a = new A();
A ref_variable_b = new A(10);
}
}
10
class A {
A() {
System.out.println("Constructor called.");
}
A(int a) {
System.out.println("Constructor called. " + a);
}
}
Constructor called.
Constructor called. 10
• Copy Constructor, it takes the already initialized object and copies it
instance variable value.
public class Test {
public static void main(String args[]) {
A ref_variable_a = new A(10);
A ref_variable_b = new A(ref_variable_a);
System.out.println(ref_variable_a.a);
System.out.println(ref_variable_b.a);
}
}
class A {
int a;
A(int a) {
this.a = a;
System.out.println("Constructor called.");
}
A(A object) {
this.a = object.a;
System.out.println("Copy Constructor called.");
}
}
Constructor called.
Copy Constructor called.
10
10
11
Abstract Class
• abstract, is a keyword used before class, or method declaration.
• Data abstraction is the process of hiding certain details and showing only
essential information to the user.
• Abstract class: we cannot create object of this type class, it needs to be
inherited by other class to make it useable.
• Abstract Method: they don’t have a body, the class that is going to
inherit needs to provide the body.
public class Test {
public static void main(String args[]) {
B ref_variable = new B();
ref_variable.display();
ref_variable.default_display();
}
}
abstract class A {
abstract void display(); // no-body
void default_display() {
// non-abstract can have body
System.out.println("I have multiple family. multiple bae. hate 1st one.");
}
}
class B extends A {
void display() {
// provide body here.
System.out.println("2nd wife. sassy!!");
}
}
2nd wife. sassy!!
I have multiple family. multiple bae. hate 1st one.
Interface Class
• interface keyword, used same way as abstract.
• Interface: is like abstract, but it does not have a method which can have
a body, means, every method inside of interface has to be implemented by
the inherited class.
• Like, abstract, interface cannot be used to create object.
• Interface: methods are by default, abstract, public (that’s why it’s
required to override every method.)
• Interface: attributes are by default, public, static, final
• Why we need interface that just acts like abstract class?: Java
12
does not support multiple inheritance, with support of interface, we can
use multiple class to be inherited from.
Example: Interface
public class Test {
public static void main(String args[]) {
B ref_variable = new B();
ref_variable.display();
ref_variable.default_display();
}
}
interface A {
// must be public, otherwise cause an error.
public void display(); // no-body
public void default_display();
}
class B implements A {
// must be public, otherwise cause an error.
public void display() {
// provide body here.
System.out.println("2nd wife. sassy!!");
}
13
interface Anime {
public void Rate(int scale);
public void WhichAnime(String name);
}
interface Hentai {
public void IsPGRated(boolean value);
public void HasStory(boolean value);
}
Example 1:
public class Test {
public static void main(String args[]) {
B ref_variable = new B();
}
14
}
class A {
int instance_a;
int instance_b;
A() {
System.out.println("Constructor called.");
}
A(int a, int b) {
System.out.println("Constructor called. " + a + ", " + b);
this.instance_a = a;
this.instance_b = b;
}
}
class B extends A {
B() {
super(10, 20); // calling parent class constructor
}
}
Constructor called. 10, 20
Example 2:
public static void main(String args[]) {
A ref_variable = new A();
}
}
class A {
int instance_a;
int instance_b;
A() {
this();
}
}
Test.java:12: error: recursive constructor invocation
this();
^
1 error
error: compilation failed
15