4.Classes, Interfaces and Packages

Download Report

Transcript 4.Classes, Interfaces and Packages

Classes, Interfaces and Packages
1
Objectives
After completing this chapter, the student will be able to:
•
•
•
•
•
•
Explain Java classes
Explain Java objects
Declare and define simple Java classes
Create objects of Java classes and do
manipulation on those objects by invoking class
methods
Explain class constructors and finalizers
Explain access modifiers and the difference
between different access modifiers
2
Objectives contd..
•
•
•
•
•
•
•
•
•
Explain class inheritance
Explain the difference between method
overloading and method overriding
Explain abstract classes, interfaces and
interface implementation
Explain Java packages
Explain CLASSPATH environment variable and
set it to suit his/her requirement
Write programs to create Java packages
Explain the usage of import and package
statements
Write Java programs using previously created
packages
Explain the important packages in Java API
3
Introduction
•
Java is an object oriented programming language.
•
Object oriented programming revolves around the
concept of objects.
•
Objects are software bundles of data abstractions
and procedures that act on the data.
•
These procedures are also known as methods.
•
Two powerful benefits of encapsulation
implementation hiding and modularity.
are
4
Classes
•
A class is a template or a prototype that defines a
type of object.
•
A class is to an object what a blue print is to a house.
•
A class outlines the properties of an object.
•
Objects created from the same class show similar
characteristics.
5
Declaring Classes
•
The syntax for declaring classes in Java is as
follows:
class identifier
{
classBody
}
•
ClassBody consists of data declarations and method
definitions.
•
Curly braces surround the classBody.
6
The new Operator
•
Here is how you create instances of classes or object
variables and invoke methods or member functions
in Java.
ExampleClass ec;
ec = new ExampleClass();
ec.exampleMethod1();
ec.exampleMethod2();
•
•
The declaration “ExampleClass ec:” simply states
what type of object variable ec will be.
The object is actually created when the new operator
is called.
7
Constructors
•
Constructors are special methods you can implement
in your classes.
•
Constructors are always given the same name as
that of the class.
•
It do not have return type.
•
Used to initialize the member variables.
8
Finalize Method
•
Finalizer methods are automatically called when an
object is destroyed.
•
Java objects
collection.
•
Finalizers are thus called when Java performs
garbage collection.
void finalize() {
// do clean u job
}
are
destroyed
during
garbage
9
Java Objects as Class Member Variables
•
Java class member variables are of two types.
•
One is the built in type like char, int, float, double, etc.
•
Class member variables can be Java objects as well.
Ex:
String msg;
10
Method Overloading
•
If two or more methods of a class have the same name but
different signatures, then the method name is said to be
overloaded.
•
The signature of a method consists of the name of the
method and the number and types of formal parameters in
particular order.
•
Only the method name is reused in overloading, so
method overloading is actually method name overloading.
•
Overloaded methods may have arguments with different
types and order of the arguments may be different.
11
Method Overloading
contd..
•
Overloaded methods are not required to have the
same return type or the list of thrown exceptions.
•
Overloading is used while implementing several
methods that implement similar behavior but for
different data types.
•
Overloaded methods are independent methods of a
class and can call each other just like any other
method.
12
Constructor Overloading
•
Constructors can also be overloaded as they are also
methods.
•
Java has some classes with overloaded constructors.
Ex:
Integer i =new Integer(3);
Integer j =new Integer(“2012”);
•
One constructor can call another overloaded
constructor of the same class by using this keyword.
•
One constructor can call constructor of it’s super
class by using the super keyword.
13
Deriving Classes
•
A Class can be built on another class that is already
defined and is existing. This already existing class is called
the base class or the parent class. The newly built class is
called the derived class or child class. extends is the
keyword used for inheritance.
•
The Child class inherits all the properties of the parent
class.
•
The child class inherits all the member variables and
methods of the parent class.
•
The child class can have its own member variables and
methods.
14
Method Overriding
•
When a class defines a method with same method
name ,argument types, argument order and return
type as a method in its super class, it’s called method
overriding.
•
The method in the super class is said to be
overridden by the method in the subclass.
•
Overriding method actually replaces the behavior in
super class for a subclass.
•
The call super.method() will invoke the method of
immediate super class.
15
Rules for Method Overriding
•
Methods overriding cannot be declared more private than
the super class method.
•
Any exceptions declared in overriding method must be of
the same type as those thrown by the super class or a
subclass of that type.
•
Methods declared as final cannot be overridden.
•
An overriding method can be declared as final as the
keyword final only suggests that this method cannot be
further overridden.
•
Methods declared as private cannot be overridden.
16
Access Modifiers
•
Access to member variables and methods in a Java
class is accomplished through access modifiers.
•
Access modifiers define the varying levels of access
between class members and outside world.
•
Access modifiers are declared immediately before
the data type of a member variable or the return type
of a method.
•
There are four access modifiers:
protected and public.
default, private,
17
Access Modifiers
•Access modifiers on class member declarations are listed
here.
Modifier
Same
Class
Same
Package
Subclass
private
Yes
default
Yes
Yes
protected
Yes
Yes
Yes
public
Yes
Yes
Yes
Universe
Yes
18
The static Modifier
•
The static modifier specifies that a variable or a
method is the same for all objects of a particular
class.
•
static variables memory is allocated only once i.e.
the first time when we create the object.
•
static method can be accessed without having to
create an object of the class.
•
Static method has access to static variables only.
19
The final Modifier
•
The final modifier with variable specifies that the
variable has a constant value.
•
The final modifier with method specifies that the
method can not be overridden in a subclass or child
class.
•
The final modifier with class specifies that the class
can not be inherited.
20
The native Modifier
•
The native modifier informs the Java compiler that a
method’s implementation is in an external C file.
•
Syntax:
native void classMethod();
21
Abstract Classes and Methods
•
Abstract classes
implemented.
•
Abstract methods are methods that are declared, but not
implemented.
•
Abstract classes are used as base classes for deriving
subclasses.
•
In the child class, all the abstract methods of the base
class will be overridden.
•
Constructors, static methods and private methods cannot
be abstract.
are
classes
that
are
partially
22
Interfaces
•
•
An interface is a prototype for a class.
The syntax for creating interfaces is shown below:
interface identifier
{
interface Body
}
•
•
•
Here interface is a keyword.
identifier is the name of the interface.
interface body consists of static final variables and
abstract methods.
23
Naming Conflicts
•
A Java program consists of a collection of classes.
When a Java program is compiled, each class in it is
compiled into a byte code file with the class name as
file name and extension .class. This creates a potential
naming conflict.
•
You should not have a second Java program in your
working directory in which classes, with names the
same as that of the classes in your first program, are
appearing.
•
It is always safe to have unique class names in every
program in the same directory.
24
Points to note
•
Any method in a class can happily reference another
class or class method even if that class does not occur
immediately above.
•
You can not define a second class when you are
already in the middle of defining another class.
•
You cannot have class declaration in one file and class
method implementation in a different file.
25
Large Programs
•
•
•
•
•
Large program means a program consisting of more
than one file.
The Java programs we have seen so far consisted of
one single compilation unit or file.
The class you are compiling makes references to
other classes in other files.
Javac compiler will compile all these classes.
If a class is modified after the previous compilation,
Java compiler detects that and that class alone will
be compiled in the next run.
26
Packages and Imports
•
•
•
•
A package provides a means to group together
related classes and reference them as a whole.
To create a package you need to label each file in
which the classes are defined with the name of the
package.
This is done through a statement
package packname;
as the very first statement of the file concerned.
The import statement is used to import the classes in
the named package into the file concerned.
Syntax :
import packagename.*;
import packagename.Classname;
27
Classpath
•
•
•
You will find some relationship between the
CLASSPATH, package name and the directory path
in which the file is located.
The package name is the difference between the
directory path and CLASSPATH.
The package name bridges the gap in the directory
path name between the CLASSPATH and the file
name.
CLASSPATH + packagename = DirectoryPath
28
javac -d option
•
It is possible to segregate the .class files into
different directories with –d option in javac command.
Ex:
Example1.java is in the package mypack.pack1.
Compile as
d:\user\javac –d c:\cmc Example1.java
* c:\cmc directory should exist
* The directory c:\cmc\mypack\pack1 will be created if it
does not already exist.
* Example1.class will be found in
c:\cmc\mypack\pack1\Example1.class
29