You are on page 1of 48

Brief History of Java

In 1990, Sun Microsystems began an internal project known as


the Green Project to work on a new technology.
In 1992, the Green Project was spun off and its interest directed
toward building highly interactive devices for the cable TV
industry. This failed to materialize.
In 1994, the focus of the original team was re-targeted, this time
to the use of Internet technology. A small web browser called
HotJava was written. Oak was renamed to Java after learning
that Oak had already been trademarked.
In 1995, Java was first publicly released.
In 1996, Java Development Kit (JDK) 1.0 was released.
In 2002, JDK 1.4 (codename Merlin) was released, the most
widely used version.
In 2004, JDK 5.0 (codename Tiger) was released, the latest
version.
James Gosling

 James Gosling is generally


credited as the inventor of the
Java programming language
 He was the first designer of Java
and implemented its original
compiler and virtual machine
 He is also known as the Father of
Java
 He is currently the Chief
Technical Officer of Sun
Microsystems
What is Java?

A multi-platform, network-centric, object-oriented


programming language
 Multi-platform

 It can run on almost any computer platform

 Network-centric

 Designed with network in mind – “the network

is the computer”
 Designed for building applications for the

Internet
 Object-oriented

 It incorporates object-oriented programming

model
Java Platform Editions

A Java Platform is the set of APIs, class libraries, and other programs
used in developing Java programs for specific applications

There are 3 Java Platform Editions

1. Java 2 Platform, Standard Edition (J2SE)


• Core Java Platform targeting applications running on workstations

2. Java 2 Platform, Enterprise Edition (J2EE)


• Component-based approach to developing distributed, multi-tier
enterprise applications

3. Java 2 Platform, Micro Edition (J2ME)


• Targeted at small, stand-alone or connectable consumer and
embedded devices
Java Development Kit (JDK)
Java Development Kit (JDK)
• Is a set of Java tools for developing Java programs
• Consists of Java API, Java Compiler, and JVM
Java Application Programming Interface (API)
• Is prewritten code, organized into packages of similar topics
Java Virtual
Machine (JVM) JDK
•Is an
execution engine that JRE
runs compiled Java
MyProgram.java
byte code
Java API
Java Virtual Machine
Hardware - Based Platform
Key Points

 A Java source file can include package, import


and class declarations in that order
 The main() method is the start of execution of a
Java application
 Each Java statement is terminated by a semicolon
“;”
 Identifiers are case-sensitive
 Java keywords cannot be used as identifiers
 Each variable must be declared with a data type
 There are 8 primitive data types: boolean, char,
byte, short, int, long, float and double
 There are 3 reference data types: class, array and
interface
Key Points (continued)

 Use unary, arithmetic operators for basic


mathematical operations
 Use string operator to concatenate strings
 Use relational operators to compare objects
 Use conditional operator as alternative to if-
else() statement
 Use logical operators to compare boolean values
 Use assignment operators to assign values to
variables
 Get familiar with object, shift and bitwise
operators
 Java evaluates operators in order of precedence
Key Points
 if() and switch() are used for
branching statements

 while(), do-while() and for() are


used for iterating statements

 break, continue and label are used


to branch inside loops
Key Points
 An array is a collection of values (either
primitive or objects) all of which have
the same data type

 There are 3 steps in creating an array:


 Declaration
 Creation
 Initialization

 Array elements are accessed using an


index that starts at 0
Key Points
 A method refers to what an object or class
can do
 A method must have a return type, a
name, and optional parameters
 The method signature refers to a method
name and its parameters
 Return statement returns a value to its
caller or returns control to its caller
 A method that does not return a value
must specify void as a return type
 Calling a method should match its method
signature
 When calling a method in the same class,
What is an Exception?
 An event during program
execution that prevents the
program from continuing
normally

 An error condition that changes


the normal flow of control in a
program
Types of Exceptions
 All exceptions in Java are objects of
Throwable class
 Unchecked Exceptions
 are exceptions derived from Error and

RuntimeException classes
 are usually irrecoverable and not
handled explicitly
 are not checked by the compiler

 Checked Exceptions
 are exceptions derived from Exception class
