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

Brief History of Java

Embed Size (px)

Citation preview

Page 1: Brief History of Java

Brief History of JavaBrief History of Java

In 1990, Sun Microsystems began an internal project known as In 1990, Sun Microsystems began an internal project known as the the Green ProjectGreen Project to work on a new technology. to work on a new technology.

In 1992, the Green Project was spun off and its interest directed In 1992, the Green Project was spun off and its interest directed toward building highly interactive devices for the cable TV toward building highly interactive devices for the cable TV industry. This failed to materialize.industry. This failed to materialize.

In 1994, the focus of the original team was re-targeted, this time In 1994, the focus of the original team was re-targeted, this time to the use of Internet technology. A small web browser called to the use of Internet technology. A small web browser called HotJavaHotJava was written. Oak was renamed to was written. Oak was renamed to JavaJava after learning after learning that Oak had already been trademarked.that Oak had already been trademarked.

In 1995, Java was first publicly released. In 1995, Java was first publicly released.

In 1996, Java Development Kit (In 1996, Java Development Kit (JDKJDK) 1.0 was released.) 1.0 was released.

In 2002, JDK 1.4 (codename In 2002, JDK 1.4 (codename MerlinMerlin) was released, the most ) was released, the most widely used version.widely used version.

In 2004, JDK 5.0 (codename In 2004, JDK 5.0 (codename TigerTiger) was released, the latest ) was released, the latest version.version.

Page 2: Brief History of Java

James GoslingJames Gosling

James GoslingJames Gosling is generally credited is generally credited as the inventor of the Java as the inventor of the Java programming languageprogramming language

He was the first designer of Java He was the first designer of Java and implemented its original and implemented its original compiler and virtual machinecompiler and virtual machine

He is also known as the Father of He is also known as the Father of JavaJava

He is currently the Chief Technical He is currently the Chief Technical Officer of Sun MicrosystemsOfficer of Sun Microsystems

Page 3: Brief History of Java

What is Java?What is Java?

A A multi-platform, network-centric, object-oriented multi-platform, network-centric, object-oriented programming languageprogramming language Multi-platform Multi-platform

It can run on almost any computer platformIt can run on almost any computer platform Network-centricNetwork-centric

Designed with network in mind – “the network Designed with network in mind – “the network is the computer”is the computer”

Designed for building applications for the Designed for building applications for the InternetInternet

Object-oriented Object-oriented It incorporates object-oriented programming It incorporates object-oriented programming

modelmodel

Page 4: Brief History of Java

Java Platform EditionsJava 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

Page 5: Brief History of Java

Java Development Kit (JDK)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 MachineJava API

MyProgram.java

Hardware - Based Platform

JDK

JRE

Java Virtual Machine (JVM)

•Is an execution engine that runs compiled Java byte code

larry.westenberg
Fix the indent on bullet
Page 6: Brief History of Java

Key PointsKey Points

A Java source file can include package, import and A Java source file can include package, import and class declarations in that orderclass declarations in that order

The The main()main() method is the start of execution of a method is the start of execution of a Java applicationJava application

Each Java statement is terminated by a semicolon Each Java statement is terminated by a semicolon “;”“;”

Identifiers are case-sensitiveIdentifiers are case-sensitive Java keywords cannot be used as identifiersJava keywords cannot be used as identifiers Each variable must be declared with a data typeEach variable must be declared with a data type There are 8 primitive data types: There are 8 primitive data types: booleanboolean, , charchar, , bytebyte, , shortshort, , intint, , longlong, , floatfloat and and doubledouble

There are 3 reference data types: class, array and There are 3 reference data types: class, array and interfaceinterface

Page 7: Brief History of Java

Key Points (continued)Key Points (continued)

Use unary, arithmetic operators for basic mathematical Use unary, arithmetic operators for basic mathematical operationsoperations

Use string operator to concatenate stringsUse string operator to concatenate strings Use relational operators to compare objectsUse relational operators to compare objects Use conditional operator as alternative to Use conditional operator as alternative to if-else()if-else()

