Professional Documents
Culture Documents
Lecture – 01
Spring 2021
Waqas Tariq Dar
GIFT University
What is Java?
Java is a cross-platform object-oriented
programming language with advanced and
simplified features.
Independent programming language that
follows the logic of “Write once, Run
anywhere” i.e., the compiled code can run
on all platforms which supports java.
Java Virtual Machine (JVM)
A Java virtual machine (JVM) is a virtual machine that
enables a computer to run Java programs as well as
programs written in other languages that are also
compiled to Java bytecode.
Bytecode is a highly optimized set of instructions
designed to be executed by what is called the Java
Virtual Machine (JVM), which is part of the Java
Runtime Environment (JRE)
With the use of JIT (Just-In-Time) compilers, it enables
high performance.
Building and Running a Java Program
You need JDK (Java Development Kit)
Create a class file e.g., Book.java
Set JDK path (bin directory)
Compile
●
javac Book.java
●
Above command will create a Book.class file
Run
●
java Book
Java is different than C++
No Preprocessor
No Global Variables
No Goto statements
No Pointers
No Multiple Inheritance
No Operator Overloading
Object & classes
Object - Objects have states and behaviors.
●
Example: A dog has states - color, name, breed as well
as:
●
Behaviors - wagging, barking, eating.
An object is an instance of a class.
Class - A class can be defined as a template/blue print
that describes the behaviors/states that object of its type
support.
Java Access Modifiers
Method Overloading
●
When a class has two or more methods by same name but different
parameters, it is known as method overloading.
// for integer
public static int sum(int n1, int n2) {
return n1+n2;
}
// for double
public static double sum (double n1, double n2) {
return n1+n2 ;
}
}
Method Overriding
A child class can override the definition of an inherited
method in favor of its own.
The new method must have the same signature as the
parent's method, but can have a different body.
The type of the object executing the method determines
which version of the method is invoked.
A parent method can be invoked explicitly using the super
reference.
If a method is declared with the final modifier, it cannot be
overridden.
Overriding vs Overloading
Don't confuse the concepts of overloading and overriding
Overloading deals with multiple methods with the same
name in the same class, but with different signatures
Overriding deals with two methods, one in a parent class
and one in a child class, that have the same signature
Overloading lets you define a similar operation in
different ways for different data
Overriding lets you define a similar operation in different
ways for different object types
Polymorphism in Java
Polymorphism is the ability to create a variable, a
function, or an object that has more than one form.
In java language, Polymorphism is essentially
considered into two versions.
Compile time Polymorphism (static binding or
method overloading)
Runtime Polymorphism (dynamic binding or method
overriding)
Compile time Polymorphism - Example
Parameter type: Type of method parameters can be different. e.g.
java.util.Math.max() function comes with following versions:
public static double Math.max(double a, double b){..}
public static float Math.max(float a, float b){..}
public static int Math.max(int a, int b){..}
Parameter count: Functions accepting different number of
parameters. e.g. in employee management application, a factory
can have these methods:
EmployeeFactory.create(String firstName, String lastName){...}
EmployeeFactory.create(Integer id, String firstName, String
lastName){...}
Runtime Polymorphism - Example
Runtime polymorphism is essentially referred as
method overriding. Method overriding is a feature
which you get when you implement inheritance in
your program.
Runtime Polymorphism - Example
public class Animal {
public void makeNoise()
{
System.out.println("Some sound");
}
}
class Dog extends Animal{
public void makeNoise()
{
System.out.println("Bark");
}
}
class Cat extends Animal{
public void makeNoise()
{
System.out.println("Meawoo");
}
}
Runtime Polymorphism - Example
Abstraction is the quality of dealing with ideas
rather than events.
In Object oriented programming Abstraction is a
process of hiding the implementation details from
the user, only the functionality will be provided to
the user.
In other words user will have the information on
what the object does instead of how it does it.
Ways to Achieve Abstraction
There are two ways to achieve abstraction in
java.
Abstract class (0 to 100%)
Interface (100%)
Abstract Class
A class which contains the abstract keyword in its
declaration is known as abstract class.
Abstract classes may or may not contain abstract
methods.
But, if a class have at least one abstract method, then the
class must be declared abstract.
If a class is declared abstract it cannot be instantiated.
To use an abstract class you have to inherit it from
another class, provide implementations to the abstract
methods in it.
Abstract Method
If you want a class to contain a particular method but you want
the actual implementation of that method to be determined by
child classes, you can declare the method in the parent class
as abstract.
Abstract keyword is used to declare the method as abstract.
You have to place the abstract keyword before the method
name in the method declaration.
An abstract method contains a method signature, but no
method body.
Instead of curly braces an abstract method will have a
semicolon ( ; ) at the end.
Abstract Method
•Any class inheriting the current class must override the abstract
method.
Interface
An interface is a class-like construct that contains only
constants (static and final) and abstract methods.
In many ways an interface is similar to an abstract class, but
its intent is to specify common behavior for objects of related
classes or unrelated classes.
Multiple inheritance can be achieved with Interfaces.
modifier interface InterfaceName {
/** Constant declarations */
/** Abstract method signatures */
}
Interface
An interface is treated like a special class in Java. Each
interface is compiled into a separate bytecode file, just
like a regular class.
You can use an interface more or less the same way you
use an abstract class.
As with an abstract class, you cannot create an instance
from an interface using the new operator.
You can use the Edible interface to specify whether an
object is edible.
This is accomplished by letting the class for the object
implement this interface using the implements keyword.
… Interface – Edible Interface Example
… Interface – Edible Interface Example...
… Interface – Edible Interface Example...
… Interface – Edible Interface Example
References
...