Title: CSE 501N Fall 06 01: OO Concepts Java Basics
1CSE 501NFall 0601 OO Concepts Java Basics
2Lecture Outline
- From idea to software
- Compiled Languages
- Interpreted Languages
- Evolution of programming languages
- Object-oriented programming concepts
- Java Basics
3From Ideas to SoftwareCompiled Languages (C,
C, VB, etc.)
- Code is translated directly to machine executable
binary - Each binary is specific to the type of platform
(machine and OS) - E.g., Mac, PC, Linux
- Fast execution
- No cross-platform compatibility
4From Ideas to SoftwareInterpreted Languages
(BASIC, Java, etc.)
- Code is translated to an interim format
- An interpreter translates between the interim
format and machine code - Slow execution due to translation
- Cross-platform compatibility
- Still require platform specific interpreter
5Evolution of Programming Languages
- Programming in binary
- 1011101010101
- 01000101010101
- 01010100101001
- Low level languages
- Assembly
- MOV AX 2
- MOV BX 3
- ADD AX BX
- MOV AX CX
- High level languages
- Functional languages, e.g., C
- add(int a, int b)
- a ab
-
- Object oriented languages, e.g., Java, C
compiles into
6Object-oriented ProgrammingEntities
- Class
- Blueprint for certain type of object, e.g., the
class House - Defines an internal state
- Defines methods to change its internal state
- Object
- A specific instantiation of a class definition,
e.g., Bobs House - The unit of programming in OO languages
- Multiple objects can be created from a single
class definition Bobs House, Alices House,
etc. - Method
- A means of defining the behavior of the object
- May change the state of the object
- Accessors / Mutators / Subroutines
- Attributes
- Define the state of the objext
Bobs House
people_inside
enter
exit
7Object-oriented ProgrammingDifferentiating
between Classes and Objects
- It is extremely important to be able to
distinguish between classes and objects - A class defines the basic features of a certain
type of entity - Informally stated, a valid class definition for a
house requires that anything claiming to be a
house - Must have a number of doors
- Must have a number of windows
- Must have some bedrooms
- Must have some bathrooms
- An object is a specific example of a certain type
of entity - Bobs House and Alices House are instances of
the class House - Notice that they obeys the definition of the
general class House - In addition, they also specify the attributes
that are particular to each house -
Bobs House
2 doors
10 windows
3 bedrm
2 baths
House Class Definition
Alices House
5 doors
7 windows
4 bedrm
1 baths
8Object-oriented ProgrammingMethods and Attributes
House Class Definition
Bobs House
2 doors
10 windows
3 bedrm
2 baths
- Attributes
- numDoors
- numWindows
- numBedrooms
- numBathrooms
- Methods
- addWindow()
- removeWindow()
- Attributes
- numDoors 2
- numWindows 10
- numBedrooms 3
- numBathrooms 2
- Methods
- addWindow()
- removeWindow()
9Object-oriented ProgrammingCore Concepts -
Abstraction
- The act of representing essential features
without including the background details or
explanations - Classes use the concept of abstraction to hide
implementation details - For example, the method addWindow() of the class
House adds a window to the instance of the house - However, it does not specify exactly how it does
this - Abstraction is a very useful concept because it
allows programmers to write code - At a higher level
- Without the need to understand implementation
details - And promotes reuse of code
10Object-oriented ProgrammingCore Concepts -
Encapsulation
- Protect attributes by preventing open access to
them - Allow access via controlled methods
- Accessors and mutators
numWindows 30
Bobs House
addWindow
numWindows
removeWindow
11Object-oriented ProgrammingCore Concepts -
Inheritance
- Use a basic class definition to derive another
more complex class definition - Create class hierarchies
- The superclass is the most generic class
definition - The subclass is a more specific class definition
- Though called a subclass, capabilities of the
subclass are greater than that of the superclass
Structure
Superclass
is-a
is-a
Garage
House
Subclass
12Object-oriented ProgrammingCore Concepts -
Polymorphism
- The ability to use the same code, yet process
differently based on the class of an object - Utilized in conjunction with inheritance to write
general and abstract programming solutions - Supported by dynamic binding
13Java Programming Basics
- In the Java programming language
- A program is made up of one or more classes
- A class contains one or more methods
- A method contains program statements
14Java Programming BasicsProgram Structure
// comments about the class
public class QuoteGenerator
class header
class body
Comments can be placed almost anywhere
15Java Programming BasicsProgram Structure
// comments about the class
public class QuoteGenerator
// comments about the method
public static void main (String args)
method header
method body
16Java Programming BasicsProgram Structure
// comments about the class
public class QuoteGenerator
// comments about the method
public static void main (String args)
System.out.println(Ask not)
statement
statement terminator
17Java Programming BasicsComments
// this comment runs to the end of the line
/ this comment runs to the terminating
symbol, even across line breaks /
/ this is a javadoc comment /
18Java Programming BasicsIdentifiers
- An identifier can be made up of letters, digits,
the underscore character ( _ ), and the dollar
sign - Identifiers cannot begin with a digit
- Java is case sensitive
19Java Programming BasicsIdentifiers
- Identifiers may be chosen by the programmer
- Identifiers are used by the programmer
- Special identifiers called reserved words have a
predefined meaning in the language
20Java Programming BasicsReserved Words
abstract boolean break byte case catch char class
const continue default do double
else enum extends false final finally float for go
to if implements import instanceof
int interface long native new null package private
protected public return short static
strictfp super switch synchronized this throw thro
ws transient true try void volatile while
21Java Programming BasicsWhite Space
- Spaces, blank lines, and tabs are called white
space - White space is used to separate words and symbols
in a program - Extra white space is ignored
- White space is FREE. Use it liberally to make
your code legible
22Java Programming BasicsErrors
- A program can have three types of errors
- The compiler will find syntax errors and other
basic problems (compile-time errors) - A problem can occur during program execution,
which causes a program to terminate abnormally
(run-time errors) - A program may run, but produce incorrect results
(logical errors)
23The Java Style GuideGood Habits to Adopt Early
- It is very important that others can read and
understand your code - In this class
- In the real world
- The best way to ensure this is to write clean,
well organized code - The style guide provides guidelines that have
been adopted as standard by the Java community - You will be expected to adhere to the style guide
for all your lab assignments
24The Java Style GuideGood Habits to Adopt Early
- import java.util.
- public class TreasureChest extends Chest
implements RagsToRichesInterface - //public variables
- public int numOfGoldCoins 0
- //protected and private variables
- public TreasureChest(int goldCoins, int
silverCoins) - // Statements
-
- public int lootChest()
- //Statements
- return numOfGoldCoins
-
25The Java Style GuideGood Habits to Adopt Early
- public void aDummyMethod()
- if(xtrue) Wrong!
- //Statements
- else
- //Statements
-
-
- boolean y true
- while(y)
- //Statements
-
- for(i 0 i lt 10 i)
- //Statements
-
26Conclusion
- Questions
- Lab 0 is assigned
- Get it online at http//www.cs.wustl.edu/ras6/CSE
501/assignments.htm