excluding the RuntimeException class
 must be handled explicitly
Key Points
 Exceptional conditions sometimes occur
in properly written code and must be
handled
 Exception handling in Java is build
around the “throw-and-catch” paradigm
 If a method can’t handle the exception,
it can propagate the exception to the
call stack
 All exceptions are objects of Throwable
class

Defining a Class
 A Class acts as the template from
which an instance of an object is
created. The class defines the
properties of the object and the
methods used to control the object's
behavior.
 A Class specifies the structure of data
as well as the methods which
manipulate that data. Such data and
methods are contained in each
instance of the class.
Defining an Object

An object is a self-contained
entity
with
information attributes
an object must know: and
 identity – uniqueness
behaviors
behavior an object must do:
 methods – what it can do
 attributes – structure  events – what it responds to
 state – current condition
Defining Abstraction
 Abstraction is the process of
extracting common features from
specific examples
 Abstraction is a process of defining the
essential concepts while ignoring the
inessential details
Different Types of Abstraction

 Data Abstraction
Programming languages define constructs to
simplify the way information is presented to the
programmer.
 Functional Abstraction
Programming languages have constructs that ‘gift
wrap’ very complex and low level instructions into
instructions that are much more readable.
 Object Abstraction
OOP languages take the concept even further and
abstract programming constructs as objects.
Defining a Java Class

 A Java Class denotes a category of objects, and acts


as a blueprint for creating such objects.
 It defines its members referred to as fields and
methods.
 The fields (also known as variables or attributes)
refer to the properties of the class.
 The methods (also known as operations) refer to
behaviors that the class exhibits.
Class Members
 A class member refers to one of the fields or methods of a
class.
 Static members are variables and methods belonging to a
class where only a single copy of variables and methods are
shared by each object.
 Instance members are variables and methods belonging to
objects where a copy of each variable and method is created
for each object instantiated.
Class Modifiers
 Class modifiers change the way a class can be
used.
 Access modifiers describe how a class can be
accessed.
 Non-access modifiers describe how a class can be
Modifier
manipulated.Description
(no modifier) class is accessible within its package only
public class is accessible by any class in any
package
abstract class cannot be instantiated (a class
cannot be abstract and final at the same
time)
final class cannot be extended
strictfp class implements strict floating-point
arithmetic
Access Modifiers
private default
Private features of the Only classes that are in the
Sample class can only be Clas package may access
accessed from within the s default features of classes
class itself. that are in the package
Packag
e
Sampl
e
Clas
s

Clas Clas
s s

protected public
Classes that are in the All classes may access
package and all its Clas Clas public features of the
subclasses may access s s Sample class.
protected features of the
Sample class.
* Default is not a modifier; it is just the name of the access level if no access modifier is specified.
Accessibility Scope
 Accessibility scope defines the boundary of access
to a class and its members

Scope Access
static static code can access static members
but not instance members
non- non-static code can access both static
static members and instance members
package a class and its members can be
accessed within the package they are
class declared
class members can be accessed within
the class
block local variables can be accessed only
within a block
•Encapsulation is the process of hiding an
object’s implementation from another object,
while presenting only the interfaces that should
be visible.

•Encapsulating a Class

Members of a class must always be declared with


the minimum level of visibility.
Provide setters and getters (also known as
accessors/mutators) to allow controlled access to
private data.
Provide other public methods (known as interfaces
) that other objects must adhere to in order to
interact with the object.
Key Points
 Abstraction is the process of formulating general
concepts by extracting common properties of
instances.
 A class is an abstraction of its instances.
 A Java Class denotes a category of objects.
 Class members refer to its fields and methods.
 Static members are variables and methods
belonging to a class.
 Instance members are variables and methods
belonging to objects.
 Instantiating a class means creating objects of its
own type.
 Class modifiers include: (no modifier), public,
abstract, final and strictfp.
 Member modifiers include: (no modifier), public,
protected, private, static, final, abstract,
strictfp, synchronized, native, transient and
Key Points (Continued)

 Encapsulation hides implementation details of a


class.
 Encapsulating a class means declaring members
