The Java Language
The Java Language
com
Engaging Peers, Inspiring Careers!
• A java Application
• Packages
• Overloading Constructors
• Access modifiers
Classes and Objects
• In object-oriented programming, a class is a construct that is used as a
blueprint (or template) to create objects of that class.
• This blueprint describes the state and behavior that the objects of the class
all share.
• The class that contains (and was used to create) that instance can be
considered as the type of that object, e.g. an object instance of the "Fruit"
class would be of the type "Fruit".
Creating classes and Objects
Declaring Classes
class MyClass {
• The class body (the area between the braces) contains all the code that
provides for the life cycle of the objects created from the class:
• Constructors for initializing new objects, declarations for the fields that
provide the state of the class and its objects, and methods to implement the
behavior of the class and its objects.
Creating classes and Objects
In general, class declarations can include these components, in order:
• The Bicycle class uses the following lines of code to define its fields:
• The only required elements of a method declaration are the method's return
type, name, a pair of parentheses, (), and a body between braces, {}.
Creating classes and Objects
More generally, method declarations have six components, in order:
• The return type—the data type of the value returned by the method, or
void if the method does not return a value.
• The method name—the rules for field names apply to method names as
well, but the convention is a little different.
• An exception list.
• The first line creates an object of the Point class, and the second and third
lines each create an object of the Rectangle class.
Creating Objects
Each of these statements has three parts (discussed in detail below):
• Declaration
• Instantiation: The new keyword is a Java operator that creates the object.
• Objects are needed to use the value of one of its fields, change one of its
fields, or call one of its methods to perform an action.
Referencing an Object's Fields
• Object fields are accessed by their name.
• Code that is outside the object's class must use an object reference or
expression, followed by the dot (.) operator, followed by a simple field
name, as in:
objectReference.fieldName
Instance and Class Variables and Methods
• The variables and methods that are associated with a particular object are
called instance variables and methods.
• These methods and variables are called as class methods and variables,
because these belong only to class.
• Java packages can be stored in compressed files called JAR files, allowing
classes to download faster as a group rather than one at a time.
• You can refer to any member of the current object from within an instance
method or a constructor by using this.
• The most common reason for using the this keyword is because a field is
shadowed by a method or constructor parameter.
Using the this Keyword
• For example, the Point class was written like this
public int x = 0;
public int y = 0;
x = a;
y = b;
}
Using the this Keyword
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
• Each argument to the constructor shadows one of the object's fields —
inside the constructor x is a local copy of the constructor's first argument.
To refer to the Point field x, the constructor must use this.x.
Using this with a Constructor
• From within a constructor, you can also use the this keyword to call
constructor invocation.
• Example
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
Using this with a Constructor
• public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
Inheritance and Overriding Methods
• A class that is derived from another class is called a subclass (also a
derived class, extended class, or child class). The class from which the
subclass is derived is called a superclass (also a base class or a parent
class).
• A subclass inherits all the members (fields, methods, and nested classes.
• An instance method in a subclass with the same signature (name, plus the
number and the type of its parameters) and return type as an instance
method in the superclass overrides the superclass's method.
Abstract Methods and Classes
• 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.
// declare fields
• Interfaces are declared using the interface keyword, and may only contain
method signatures and constant declarations (variable declarations that are
declared to be both static and final).
• The class must implement all of the methods described in the interface, or be an
abstract class.
Interfaces and Implementation
interface IntExample{
javaInterfaceExample.sayHello();
}}
Access Modifiers
public - All Classes have access to public attributes and behavior.
default - Only classes within the package have access to default attributes
and behavior.
protected - Only classes within the same package or classes from different
packages have access to protected attributes and behavior.
private - Private attributes and behavior are only accessible within a single
class.
FaaDoOEngineers.com
Engaging Peers, Inspiring Careers!