Professional Documents
Culture Documents
Janaranjani K
Outline
History and design goals
Java Classes And Objects
Language Terminology
Naming Convention
Java Types
Visibility and Access
Constructors
Inheritance
Encapulation an Packages
Abstract Class and Methods
Final Class and Methods
Interface
Exception Handling
Garbage Collection
Origins of the language
Internet application
simple language for writing programs that can be
transmitted over network
Design Goals
Portability
Internet-wide distribution: PC, Unix, Mac
Reliability
Avoid program crashes and error messages
Safety
Programmer may be malicious
Simplicity and familiarity
Appeal to average programmer; less complex than
C++
Efficiency
Important but secondary
Java Classes and Objects
Classes
Modifiers such as public, private, and a number of
others that you will encounter later.
The class name, with the initial letter capitalized by
convention.
The name of the class's parent (superclass), if any,
preceded by the keyword extends. A class can only
extend (subclass) one parent.
A comma-separated list of interfaces implemented
by the class, if any, preceded by the keyword
implements. A class can implement more than one
interface.
The class body, surrounded by braces, {}.
Continued …
Eg:
1. class MyClass { //field, constructor, and method
declarations }
Name
Eg:
graphics.Rectangle myRect = new graphics.Rectangle();
Importing a Package Member in other class
Eg:
import graphics.Rectangle;
Importing an Entire Package
Eg:
import graphics.*;
Abstract Class and
Methods
Abstract Class
An abstract class is a class that is declared abstract—it
may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be
subclassed.
Abstract Method
An abstract method is a method that is declared without
an implementation (without braces, and followed by a
semicolon),
Eg:
abstract void moveTo(double deltaX, double
deltaY);
Restrict inheritance
Final classes and methods cannot be redefined
Example
java.lang.String
Note
Final class – Cannot be Sub classed
class ChessAlgorithm {
enum ChessPlayer { WHITE, BLACK }
...
// Define constructor
ColorPoint(int xval, Color cval) {
super(xval); // call Point constructor
c = cval; } // initialize ColorPoint field
}
}
Interfaces
Eg:
public Object findLargest(Object object1, Object object2)
{
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ( (obj1).isLargerThan(obj2) > 0)
return object1;
else
return object2;
}
Overriding and Hiding Methods
Instance Methods
Class Methods
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
Abstract Classes versus
Interfaces
Unlike interfaces, abstract classes can contain
fields that are not static and final, and they
can contain implemented methods.
Throwable
Exception Error
RuntimeException
checked
exceptio
ns