1.
Interface (WBUT 2013)
An interface is a collection of abstract
methods. A class implements an interface, thereby inheriting the abstract
methods of the interface.
An interface is not a class. Writing an
interface is similar to writing a class, but they are two different concepts. A
class describes the attributes and behaviors of an object. An interface
contains behaviors that a class implements.
Unless the class that implements the interface
is abstract, all the methods of the interface need to be defined in the class.
·
An interface can
contain any number of methods.
·
An interface is
written in a file with a .java extension, with the name of the
interface matching the name of the file.
·
The bytecode of
an interface appears in a .class file.
·
Interfaces
appear in packages, and their corresponding bytecode file must be in a
directory structure that matches the package name.
However, an interface is different from a
class in several ways, including:
·
You cannot
instantiate an interface.
·
An interface
does not contain any constructors.
·
All of the
methods in an interface are abstract.
·
An interface
cannot contain instance fields. The only fields that can appear in an interface
must be declared both static and final.
·
An interface is
not extended by a class; it is implemented by a class.
·
An interface can
extend multiple interfaces.
Declaring Interfaces:
The interface keyword is used to declare an
interface. Here is a simple example to declare an interface:
NB: ex. Already given in previous
program
2.
Applet viewer (WBUT
2013)
AppletViewer is a standalone command-line program from Sun to run Java applets. Appletviewer is generally used by
developers for testing their applets before deploying them to a website.
As a Java developer, it is a preferred option
for running Java applets that do not involve the use of a web browser. Even
though the applet viewer logically takes the place of a web browser, it
functions very differently from a web browser. The applet viewer operates on HTMLdocuments,
but all it looks for is embedded applet tags; any other HTML code in the
document is ignored. Each time the applet viewer encounters an applet tag in an
HTML document, it launches a separate applet viewer window containing the
respective applet. The only drawback to using the applet viewer is that it will
not show how an applet will run within the confines of a real web setting.
Because the applet viewer ignores all HTML codes except applet tags, it does
not even attempt to display any other information contained in the HTML
document.
Appletviewer is included with Sun's JDK package, but not with Sun's JRE package. The Java development kit GCJ also has its own version of appletviewer, called
"gappletviewer".
3.
JVM
(WBUT 2013)
A Java virtual machine (JVM), an implementation
of the Java Virtual Machine Specification, interprets compiled Java binary code (called bytecode) for a computer's processor (or "hardware platform") so that it
can perform a Java program's instructions. Java was designed to allow application
programs to be built that could be run on any platform without having to be rewritten or recompiled
by the programmer for each separate platform. A Java virtual machine makes this
possible because it is aware of the specific instruction lengths and other
particularities of the platform.
The Java Virtual Machine Specification
defines an abstract -- rather than a real -- machine or processor. The
Specification specifies an instruction set, a set of registers, a stack, a "garbage heap," and a method area. Once a Java virtual machine has been
implemented for a given platform, any Java program (which, after compilation,
is called bytecode) can run on that platform. A Java virtual machine can either
interpret the bytecode one instruction at a time (mapping it to a real processor
instruction) or the bytecode can be compiled further for the real processor
using what is called a just-in-time
compiler.
4.
What is similarity/difference between an
abstract class and interface? (WBUT 2012)
· abstract class is a special kind of class that cannot be instantiated. So the
question is why we need a classthat
cannot be instantiated? An abstract class is only to be sub-classed
(inherited from). In other words, it only allows other classes to inherit from it but
cannot be instantiated. The advantage is that it enforces certain hierarchies
for all the subclasses. In simple words, it is a kind of contract that
forces all the subclasses to carry on the same hierarchies or standards.
· interface is not a class. It
is an entity that is defined by the word Interface.
An interface has no implementation; it only has the
signature or in other words, just the definition of the methods without the
body. As one of the similarities to Abstract class, it is a contract that is
used to define hierarchies for all subclasses or it defines specific set
of methods and their arguments. The main difference between them is that a class can implement more than oneinterface but can only inherit from one abstractclass.
· When we create an interface,
we are basically creating a set of methods without any implementation that must
be overridden by the implemented classes.
The advantage is that it provides a way for a class to be a part of two classes: one from inheritance
hierarchy and one from the interface.
· When we create an abstract class, we are creating a base class that might have one or more completed
methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of
an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then
allow the programmers to fill the implementation in the derivedclasses.
· There are some similarities and differences
between an interface and an abstract class that I have arranged in a table for
easier comparison:
Feature
|
Interface
|
Abstract class
|
Multiple inheritance
|
A class may inherit several interfaces.
|
A class may inherit only one abstractclass.
|
Default implementation
|
An interface cannot provide any code, just the
signature.
|
An abstract class can provide complete, default code
and/or just the details that have to be overridden.
|
Access Modfiers
|
An interface cannot have access modifiers for the
subs, functions, properties etc everything is assumed as public
|
An abstract class can contain access modifiers for the
subs, functions, properties
|
Core VS Peripheral
|
Interfaces are
used to define the peripheral abilities of a class. In other words both
Human and Vehicle can inherit from a IMovable interface.
|
An abstract classdefines the core
identity of a class and there it is used for objects of
the same type.
|
Homogeneity
|
If various implementations only share method signatures then it is
better to useInterfaces.
|
If various implementations are of the same kind and use common
behaviour or status then abstractclass is better to use.
|
Speed
|
Requires more time to find the actual method in the correspondingclasses.
|
Fast
|
Adding functionality (Versioning)
|
If we add a new method to anInterface then we have to track down all the
implementations of the interface and define implementation for the
new method.
|
If we add a new method to an abstract classthen we have the option
of providing default implementation and therefore all the existing code might
work properly.
|
Fields and Constants
|
No fields can be defined ininterfaces
|
An abstract class can have fields and constrants
defined
|
No comments:
Post a Comment