statementstatement Use logical operators to compare Use logical operators to compare booleanboolean values values Use assignment operators to assign values to variablesUse assignment operators to assign values to variables Get familiar with object, shift and bitwise operatorsGet familiar with object, shift and bitwise operators Java evaluates operators in order of precedenceJava evaluates operators in order of precedence CastingCasting is converting one data type to another is converting one data type to another

Page 8: Brief History of Java

Key PointsKey Points

if()if() and and switch()switch() are used for are used for branching statementsbranching statements

while()while(), , do-while()do-while() and and for()for() are are used for iterating statementsused for iterating statements

breakbreak, , continuecontinue and and labellabel are used are used to branch inside loopsto branch inside loops

Page 9: Brief History of Java

An array is a collection of values (either primitive or An array is a collection of values (either primitive or objects) all of which have the same data type objects) all of which have the same data type

There are 3 steps in creating an array:There are 3 steps in creating an array: DeclarationDeclaration CreationCreation InitializationInitialization

Array elements are accessed using an index that starts Array elements are accessed using an index that starts at 0at 0

lengthlength is the property of array that returns its size is the property of array that returns its size (i.e., number of elements)(i.e., number of elements)

Arrays can be passed to methods or assigned to Arrays can be passed to methods or assigned to variablesvariables

Key PointsKey Points

Page 10: Brief History of Java

Key PointsKey Points A method refers to what an object or class can doA method refers to what an object or class can do A method must have a return type, a name, and optional parametersA method must have a return type, a name, and optional parameters The method signature refers to a method name and its parametersThe method signature refers to a method name and its parameters Return statement returns a value to its caller or returns control to its Return statement returns a value to its caller or returns control to its

callercaller A method that does not return a value must specify A method that does not return a value must specify voidvoid as a return as a return

typetype Calling a method should match its method signatureCalling a method should match its method signature When calling a method in the same class, use only the method nameWhen calling a method in the same class, use only the method name When calling a method in a different class, use the object referenceWhen calling a method in a different class, use the object reference When calling a static method, use the class nameWhen calling a static method, use the class name Methods are invoked sequentially in the call stack and executed in Methods are invoked sequentially in the call stack and executed in

reverse orderreverse order Passing parameters in Java is always pass by valuePassing parameters in Java is always pass by value

Page 11: Brief History of Java

What is an Exception?What is an Exception?

An event during program execution that An event during program execution that prevents the program from continuing prevents the program from continuing normally normally

An error condition that changes the An error condition that changes the normal flow of control in a program normal flow of control in a program

A signal that some unexpected condition A signal that some unexpected condition has occurred in the program has occurred in the program

Page 12: Brief History of Java

Types of ExceptionsTypes of Exceptions All exceptions in Java are objects of All exceptions in Java are objects of ThrowableThrowable class class Unchecked ExceptionsUnchecked Exceptions

are exceptions derived from are exceptions derived from ErrorError and and RuntimeExceptionRuntimeException classesclasses

are usually irrecoverable and not handled explicitlyare usually irrecoverable and not handled explicitly are not checked by the compilerare not checked by the compiler

Checked ExceptionsChecked Exceptions are exceptions derived from are exceptions derived from ExceptionException class excluding the class excluding the RuntimeExceptionRuntimeException class class

must be handled explicitlymust be handled explicitly are checked by the compilerare checked by the compiler

Both Both checkedchecked and and uncheckedunchecked exceptions can be thrown and exceptions can be thrown and caughtcaught

New exceptions are created by extending the New exceptions are created by extending the ExceptionException class or its subclassesclass or its subclasses

Page 13: Brief History of Java

Key PointsKey Points Exceptional conditions sometimes occur in properly Exceptional conditions sometimes occur in properly

written code and must be handledwritten code and must be handled Exception handling in Java is build around the “Exception handling in Java is build around the “throw-throw-

and-catchand-catch” paradigm” paradigm If a method can’t handle the exception, it can propagate If a method can’t handle the exception, it can propagate

the exception to the call stackthe exception to the call stack All exceptions are objects of All exceptions are objects of ThrowableThrowable class class Only Only Checked exceptionsChecked exceptions must be handled explicitly must be handled explicitly try-catch()-finallytry-catch()-finally statement is the exception statement is the exception

