2.16k likes | 2.3k Views
Object Oriented Development. Java. Objectives. Develop a basic understanding of: OO Concepts Networking Web servers Servlets (live reports from databases) Using an IDE to develop applications. Learn enough Java to enable you to continue learning on your own. Non-objectives.
E N D
Objectives Develop a basic understanding of: • OO Concepts • Networking • Web servers • Servlets (live reports from databases) • Using an IDE to develop applications. • Learn enough Java to enable you to continue learning on your own.
Non-objectives • Language theory. • Thorough tour of the Java APIApplication Programmer Interface • Discussion of inner-workings of Java. • GUI application development (Swing/AWT) • Why the designers of Java did what they did.
Prerequisites Classes assume basic knowledge of: • Algorithms • Networks • World Wide Web • Databases
Class Format • Lectures. • Review Questions. • Lab work.All lab work is limited to the illustrating the subject material presented. Lab work algorithms are simple. • Ask questions.
Other Methodologies • For example: • Procedural • Aspect Oriented Programming • Integration Oriented Programming • etc…
Introduction • First primitive Object Oriented Programming (OOP) language, Simula, released in 1967 • Some think it simplifies coding as opposed to Procedural, or Aspect Oriented Programming. • Coded from the point of view of items in a problem rather than a set of procedures. • Office Example.
Object Oriented Programming • A PIE • Abstraction • Polymorphism • Inheritance • Encapsulation
Abstraction • Representing the essential parts of a real-world thing as data. • What’s needed for your solution. • Some abstractions of a Car • VIN, Color, Make, Model, Year (Manufacturer) • Transponder ID, License Plate Number, Account Number (Fast Lane)
Encapsulation • Binding data together with its functionality (methods) • Department.addEmployee() • Car.drive() • Door.open() • File.open()
Inheritance • Building on an existing concept by adding new properties or functionality. • Fruit • grams • calories per gram • Citrus is a Fruit • can be squeezed into juice • “Is A” implies inheritance
Polymorphism • Scary name; simple concept.Literally: “Many shapes” • Overloading • Traditionally we havesquare(int), squareF(float)… • In OO we have square(int), square(float) … • Overriding • Same function as parent class being respecified in child class.
Example • Sports Car inherits from car • Drive method is overriden as well as overloaded. • Car and SportsCar abstract the “concept” of a car. • Car and SportsCar encapsulate the functionality of a car. Car int speed drive() drive(int speed) SportsCar drive() overdrive()
Classes and Objects • A Class is a collection of properties (also called fields or member variables) and methods that define a new data type • An Object is an instance of a class. • Example • If Person is a class • Bob, Jim, and Mary are all objects (instances of the Person Class)
JAVA Object Based from the Start
What is Java? • The Java Language • Java Virtual Machine • Java API
The Java Language • Developed at Sun Microsystems in 1991, released to the public in 1994 • Based upon C++, but it was made to be accessible to more programmers. • Object Oriented. Everything is a class (i.e no stand-alone functions) • Delivers secure, portable, multi-threaded, networking related applications • Case-sensitive
Java Virtual Machine • Referred to as the Sandbox or JVM • Java code compiles into bytecode, unlike most other languages that compile to machine code. • Since bytecode is readable to the JVM, Java is portable to any operating system that has a Java Virtual Machine built for it.
Java APIApplication Programmer Interface • A collection of software libraries, called packages, that contain Java classes • As testimony to the reusability of Java code, you use the Java API to create functionality. • Use the import keyword to access a software library • Example:import java.io.*;
Programming Java Applications • A Java application is a collection of classes with one class that contains a main method as an entry point. • Most classes must reside in a file with the same namee.g. A public class called Learning must reside in a file called Learning.java • Class declarations and control flow structures are contained in {} • Variable declarations, expressions, functions and library calls end with semi-colons
Main Method Method name public static void main(String[] args){ } Classmethod No return value Accessible to any class An array of String objects
Comments • Java uses C/C++ commenting styles. • Single line comments are denoted by //int a; // a will be used to count sheep • Block comments begin with /* and end with *//* Author: Joe Smith Date: Jan 7th, 2002 Description: First lines of code */
Declaration of class Entrance point for application public class HelloWorld { public static void main( String[] args) { System.out.println(“Hello World”); } } Method of out object Static object Object contained in System
Compiling and Running Java Applications • To transform files into bytecode, use the javac (abbreviated java compile) command • javac [filename] • java [classname]
Review Questions • What is the purpose of the main method? • What is the difference between a Class and an Object? • Given a cookie cutter and cookies, which would best represent an object, and which best represents a class? • What two ways are comments marked in a Java program? • How do you include other libraries so that your class can use them?
Lab 1 Hola.java public class Hola { public static void main( String args[]) { System.out.println(“Hello World”); } }
Borland’s JBuilder • An Integrated Development Environment (IDE) • Project • A collection of source files that solve a problem.
To Run a ProjectWe have to tell Jbuilder which class has the main method. • Click on the • Click on • Select HelloWorld
Lab 2 Hello World in Jbuilder public class HelloWorld { public Helloword() { } public static void main( String args[]) { System.out.println(“Hello World”); } }
Variables and Operators • Primitive Data-types • Classes and Objects • Operators
Primitive Data-types • A data-type that does not contain any other data-types • List of Primitive Data-types • boolean byte • int long • float double • char short • JVM ensures these types are identical on all platforms
Defining Variables • Format: • datatype variableName; • Examples: int a;boolean done;float salary;
Using Identifiers of Variables • Identifiers are the names of the variables that a programmers assigns. • Identifiers can consist of alphanumeric symbols, underscores and dollar signs, but cannot be reserved operator symbols. • Identifiers may not begin with numeric symbols. • Identifiers are case sensitive.
Variable Names • Examples of Valid Identifiers $joe J3_the_robot _this4$ i • Style: stusCaremployeeJohn myRedRobot vPawns
Classes and Objects • Class • A collection of properties (also called fields or member variables) and methods that define a new data type like metadata (like the def. of a db table) • Object • A particular instance of a class.like data (the rows in a database table)
Class • A class is a description of what an object will look like and how it will behave. • Classes are like an idea. You cannot use them until you make an object out of the class.
Reference Variables & Objects • Reference Variables point to objects datatype refVar • When they are first defined, they point nowhere. Pawn stusPawn;Pawn jeffsPawn;
new • new reserves memory for an object of a specified class and returns a reference to it. ClassName myObj;myObj = new Constructor();Pawn p;p = new Pawn();
Constructing Objects • Because objects are complex datatypes, (classes) they need to be “constructed”. • Format: refVar = new[constructor()] ; Pawn stusPawn;Pawn jeffsPawn;stusPawn = new Pawn();
Construction Continued Pawn stusPawn; Pawn jeffsPawn; stusPawn = new Pawn(); stusPawn = new Pawn(); stusPawn = new Pawn();
Garbage Collection • Garbage Collection automatically reclaims an object when its reference count is zero • Objects have a reference Count
Review Questions • Is int a class or a primitive datatype? • When you declare a reference variable, what does it point to? Pawn p; • What does the Garbage Collector do?
Defining a class Format: [visibility] class ClassName{ [properties] the order [constructor methods] does not [methods] matter }