Professional Documents
Culture Documents
JAVA
“Object Orientation involving encapsulation,
inheritance, polymorphism, and abstraction, is an
important approach in programming and program
design. It is widely accepted and used in industry and
is growing in popularity in the first and second
college-level programming courses.”
ClassName
- attribute_declaration
+ constructor_declaration
+ method_declaration
Visibility shown as :
+ public
− private
# protected
The Solution
1. public class MyDate {
2. private int day;
3. private int month; MyDate
4. private int year;
• Platform-independent software
• Relatively easy graphics and
GUI programming
• Lots of library packages
• Free compiler and IDEs
Java Programming Styles
(1)
• Packages:
– Package names are entirely in lower case.
– Package name should start with the web domain
name reversed
– Examples:
package com.sun.java.lang;
package edu.nmsu.is.us.sp;
• Files:
– The file name must have the same base name as the
name of the public class defined in the file.
– Example:
– If you have a public class named “RecordList”, the file
containing this class should be named RecordList.java
Java Programming Styles
(2)
• Classes and Interfaces:
– Use meaningful identifiers for classes ,
and interfaces.
– Capitalize each word contained in a
class identifier name.
– No underscores.
– Examples:
public class RecordList {…}
public interface PanelFace {…}
Java Programming Styles
(3)
• Variables:
– Use meaningful identifiers for variables.
– Capitalize each word contained in a name of a
variable except the first word.
– Use nouns to identify variables as possible.
– For boolean variables, use identifirs that are like
questions.
– Use all-caps indentifiers for constants.
– Examples:
int number;
String myName;
boolean isValid;
final int CODE = 707;
Java Programming Styles
(4)
• Methods:
– Use meaningful identifiers for methods.
– Capitalize each word contained in a name of a
method except the first word.
– Use verbs to identify methods as possible.
– For the methods dealing with objects’ properties,
start the method identifier with get or set.
– If the method returns boolean use “is” or “are”
instead of get to name this method.
– Examples:
private boolean paint()…
boolean isObjectValid()…
Font getFont()…
void setFont(Font f)…
Java Programming Styles
(5)
• General Considerations:
– Use three-space indentation style. Example
if(num < 10)
{
System.out.println(“Not Enough”);
}
– Use comments to mark the beginning and the end of blocks
– Use three-line style to comment your code. Use either one of:
// /*
// This part is to … or * This part is to …
// */
– Use empty lines to increase the readability of your code
– Use spaces between the operators such as +, -, =, … and the
operands. Example:
c = a + b;
Some other reasons to move on
to Java:
• Colleges are teaching it
• Companies are using it
• Students want it
• (Teachers welcome it... ;)
• (Programmers drink it... :)
What are OOP’s claims to fame?
• Better suited for team development
• Facilitates utilizing and creating reusable
software components
• Easier GUI programming
• Easier program maintenance
OOP in a Nutshell:
• A program models a • Each object belongs to
world of interacting a class; a class defines
objects properties of its objects
• Objects create other • A class implements an
objects and “send ADT; the data type of
messages” to each an object is its class
other (in Java, call • Programmers write
each other’s classes (and reuse
methods) existing classes)
OOP
Abstraction
Abstraction means ignoring irrelevant
features, properties, or functions and
emphasizing the relevant ones...
“Relevant” to what?
// Constructor:
protected Foot(String side, int x, int y, int dir)
{
amLeft = side.equals("left");
myX = x;
myY = y;
myDir = dir;
myWeight = true;
}
Continued È
Encapsulation ensures that
structural changes remain local
• Changes in the code create software
maintenance problems
• Usually, the structure of a class (as defined
by its fields) changes more often than the
class’s constructors and methods
• Encapsulation ensures that when fields
change, no changes are needed in other
classes (a principle known as “locality”)
Inheritance
A class can extend another class,
inheriting all its data members and
methods while redefining some of them
and/or adding its own.
Inheritance represents the is a
relationship between data types. For
example: a FemaleDancer is a
Dancer.
Inheritance Terminology:
subclass superclass
or extends or
derived class base class
Situation 1:
A collection (array, list, etc.) contains
objects of different but related types, all
derived from the same common base
class.