handling mechanism of Javahandling mechanism of Java throwthrow statement manually creates an exception or statement manually creates an exception or

rethrows an exceptionrethrows an exception throwsthrows statement allows propagation of exception to the statement allows propagation of exception to the

call stackcall stack

Page 14: Brief History of Java

A A ClassClass acts as the template from which an instance of acts as the template from which an instance of an object is created. The class defines the properties of an object is created. The class defines the properties of the object and the methods used to control the object's the object and the methods used to control the object's behavior. behavior.

A Class specifies the structure of data as well as the A Class specifies the structure of data as well as the methods which manipulate that data. Such data and methods which manipulate that data. Such data and methods are contained in each instance of the class.methods are contained in each instance of the class.

A Class is a model or template that can be instantiated A Class is a model or template that can be instantiated to create objects with a common definition, and to create objects with a common definition, and therefore common properties, operations and behavior. therefore common properties, operations and behavior.

A Class provides a template for defining the behavior of A Class provides a template for defining the behavior of a particular type of object. Objects are referred to as a particular type of object. Objects are referred to as “instances” of a class. “instances” of a class.

Defining a ClassDefining a Class

Page 15: Brief History of Java

AnAn object object is a self-contained is a self-contained entity entity

with attributes and behaviors with attributes and behaviors

Defining an ObjectDefining an ObjectDefining an ObjectDefining an Object

information an object must know: identity – uniqueness attributes – structure state – current condition

behavior an object must do: methods – what it can do events – what it responds to

Page 16: Brief History of Java

Defining AbstractionDefining Abstraction Abstraction is the process of Abstraction is the process of

extracting common features from extracting common features from specific examplesspecific examples

Abstraction is a process of defining the Abstraction is a process of defining the essential concepts while ignoring the essential concepts while ignoring the inessential detailsinessential details

Page 17: Brief History of Java

Different Types of AbstractionDifferent Types of Abstraction

Data AbstractionData Abstraction

Programming languages define constructs to Programming languages define constructs to simplify the way information is presented to the simplify the way information is presented to the programmer. programmer.

Functional AbstractionFunctional Abstraction

Programming languages have constructs that ‘gift Programming languages have constructs that ‘gift wrap’ very complex and low level instructions into wrap’ very complex and low level instructions into instructions that are much more readable.instructions that are much more readable.

Object AbstractionObject Abstraction

OOP languages take the concept even further and OOP languages take the concept even further and abstract programming constructs as abstract programming constructs as objectsobjects. .

Page 18: Brief History of Java

A class member refers to one of the fields or methods of a class.A class member refers to one of the fields or methods of a class. Static membersStatic members are variables and methods belonging to a class where only are variables and methods belonging to a class where only

a single copy of variables and methods are shared by each object.a single copy of variables and methods are shared by each object. Instance members Instance members are variables and methods belonging to objects where a are variables and methods belonging to objects where a

copy of each variable and method is created for each object instantiated. copy of each variable and method is created for each object instantiated.

Class Members

A Java Class denotes a category of objects, and acts as a blueprint A Java Class denotes a category of objects, and acts as a blueprint for creating such objects.for creating such objects.

It defines its members referred to as It defines its members referred to as fieldsfields and and methods.methods. The The fieldsfields (also known as (also known as variablesvariables or or attributesattributes) refer to the ) refer to the

properties of the class.properties of the class. The The methodsmethods (also known as (also known as operationsoperations) refer to behaviors that ) refer to behaviors that

the class exhibits.the class exhibits.

Defining a Java Class

Page 19: Brief History of Java

Class ModifiersClass Modifiers

Modifier 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

Class modifiers change the way a class can be used.Class modifiers change the way a class can be used. Access modifiersAccess modifiers describe how a class can be accessed. describe how a class can be accessed. Non-access modifiersNon-access modifiers describe how a class can be describe how a class can be

manipulated.manipulated.

Page 20: Brief History of Java

Sample

Package

ClassClass

Class

Class

Private features of the Sample class can only be accessed from within the

class itself.

Private features of the Sample class can only be accessed from within the

