Upload
markus-eisele
View
3.549
Download
2
Tags:
Embed Size (px)
DESCRIPTION
This is a mini-lesson on Java concepts and syntax, aimed at PL/SQL developers and DBAs who need to know sufficient Java to start build ADF Components with Java.Held at KScope 11 in Long Beach.
Citation preview
msg systems ag, JUNE 26 - 2011 Markus Eisele 1
All the Java ADF Beginners
need to know! – Part 2
Markus Eisele
Oracle ACE Director
msg systems ag, Germany
msg systems ag, JUNE 26 - 2011 Markus Eisele 3
1. Introduction
2. Basic Object Oriented Concepts
3. Classes and Objects
4. Exceptions
5. Generics
6. Design Pattern
Overview
msg systems ag, JUNE 26 - 2011 Markus Eisele 4
• YOU„VE MISSED (or skipped) part 1 ;) (The real
basics)
• Let„s get more theoretical and detailed now!
• Disclaimer:
Even if there is “ADF” in the title, we still don’t
touch ADF or even open up JDeveloper …
Introduction
msg systems ag, JUNE 26 - 2011 Markus Eisele 5
Why do you have „ADF“ in the title?
msg systems ag, JUNE 26 - 2011 Markus Eisele 6
What is Object Orientation?
Procedural paradigm:
• Software is organized around the notion of procedures
• Procedural abstraction
Works as long as the data is simple
• Adding data abstractions
Groups together the pieces of data that describe some entity
Helps reduce the system‟s complexity.
- Such as Records and structures
Object oriented paradigm:
• Organizing procedural abstractions in the context of data abstractions
msg systems ag, JUNE 26 - 2011 Markus Eisele 7
Object Orientation paradigm
An approach to the solution of problems in which all
computations are performed in the context of objects.
• The objects are instances of classes, which:
are data abstractions
contain procedural abstractions that operation on the
objects
• A running program can be seen as a collection of objects
collaborating to perform a given task
msg systems ag, JUNE 26 - 2011 Markus Eisele, Oracle ACE Director FMW & SOA 8
Necessary for a system or language to be object oriented (OO)
• Identity
Each object is distinct from each other object, and can be referred to
Two objects are distinct even if they have the same data
• Classes
The code is organized using classes, each of which describes a set of
objects
• Inheritance
The mechanism where features in a hierarchy inherit from superclasses to
subclasses
• Polymorphism
The mechanism by which several methods can have the same name and
implement the same abstract operation.
Concepts that Define Object Orientation
msg systems ag, JUNE 26 - 2011 Markus Eisele, Oracle ACE Director FMW & SOA 9
• Abstraction
Object -> something in the world
Class -> objects
Superclass -> subclasses
Operation -> methods
Attributes and associations -> instance variables
• Modularity
Code can be constructed entirely of classes
• Encapsulation
Details can be hidden in classes
This gives rise to information hiding:
Programmers do not need to know all the details of a class
Other Concepts
msg systems ag, JUNE 26 - 2011 Markus Eisele 10
• Object
A chunk of structured data
Has properties (attributes)
Representing it„s state
Has behaviour (methods)
Representing how it acts and reacts
Simulates the behaviour of the object in the real worl
• Class
Is a unit of abstraction
It„s instances represent similar objects
Is a kind of software module
Has methods
to implement behaviour
Classes and Objects
msg systems ag, JUNE 26 - 2011 Markus Eisele 11
• Something should be a class if it could have
instances
• Something should be an instance if it is clearly a
single member of a set defined by a class
• Person
Class: instances are individual persons
• Markus
Instance: A person with the name: „Markus“
• …
• The act of creating an object instance of a class is
called the instantiation of the class.
Person markus = new Person(„Markus“);
Class or Instance?
msg systems ag, JUNE 26 - 2011 Markus Eisele 12
• Use capital letters
Classes always start with capital letters
E.g. CellPhone NOT cellPhone
• Use singular nouns
A class is a generalization of a single object.
E.g. Person NOT Persons
• Use the right level of generality
This highly depends on souroundings. Be as specific as
needed.
E.g. Cat not Animal
• Make sure the name has only one meaning
Name your Classes
msg systems ag, JUNE 26 - 2011 Markus Eisele 13
• Attributes and methods have visibility modifier
private
Visible to all class internal methods and attributes. private List<Person> attendees;
protected
Visible to all the classes within the same package and to sub-classes. protected List<Person> getAttendees();
public
Visible outside the class. Public interface to access and manipulate
objects public Date fromDate;
No modifier / package-private
Visible outside the class. Public interface to access and manipulate
objects
• Class attributes and methods are marked with the keyword
„static“. Can be combined with any of the above visibility modifier.
protected static int MAX_ATTENDEES;
Encapsulation and Class attributes and methods
msg systems ag, JUNE 26 - 2011 Markus Eisele 14
• Classes can be organized into a hierarchical inheritance structure, where a child class inherits attributes from a parent class. Also known synonymously as an inheritance tree.
• A child class is synonymous with the term subclass or derived class. The parent class is synonymous with the terms superclass or base class.
• The inheritance tree may run many levels deep.
• In a pure Object-Oriented system, a class may support multiple inheritance.
public class Student extends Person {
}
• Always check generalizations to ensure they obey the “is a” rule:
“A Student is a Person”
“A village is a municipality”
Inheritance
msg systems ag, JUNE 26 - 2011 Markus Eisele 15
• An operation should be declared to exist at the highest class in the hierarchy where it makes sense
The operation may be abstract (lacking implementation) at that level
If so, the class also must be abstract No instances can be created
The opposite of an abstract class is a concrete class
• If a superclass has an abstract operation then its subclasses at some level must have a concrete method for the operation
Leaf classes must have or inherit concrete methods for all operations
Leaf classes must be concrete
public abstract class Person {
public abstract String drinkingHabits();
}
public class Student extends Person {
public String drinkingHabits() {
return "Lot of beer!";
}
}
Abstract classes and methods
msg systems ag, JUNE 26 - 2011 Markus Eisele 16
• One object calls another through a message,
specifying a called method. If that method is found
within the receiver class, the method is invoked. If
the method is not found the receiver class‟s parent
class equivalent method is invoked, and so on. This
behavior is called polymorphism
• Polymorphism extends not only to the methods but
also the attributes of an object.
Polymorphism
public class Person {
private String name;
private String surename;
public Person(String name, String surename) {
this.name = name;
this.surename = surename;
}
}
public class Student extends Person {
public Student(String name, String surename) {
super(name,surename);
}
}
msg systems ag, JUNE 26 - 2011 Markus Eisele 17
• Like abstract classes, but cannot have executable statements
Define a set of operations that make sense in several classes
Abstract Data Types
• A class can implement any number of interfaces
It must have concrete methods for the operations
public interface Mammal {
public abstract List<Mammal> getChildren();
}
public abstract class Person implements Mammal {
public List<Mammal> getChildren() {
return Collections.emptyList();
}
}
Interfaces
msg systems ag, JUNE 26 - 2011 Markus Eisele 18
• Anything that can go wrong should result in the
raising of an Exception. Exception is a class with
many subclasses for specific things that can go
wrong.
• Use a try - catch block to trap an exception
try
{
int x = 1/0;
}
catch (ArithmeticException e)
{
// code to handle division by zero
}
Exceptions
msg systems ag, JUNE 26 - 2011 Markus Eisele 19
• J2SE 5.0 and higher provides compile-time type safety with the Java Collections framework through generics
• Generics allows you to specify, at compile-time, the types of objects you want to store in a Collection. Then when you add and get items from the list, the list already knows what types of objects are supposed to be acted on.
• So you don't need to cast anything. The "<>" characters are used to designate what type is to be stored. If the wrong type of data is provided, a compile-time exception is thrown.
Sample<String> object = new Sample<String>();
public class Sample<T> {
private T data;
public void setData(T newData) {
data = newData;
}
public T getData() {
return data;
}
}
Generics
msg systems ag, JUNE 26 - 2011 Markus Eisele 20
• The recurring aspects of designs are called design
patterns.
A pattern is the outline of a reusable solution to a
general problem encountered in a particular context
Many of them have been systematically
documented for all software developers to use
A good pattern should
Be as general as possible
Contain a solution that has been proven to
effectively solve the problem in the indicated
context.
Studying patterns is an effective way to learn from the
experience of others
Design Pattern
msg systems ag, JUNE 26 - 2011 Markus Eisele, Oracle ACE Director FMW & SOA 21
Context: • The general situation in which the pattern applies
Problem: A short sentence or two raising the main difficulty.
Forces: • The issues or concerns to consider when solving
the problem Solution:
• The recommended way to solve the problem in the given context.
—„to balance the forces‟
Antipatterns: (Optional) • Solutions that are inferior or do not work in this
context. Related patterns: (Optional)
• Patterns that are similar to this pattern. References:
• Who developed or inspired the pattern.
Description of Pattern
msg systems ag, JUNE 26 - 2011 Markus Eisele, Oracle ACE Director FMW & SOA 22
Example Singleton Design pattern
• Singletons can be used to create for example Connection Pool. If
programmers create a new connection object in every class that
requires it, then its clear waste of resources. In this scenario by using
a singleton connection class we can maintain a single connection
object which can be used throughout the application.
msg systems ag, JUNE 26 - 2011 Markus Eisele, Oracle ACE Director FMW & SOA 23
Gang of Four (GoF) Pattern
Design Patterns, Elements of Reusable
Object-Oriented Software - Erich
Gamma, Richard Helm, Ralph Johnson,
John M.
ISBN: 978-0-2016-3361-0
http://bit.ly/iojEpw
msg systems ag, 26/06/11 Markus Eisele, Oracle ACE Director FMW & SOA 24
Lesson in a tweet
“Good programmers use their brains,
but good guidelines save us having
to think out every case.” (Francis Glassborow)
http://www.devtopics.com/101-great-computer-programming-quotes/
msg systems ag, 26/06/11 Markus Eisele, Oracle ACE Director FMW & SOA 25
Thanks!
http://www.sagecomputing.com.au/
www.msg-systems.com
Thank you for your attention
msg systems ag, JUNE 26 - 2011 Markus Eisele 26
Markus Eisele
Principle IT Architect
http://blog.eisele.net
http://twitter.com/myfear
www.msg-systems.com