Академический Документы
Профессиональный Документы
Культура Документы
You can require that certain methods be overridden by subclasses by specifying the abstract type
modifier. To declare an abstract method, use this general form:
Any class that contains one or more abstract methods must also be declared abstract.
Here is an abstract class, followed by a class which implements its abstract method.
Question 1
abstract class MyAbstractClass {
abstract void callme();
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
Question 2
Shape(double a, double b) {
height = a;
width = b;
}
abstract double area();
}
}
}
Shape figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
Defining a Package
To define a package, include a package command as the first statement in a Java source file.
Any classes declared within that file will belong to the specified package.
If you omit the package statement, the class names are put into the default package, which has no
name.This is the general form of the package statement:
package packageName;
To create a hierarchy of packages, separate each package name from the one above it by use of a
period. The general form of a multileveled package statement:
package pkg1[.pkg2[.pkg3]];
A package hierarchy must be reflected in the file system of your Java development system.
Question 3
package MyPack;
Then try executing the class, using the following command line:
java MyPack.Main
Importing Packages
In a Java source file, import statements occur immediately following the package statement and
before class definitions.
import pkg1[.pkg2].(classname|*);
import java.lang.*;
Any place you use a class name, you can use its fully qualified name, which includes its full
package hierarchy.
import java.util.*;
class MyDate extends Date {
}
The same example without the import statement looks like this:
Source Files
All Java source files must end with the .java extension.
A source file should contain, at most, one top-level public class definition.
If a public class is present, the class name should match the unextended filename.
Three top-level elements known as compilation units may appear in a file. None of these
elements is required.
If they are present, then they must appear in the following order:
1. Package declaration
2. Import statements
3. Class, interface, and enum definitions
Question 4
package MyPack;
import java.util.Date;
Access Control
Java's access specifiers are public, private, protected and a default access level.
protected
public int i;
The following code defines a private member variable and a private member method:
private double j;
private int myMethod(int a, char b) { // ...
To understand the effects of public and private access, consider the following program:
Question 5
class Test {
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) {
c = i;
}
int getc() {
return c;
}
}
public class Main {
public static void main(String args[]) {
Test ob = new Test();
ob.a = 1;
ob.b = 2;
// This is not OK and will cause an error
// ob.c = 100; // Error!
// You must access c through its methods
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " +
ob.getc());
}
}
Question 6
class A {
private int j; // private to A
}
class B extends A {
int total;
void sum() {
total = j; // ERROR, j is not accessible here
}
}
No
Position Private Protected Public
modifier
Same class Yes Yes Yes Yes
Same package subclass No Yes Yes Yes
Same package non-subclass No Yes Yes Yes
Different package subclass No No Yes Yes
To implement an interface, a class must create the complete set of methods defined by the
interface.
Defining an Interface
An interface is defined much like a class. This is the general form of an interface:
// ...
return-type method-nameN(parameter-list);
All methods and variables are implicitly public if the interface, itself, is declared as public.
interface MyInterface{
void callback(int param);
}
Implementing Interfaces
To implement an interface, include the implements clause in a class definition, and then create
the methods defined by the interface.
The general form of a class that includes the implements clause looks like this:
access-level class classname [extends superclass] [implements interface
[,interface...]] {
// class-body
}
Here is a small example class that implements the interface shown earlier.
Question 7
interface MyInterface {
void callback(int param);
}
callback() is declared using the public access specifier. When you implement an interface
method, it must be declared as public.
Question 8
interface MyInterface {
void callback(int param);
}
class Client implements MyInterface{
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
}
public class Main {
public static void main(String args[]) {
MyInterface c = new Client();
c.callback(42);
}
}
Question 9
Polymorphic and interface
interface MyInterface {
void callback(int param);
}
class Client implements MyInterface{
// Implement Callback's interface
public void callback(int p) {
System.out.println("Client");
System.out.println("p squared is " + (p * 2));
}
}
class AnotherClient implements MyInterface{
// Implement Callback's interface
public void callback(int p) {
System.out.println("Another version of callback");
System.out.println("p squared is " + (p * p));
}
}
class TestIface2 {
public static void main(String args[]) {
MyInterface c = new Client();
AnotherClient ob = new AnotherClient();
c.callback(42);
c = ob; // c now refers to AnotherClient object
c.callback(42);
}
}
Question 10
interface MyInterface {
void callback(int param);
void show();
}
abstract class Incomplete implements MyInterface {
int a, b;
Variables in Interfaces
We can use interface to organize constants.
Question 11
interface MyConstants {
int NO = 0;
int YES = 1;
}
Question 12
interface IntefaceA {
void meth1();
void meth2();
}
interface IntefaceB extends IntefaceA {
void meth3();
}
class MyClass implements IntefaceB {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
public class Main {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
class T {
transient int a; // will not persist
int b; // will persist
}
The volatile variables can be changed unexpectedly by other parts of your program.
Using instanceof
Java provides the run-time operator instanceof to check class type for an object. The instanceof
operator has this general form:
class A {
}
class B {
}
class C extends A {
}
class D extends A {
}
if (a instanceof A)
System.out.println("a is instance of A");
if (b instanceof B)
System.out.println("b is instance of B");
if (c instanceof C)
System.out.println("c is instance of C");
if (c instanceof A)
System.out.println("c can be cast to A");
if (a instanceof C)
System.out.println("a can be cast to C");
A ob;
ob = d; // A reference to d
System.out.println("ob now refers to d");
if (ob instanceof D)
System.out.println("ob is instance of D");
ob = c; // A reference to c
System.out.println("ob now refers to c");
if (ob instanceof D)
System.out.println("ob can be cast to D");
else
System.out.println("ob cannot be cast to D");
if (ob instanceof A)
System.out.println("ob can be cast to A");
// all objects can be cast to Object
if (a instanceof Object)
System.out.println("a may be cast to Object");
if (b instanceof Object)
System.out.println("b may be cast to Object");
if (c instanceof Object)
System.out.println("c may be cast to Object");
if (d instanceof Object)
System.out.println("d may be cast to Object");
}
}
Inner Classes
An inner class is declared inside (between the opening and closing curly brac
es).
class MyClass{
class MyInnerClass{
}
}
A normal Java class can have another class declared inside it: a member of th
e normal class.
Member classes cannot have the same name as the enclosing class.
If the class is declared static, it is called a nested class and can access o
nly static methods and variables.
For a local inner class (inside a method) to use local variables or method pa
rameters,
it must be declared final.
Instances of the inner class retain the ability to access the members of the outer class
Question 14
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
public class Main {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
Question 15
class Outer {
int outer_x = 100;
void test() {
}
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
public class Main {
public static void main(String args[]) {
new Outer().new Inner().display();
}
}
Question 16
class OuterOne {
private int x = 0;
public OuterOne() {
InnerOne inner = new InnerOne();
outerMethod();
}
Question 17
Question 18
Question 19
Question 20
Question 21