class itself.

private

Classes that are in the package and all its

subclasses may access protected features of the

Sample class.

Classes that are in the package and all its

subclasses may access protected features of the

Sample class.

protected

Only classes that are in the package may access

default features of classes that are in the package

Only classes that are in the package may access

default features of classes that are in the package

default

All classes may access public features of the

Sample class.

All classes may access public features of the

Sample class.

public

Class

Class

* Default is not a modifier; it is just the name of the access level if no access modifier is specified.

Access ModifiersAccess Modifiers

Page 21: Brief History of Java

Accessibility ScopeAccessibility Scope

ScopeScope AccessAccess

static static static code can access static members static code can access static members but not instance members but not instance members

non-non-static static

non-static code can access both static non-static code can access both static members and instance members members and instance members

packagepackage a class and its members can be a class and its members can be accessed within the package they are accessed within the package they are declared declared

classclass class members can be accessed within class members can be accessed within the class the class

blockblock local variables can be accessed only local variables can be accessed only within a block within a block

Accessibility scope defines the boundary of access Accessibility scope defines the boundary of access to a class and its membersto a class and its members

Page 22: Brief History of Java

•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.

Page 23: Brief History of Java

Key PointsKey Points

AbstractionAbstraction is the process of formulating general concepts is the process of formulating general concepts by extracting common properties of instances. by extracting common properties of instances.

A class is an abstraction of its instances. A class is an abstraction of its instances. A Java Class denotes a category of objects.A Java Class denotes a category of objects. Class members refer to its Class members refer to its fieldsfields and and methods.methods. Static membersStatic members are variables and methods belonging to a are variables and methods belonging to a

class.class. Instance members Instance members are variables and methods belonging are variables and methods belonging

to objects.to objects. Instantiating a class means creating objects of its own Instantiating a class means creating objects of its own

type.type. Class modifiers include: (no modifier), Class modifiers include: (no modifier), publicpublic, , abstractabstract, , finalfinal andand strictfp.strictfp.

Member modifiers include: (no modifier), Member modifiers include: (no modifier), publicpublic, , protectedprotected, , privateprivate, , staticstatic, , finalfinal, , abstractabstract, , strictfpstrictfp, , synchronizedsynchronized, , nativenative, , transienttransient andand volatile.volatile.

Page 24: Brief History of Java

Key Points (Continued)Key Points (Continued)

Encapsulation hides implementation details of a Encapsulation hides implementation details of a class.class.

Encapsulating a class means declaring members Encapsulating a class means declaring members with minimum level of visibility.with minimum level of visibility.

SettersSetters are methods whose only function is to are methods whose only function is to alter the state of an object in a controlled alter the state of an object in a controlled manner.manner.

GettersGetters are methods which only function is to are methods which only function is to return information about the state of an object.return information about the state of an object.

ConstructorsConstructors are methods which set the initial are methods which set the initial state of an object upon creation of the object.state of an object upon creation of the object.

Page 25: Brief History of Java

Defining InheritanceDefining Inheritance

InheritanceInheritance is the ability to derive new classes is the ability to derive new classes from existing ones. A derived class ("subclass") from existing ones. A derived class ("subclass") inherits the instance variables and methods of the inherits the instance variables and methods of the base class ("superclass"), and may add new base class ("superclass"), and may add new instance variables and methods. instance variables and methods.

Inheritance defines a hierarchical relationship Inheritance defines a hierarchical relationship among classes wherein one class shares the among classes wherein one class shares the attributes and methods defined in one or more attributes and methods defined in one or more classes. classes.

Inheritance is a relationship among classes in which Inheritance is a relationship among classes in which one class shares the structure and behavior of one class shares the structure and behavior of another. A subclass inherits from a superclass.another. A subclass inherits from a superclass.

Page 26: Brief History of Java

Relationships of InheritanceRelationships of Inheritance ““is-a”is-a” relationship relationship

a subclass can be used wherever a superclass can be useda subclass can be used wherever a superclass can be used implemented in Java by implemented in Java by extendingextending a class a class