with minimum level of visibility.
 Setters are methods whose only function is to
alter the state of an object in a controlled
manner.
 Getters are methods which only function is to
return information about the state of an object.
 Constructors are methods which set the initial
state of an object upon creation of the object.
Defining Inheritance

 Inheritance is the ability to derive new classes


from existing ones. A derived class ("subclass")
inherits the instance variables and methods of the
base class ("superclass"), and may add new
instance variables and methods.
 Inheritance defines a hierarchical relationship
among classes wherein one class shares the
attributes and methods defined in one or more
classes.
 Inheritance is a relationship among classes in which
one class shares the structure and behavior of
another. A subclass inherits from a superclass.
Relationships of Inheritance
 “is-a” relationship
 a subclass can be used wherever a superclass

can be used
 implemented in Java by extending a class
 “has-a” relationship
 a whole-class relationship between a class and

its parts
 also known as composition or aggregation
 implemented in Java by instantiating an object

inside a class
“has-a” relationship
“is-a” relationship

Vehicle Car

Engine
Wheel
Car Bus
this and super

 this is a reference to the object of the current


class
 It can be used to distinguish instance variables

from local variables


 It can be assigned to other references, or

passed as a parameter, or cast to other types


 It cannot be used in a static context

 super is a reference to the object of a superclass


 Used to access hidden members of the

superclass
 It cannot be assigned to other references, nor

passed as a parameter, nor cast to other types



Casting
 Casting is converting from one data type to another
 Implicit casting is an implied casting operation

 Explicit casting is a required casting operation

 Primitive casting is converting a primitive data type


to another data type
 Widening conversion is casting a narrower data
type to a broader data type
 Narrowing conversion is casting a broader data
type to a narrower data type
 Reference casting is converting a reference data
type to another
 Upcasting is conversion up the inheritance
hierarchy
 Downcasting is conversion down the inheritance
hierarchy
 Casting between primitive and reference type is not
allowed
Key Points

 Inheritance is deriving a new class (subclass) from an


existing class (superclass)
 Inheritance can exhibit an “is-a” or “has-a” relationship
 In Java, a class can only inherit from a single class
 All classes inherit from Object class – the highest in the
inheritance hierarchy
 In Java, inheritance is implemented by the extends
keyword
 this is a reference to the object of the current class
 super is a reference to the object of a superclass
 this() is used to call constructors within the same class
 super() is used to invoke constructors in the immediate
superclass
 Casting is converting from one data type to another
Defining Polymorphism
 Polymorphism is the ability of different objects
to respond to the same message in different
ways. This means that different objects can have
very different method implementations for the
same message.
 Polymorphism is the ability of a new object to
implement the base functionality of a parent
object in a new way.
 Polymorphism is an object's ability to behave
differently depending on its type.
 Polymorphism is the ability of objects belonging
to different types to respond to methods of the
same name, each one according to the
Method Overloading
 Method Overloading is the process of
declaring methods with the same name but
different parameter types.

 A method can be overloaded in the same class


or in a subclass.

 Which overloaded method to call is based on


reference type and decided at compile time.

 Which overridden method to call is based on


object type and decided during runtime.
Rules of Method Overloading
• Overloaded methods must change the argument
list.

• Overloaded methods can change the return type.

• Overloaded methods can change the access


modifier.

• Overloaded methods can declare new or broader


checked exceptions.
Method Overriding
 Method Overriding allows a subclass to
redefine methods of the same name from the
superclass.

 The key benefit of overriding is the ability to


define/defer behavior specific to subclasses.

 Which overridden method to call is based on


object type and decided at runtime.
Rules of Method Overriding
• An overridden method must have
• the same name

• the same number of parameters and types

• the same return type

as the overridden method.


• Overriding a method cannot narrow the method
access level defined in the overridden method.
• Overriding a method cannot widen checked
exceptions defined in the overridden method.
• Methods declared as private, static, or final
cannot be overridden.
• A static method cannot override an instance
method.
Overloading vs Overriding
Overridden
Criteria Overloaded Method
Method
Argumen Different Same
t list
Return Can change Same
type
Exception Can change Cannot be wider
s
Access Can change Cannot be
level narrower
Invocatio Based on reference Based on object
n type and decided at type and decided
compile time at runtime
Defining Abstract Class
 An Abstract Class is a class that provides
