The slide contains the Introduction of Object Oriented Programming Language using Java. It covers basics of OOP, Inheritance,Polymorphism, Exception etc.
2. JAVA
Java is a popular programming language.
Java is used to develop mobile apps, web apps, desktop apps, games and
much more.
3. Hello World Program
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
4. Program in detail…
1. Every line of code that runs in Java must be inside a class.
2. In our example, we named the class Main. A class should always start with an uppercase first
letter. (Lowercase is allowed but discouraged).
3. The name of the java file must match the class name.
4. When saving the file, save it using the class name and add ".java" to the end of the filename.
5. Program in detail…
public static void main(String [] args)
The method main() is the main entry point into a Java program; this is where the processing
starts.
Also allowed is the signature public static void main(String… args).
System.out.println()
Inside the main() method, we can use the println() method to print a line of text to the screen:
6. Java Comments
1. Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed).
E.g.
// This is a comment
System.out.println("Hello World");
2. Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by Java.
7. Java Comments
E.g.
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
8. Array
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type with square brackets:
String[] cars;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
9. Access the Elements of an Array
You can access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
Example Get your own Java Server
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
10. Loop Through an Array
The following example outputs all elements in the cars array:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
11. Java Methods
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known
as functions.
12. Create a Method
public class Main {
static void myMethod() {
// code to be executed
}
}
Example Explained
•myMethod() is the name of the method.
•static means that the method belongs to the Main class and not an object of the Main class.
•void means that this method does not have a return value.
13. Call a Method
To call a method in Java, write the method's name followed by two parentheses () and a semicolon;
In the following example, myMethod() is used to print a text (the action), when it is called:Inside main, call the
myMethod() method:
public class Main {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}
// Outputs "I just got executed!"
14. Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
16. Inheritance
•Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
•Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
20. Constructor in subclasses
Constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.
Constructor has the same name as the class name.
A constructor doesn't have a return type.
A Java program will automatically create a constructor if it is not already defined in the
program. It is executed when an instance of the class is created.
A constructor cannot be static, abstract, final or synchronized. It cannot be overridden.
21. Order of execution
Parent Class constructor is executed first.
Child class constructor is executed second.
Program- subclass.java
23. Method Overloading
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
Advantage of method overloading
Method overloading increases the readability of the program.
24. Method Overloading
There are two ways to overload the method in java
1.By changing number of arguments
2.By changing the data type
25. Method Overloading: changing no. of
arguments
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
26. changing data type of arguments
class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
27. Method Overriding
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.
•Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.
•Method overriding is used for runtime polymorphism
28. Rules for Java Method Overriding
1.The method must have the same name as in the parent class
2.The method must have the same parameter as in the parent class.
3.There must be an IS-A relationship (inheritance).
4.Program Bike2.java
29. Exception Handling
In Java, an exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime.
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
30. Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the
application. An exception normally disrupts the normal flow of the application; that is
why we need to handle exceptions.
31. Hierarchy of Java Exception classes
The java.lang.Throwable class is the root class of Java Exception hierarchy
inherited by two subclasses: Exception and Error. The hierarchy of Java Exception
classes is given below:
32.
33. Types of Java Exceptions
There are mainly two types of exceptions: checked and unchecked. An error is
considered as the unchecked exception. However, according to Oracle, there are three
types of exceptions namely:
1.Checked Exception
2.Unchecked Exception
3.Error
35. Difference between Checked and
Unchecked Exceptions
1) Checked Exception
The classes that directly inherit the Throwable class except RuntimeException and Error
are known as checked exceptions. For example, IOException, SQLException, etc.
Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that inherit the RuntimeException are known as unchecked exceptions. For
example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException,
etc. Unchecked exceptions are not checked at compile-time, but they are checked at
runtime.
3) Error
Error is irrecoverable. Some example of errors are OutOfMemoryError,
VirtualMachineError, AssertionError etc.
36. Java Exception
Try - The "try" keyword is used to specify a block where we should place an exception code. It
means we can't use try block alone. The try block must be followed by either catch or finally.
Catch - The "catch" block is used to handle the exception. It must be preceded by try block
which means we can't use catch block alone. It can be followed by finally block later.
Finally- The "finally" block is used to execute the necessary code of the program. It is executed
whether an exception is handled or not.
Throw- The "throw" keyword is used to throw an exception.
Throws- The "throws" keyword is used to declare exceptions. It specifies that there may
occur an exception in the method. It doesn't throw an exception. It is always used with method
signature.