““has-a”has-a” relationship relationship a whole-class relationship between a class and its partsa whole-class relationship between a class and its parts also known as also known as composition composition or or aggregationaggregation implemented in Java by implemented in Java by instantiatinginstantiating an object inside a class an object inside a class

Car

EngineWheel

“has-a” relationship“is-a” relationship

Vehicle

Car Bus

Page 27: Brief History of Java

thisthis and and supersuper

thisthis is a reference to the object of the current class is a reference to the object of the current class It can be used to distinguish instance variables It can be used to distinguish instance variables

from local variablesfrom local variables It can be assigned to other references, or passed It can be assigned to other references, or passed

as a parameter, or cast to other typesas a parameter, or cast to other types It cannot be used in a static contextIt cannot be used in a static context

supersuper is a reference to the object of a superclass is a reference to the object of a superclass Used to access hidden members of the superclassUsed to access hidden members of the superclass It cannot be assigned to other references, nor It cannot be assigned to other references, nor

passed as a parameter, nor cast to other typespassed as a parameter, nor cast to other types It cannot be used in a static context It cannot be used in a static context

Page 28: Brief History of Java

CastingCasting

CastingCasting is converting from one data type to anotheris converting from one data type to another Implicit casting Implicit casting is an implied casting operationis an implied casting operation Explicit casting Explicit casting is a required casting operationis a required casting operation Primitive casting Primitive casting is converting a primitive data type to is converting a primitive data type to

another data typeanother data type Widening conversionWidening conversion is casting a narrower data type to a is casting a narrower data type to a

broader data typebroader data type Narrowing conversionNarrowing conversion is casting a broader data type to a is casting a broader data type to a

narrower data type narrower data type Reference castingReference casting is converting a reference data type to is converting a reference data type to

anotheranother Upcasting Upcasting is conversion up the inheritance hierarchy is conversion up the inheritance hierarchy Downcasting Downcasting is conversion down the inheritance hierarchy is conversion down the inheritance hierarchy

Casting between primitive and reference type is not allowedCasting between primitive and reference type is not allowed In Java, casting is implemented using In Java, casting is implemented using ()() operator operator

Page 29: Brief History of Java

Key PointsKey Points

InheritanceInheritance is deriving a new class (subclass) from an is deriving a new class (subclass) from an existing class (superclass)existing class (superclass)

Inheritance can exhibit an “Inheritance can exhibit an “is-ais-a” or “” or “has-ahas-a” relationship” relationship In Java, a class can only inherit from a single classIn Java, a class can only inherit from a single class All classes inherit from All classes inherit from ObjectObject class – the highest in the class – the highest in the

inheritance hierarchyinheritance hierarchy In Java, inheritance is implemented by the In Java, inheritance is implemented by the extendsextends keyword keyword thisthis is a reference to the object of the current class is a reference to the object of the current class supersuper is a reference to the object of a superclass is a reference to the object of a superclass this()this() is used to call constructors within the same class is used to call constructors within the same class super()super() is used to invoke constructors in the immediate is used to invoke constructors in the immediate

superclasssuperclass CastingCasting is converting from one data type to another is converting from one data type to another

Page 30: Brief History of Java

Defining PolymorphismDefining Polymorphism PolymorphismPolymorphism is the ability of different objects to is the ability of different objects to

respond to the same message in different ways. This respond to the same message in different ways. This means that different objects can have very different means that different objects can have very different method implementations for the same message. method implementations for the same message.

Polymorphism is the ability of a new object to Polymorphism is the ability of a new object to implement the base functionality of a parent object in a implement the base functionality of a parent object in a new way.new way.

Polymorphism is an object's ability to behave Polymorphism is an object's ability to behave differently depending on its type. differently depending on its type.

Polymorphism is the ability of objects belonging to Polymorphism is the ability of objects belonging to different types to respond to methods of the same different types to respond to methods of the same name, each one according to the appropriate type-name, each one according to the appropriate type-specific behavior. specific behavior.

Page 31: Brief History of Java

Method OverloadingMethod Overloading Method OverloadingMethod Overloading is the process of is the process of

declaring methods with the same name but declaring methods with the same name but different parameter types.different parameter types.