common behavior across a set of subclasses, but
is not itself designed to have instances of its own
 A class that dictates certain behavior but allows
its subclasses to provide implementation
 A class designed only as a parent from which sub-
classes may be derived, but which is not itself
suitable for instantiation
 A class often used to "abstract out" incomplete
sets of features which may then be shared by a
group of sibling sub-classes which add different
variations of the missing pieces
Rules on Abstract Class

 An Abstract Class cannot be instantiated


 An abstract class can SHOULD be extended
 An abstract class can have any number of
abstract methods or none at all
 A class with at least one abstract method must
be declared an abstract class
 A subclass can provide partial or full
implementations of the inherited abstract
methods
 A class that has at least one abstract method,
whether declared or inherited from an abstract
class, must be declared abstract.
Defining Interface
 An Interface defines a contract by specifying a
set of method prototypes for which each class
that implements it must adhere

 An interface is 100% abstract class

 An interface provides only a form for a class but


no implementation
 An interface defines what a class can do but not
how the class will do it
Implementing Interface
 Implementing an interface means providing
implementations for its methods
 Interfaces are implemented using the
implements keyword
 Rules on implementing the interface methods
1. Must have the same method signature and
return type
2. Cannot narrow the method accessibility

3. Cannot specify broader checked exceptions


 Interface variables are implicitly public final
static
 Interface methods are implicitly public
Abstract Class vs Interface
Considerations in using either an interface or
abstract class
 An interface is useful because any class can

implement it. But an interface, compared to an


abstract class, is a pure API specification and
contains no implementation.
 If another method is added in an interface, all

classes that implemented that interface will be


broken
 A good implementation of both is to create an

interface and let the abstract class implement


it. So when there is a need for adding methods,
it can be safely added to the abstract class
itself rather than the interface.
Defining Final Class
 A Final Class is considered complete, it cannot
be improved or specialized

 A final class ensures that its state and behavior


cannot be changed for safety and security

 To re-use a final class, you must utilize


composition instead of inheritance
Rules on Final Class
 A final class cannot be extended or subclassed

 All methods of a final class have implementations

 All methods of a final class are implicitly final

 Members of the final class cannot be inherited or


hidden

 Methods of the final class cannot be overridden


Abstract Class vs Final Class
 Abstract class provides a template of behaviors for
subclasses to follow without actually dictating to
the subclasses the exact implementation of those
behaviors.

 Final class, since it cannot be extended, encourages


composition, while abstract class encourages
inheritance.

 Inheritance, through abstract classes, introduces


tight coupling between classes and can weaken
encapsulation by deferring actual implementation
to subclasses.

 Final classes are used to prevent malicious code


What is a Collection?
A Collection (also known as container) is an object that
contains a group of objects treated as a single unit.
Any type of objects can be stored, retrieved and
manipulated as elements of collections.

Collections Framework is a unified architecture for


managing collections

Main Parts of Collections Framework


Interfaces
Core interfaces defining common functionality
exhibited by collections
Implementations
Concrete classes of the core interfaces providing
data structures
Operations
Methods that perform various operations on
Collections Hierarchy
Set and List
Collection

implements

Set List

implements extends implements

HashSet
SortedSet
extends
implements

LinkedHashSet TreeSet LinkedList Vector ArrayList


List

value “Paul” “Mark “John” “Paul” “Luke


” ”
index 0 1 2 3 4

A List cares about the


index.

ArrayList Vector LinkedList


Set

“John” “Luke”
“Paul”
“Mark” “Fred”
“Peter”

A Set cares about


uniqueness, it doesn’t
allow duplicates.
HashSet LinkedHashSet TreeSet
Map

key “Pl” “Ma” “Jn” “ul” “Le”

value “Paul” “Mark” “John” “Paul” “Luke”

A Map cares about unique


identifiers.
LinkedHash
HashMap Hashtable TreeMap
Map

You might also like