A method can be overloaded in the same class A method can be overloaded in the same class or in a subclass.or in a subclass.

Which overloaded method to call is based on Which overloaded method to call is based on reference typereference type and decided at and decided at compile time. compile time.

Which overridden method to call is based on Which overridden method to call is based on object typeobject type and decided during and decided during runtime.runtime.

Page 32: Brief History of Java

Rules of Method OverloadingRules of Method Overloading

1.1. Overloaded methods Overloaded methods mustmust change the argument change the argument list.list.

2.2. Overloaded methods can change the return type.Overloaded methods can change the return type.

3.3. Overloaded methods can change the access Overloaded methods can change the access modifier.modifier.

4.4. Overloaded methods can declare new or broader Overloaded methods can declare new or broader checked exceptions.checked exceptions.

Page 33: Brief History of Java

Method OverridingMethod Overriding

Method OverridingMethod Overriding allows a subclass to allows a subclass to redefine methods of the same name from the redefine methods of the same name from the superclass. superclass.

The key benefit of overriding is the ability to The key benefit of overriding is the ability to define/defer behavior specific to subclasses.define/defer behavior specific to subclasses.

Which overridden method to call is based on Which overridden method to call is based on object typeobject type and decided at and decided at runtime.runtime.

Page 34: Brief History of Java

Rules of Method OverridingRules of Method Overriding

1.1. An overridden methodAn overridden method mustmust have have• the same namethe same name• the same number of parameters and typesthe same number of parameters and types• the same return typethe same return typeas the as the overridden method.overridden method.

2.2. Overriding a method cannot narrow the method Overriding a method cannot narrow the method access level defined in the overridden method.access level defined in the overridden method.

3.3. Overriding a method cannot widen checked Overriding a method cannot widen checked exceptions defined in the overridden method.exceptions defined in the overridden method.

4.4. Methods declared as Methods declared as privateprivate, , staticstatic, or , or finalfinal cannot be overridden. cannot be overridden.

5.5. A static method cannot override an instance method.A static method cannot override an instance method.

Page 35: Brief History of Java

Overloading vs OverridingOverloading vs Overriding

CriteriaCriteria Overloaded MethodOverloaded Method Overridden Overridden MethodMethod

ArgumenArgument list t list

DifferentDifferent SameSame

Return Return typetype

Can changeCan change SameSame

ExceptionExceptions s

Can changeCan change Cannot be widerCannot be wider

Access Access levellevel

Can changeCan change Cannot be Cannot be narrowernarrower

InvocatioInvocationn

Based on reference Based on reference type and decided at type and decided at compile timecompile time

Based on object Based on object type and decided type and decided at runtimeat runtime

Page 36: Brief History of Java

Defining Abstract ClassDefining Abstract Class

An An Abstract ClassAbstract Class is a class that provides is a class that provides common behavior across a set of subclasses, but common behavior across a set of subclasses, but is not itself designed to have instances of its ownis not itself designed to have instances of its own

A class that dictates certain behavior but allows A class that dictates certain behavior but allows its subclasses to provide implementation its subclasses to provide implementation

A class designed only as a parent from which sub-A class designed only as a parent from which sub-classes may be derived, but which is not itself classes may be derived, but which is not itself suitable for instantiation suitable for instantiation

A class often used to "abstract out" incomplete A class often used to "abstract out" incomplete sets of features which may then be shared by a sets of features which may then be shared by a group of sibling sub-classes which add different group of sibling sub-classes which add different variations of the missing pieces variations of the missing pieces

Page 37: Brief History of Java

Rules on Abstract ClassRules on Abstract Class

An Abstract Class cannot be instantiatedAn Abstract Class cannot be instantiated An abstract class can SHOULD be extendedAn abstract class can SHOULD be extended An abstract class can have any number of An abstract class can have any number of

abstract methods or none at allabstract methods or none at all A class with at least one abstract method must A class with at least one abstract method must

be declared an abstract classbe declared an abstract class A subclass can provide partial or full A subclass can provide partial or full

implementations of the inherited abstract implementations of the inherited abstract methodsmethods

A class that has at least one abstract method, A class that has at least one abstract method, whether declared or inherited from an abstract whether declared or inherited from an abstract class, must be declared abstract.class, must be declared abstract.

Page 38: Brief History of Java

Defining InterfaceDefining Interface

An An InterfaceInterface defines a contract by specifying a defines a contract by specifying a set of method prototypes for which each class set of method prototypes for which each class that implements it must adherethat implements it must adhere

An interface is 100% abstract classAn interface is 100% abstract class

An interface provides only a form for a class but An interface provides only a form for a class but no implementationno implementation

An interface defines An interface defines whatwhat a class can do but not a class can do but not howhow the class will do it the class will do it

Page 39: Brief History of Java

Implementing InterfaceImplementing Interface

Implementing an interface means providing Implementing an interface means providing implementations for its methodsimplementations for its methods

Interfaces are implemented using the Interfaces are implemented using the implementsimplements keywordkeyword

Rules on implementing the interface methodsRules on implementing the interface methods

1.1. Must have the same method signature and return Must have the same method signature and return typetype

2.2. Cannot narrow the method accessibilityCannot narrow the method accessibility

3.3. Cannot specify broader checked exceptionsCannot specify broader checked exceptions Interface variables are implicitly Interface variables are implicitly public final public final staticstatic

Interface methods are implicitly Interface methods are implicitly public abstractpublic abstract

Page 40: Brief History of Java

Defining Final ClassDefining Final Class

A A Final ClassFinal Class is considered complete, it cannot is considered complete, it cannot be improved or specialized be improved or specialized

A final class ensures that its state and behavior A final class ensures that its state and behavior cannot be changed for safety and security cannot be changed for safety and security

To re-use a final class, you must utilize To re-use a final class, you must utilize composition instead of inheritancecomposition instead of inheritance

Page 41: Brief History of Java

Rules on Final ClassRules on Final Class

A final class cannot be extended or subclassedA final class cannot be extended or subclassed

All methods of a final class have implementations All methods of a final class have implementations

All methods of a final class are implicitly All methods of a final class are implicitly finalfinal

Members of the final class cannot be inherited or Members of the final class cannot be inherited or hiddenhidden

Methods of the final class cannot be overriddenMethods of the final class cannot be overridden

Page 42: Brief History of Java

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.

What is a Collection?

Collections Framework is a unified architecture for managing collections

Main Parts of Collections FrameworkInterfaces

Core interfaces defining common functionality exhibited by collections

ImplementationsConcrete classes of the core interfaces providing data structures

OperationsMethods that perform various operations on collections

Page 43: Brief History of Java

Collections HierarchyCollections HierarchySet and ListSet and List

HashSet

Collection

SortedSet

ListSet

LinkedHashSet TreeSet LinkedList Vector ArrayList

implements

implements

implements

implements extends

extends

Page 44: Brief History of Java

A List cares about the index.A List cares about the index.A List cares about the index.A List cares about the index.

ListList

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

index 0 1 2 3 4

LinkedListLinkedListLinkedListLinkedListVectorVectorVectorVectorArrayListArrayListArrayListArrayList

Page 45: Brief History of Java

A Set cares about uniqueness, it doesn’t allow duplicates.A Set cares about uniqueness, it doesn’t allow duplicates.A Set cares about uniqueness, it doesn’t allow duplicates.A Set cares about uniqueness, it doesn’t allow duplicates.

SetSet

“Paul”“Paul”

“Mark”“Mark”

“John”“John” “Luke”“Luke”

“Fred”“Fred”“Peter”“Peter”

TreeSetTreeSetTreeSetTreeSetLinkedHashSetLinkedHashSetLinkedHashSetLinkedHashSetHashSetHashSetHashSetHashSet

Page 46: Brief History of Java

A Map cares about unique identifiers.A Map cares about unique identifiers.A Map cares about unique identifiers.A Map cares about unique identifiers.

MapMap

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

key

value

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

LinkedHashLinkedHashMapMap

LinkedHashLinkedHashMapMap TreeMapTreeMapTreeMapTreeMapHashtableHashtableHashtableHashtableHashMapHashMapHashMapHashMap