Java Basics: Csci210: Data Structures Spring 2009
Java Basics: Csci210: Data Structures Spring 2009
1. INHERITANCE
Code sharing is a good thing. First, you do not want to re-write the same stuff more
than once. Also, code is likely to be more reliable as it only has to be debugged
once. A mechanism for sharing code is inheritance.
class A {
//variables
//methods
}
class B extends A {
//class B inherits ALL variables and methods of class A
}
We say that class A is the parent of class B, or the super-class of class B; class
B is the sub-class. A class can inherit from a single other class (in Java).
The sub-class inherits all the variables and methods in the super-class. It is as
though the variables and methods of the super-class would be textually present in
the sub-class.
Example:
class OneDPoint
Example:
class Person {
String name; //the name
void Person(String s) {
name = new String(s);
}
String getName() {
return name;
}
};
class Student extends Person {
2. JAVA INTERFACES
Java has the concept of an interface. An interfaces is like a class, just that it
does not give the implementation, just the signatures of the methods. All methods
in an interface must be public.
public interface xxx {
Now we can write a piece of code that works with a generic List:
void collectData(List l) {
....
}
and we can call this passing an argument either an ArrayList or a Vector.
class Vector {
Object[] myArray;
...
x = new SomeClass();
y = x;
//then x==y will return true
z = a copy of x
//then x==z will return false, even though the values are equal
What if we want to check whether the two objects have the same fields? Any
class has a (default) equals() method that is inherited from class Object. The
default version of equals() checks for equality of reference, just like ==.
Usually you are not interested whether the two objects refer to the same object,
but rather, whether two objects are equal—i.e. their fields are equal. If you want
to check data equality, you need to implement (override) .
This has been done for example on class String. The equals method on class
String checks for data equality. For example,
String s = "Bowdoin";
if (s.equals("bowdoin"))
will return true.
6. SCOPE
Types of variables in Java:
(1) instance variables (non-static)
(2) class variables (static variables)
(3) local variables
(4) parameters
Scope: Within a method. Between methods. Between classes.
Variables must be declared within a class (instance variables or global variables)
or a method (local variables).
An instance variable is visible/accessible to all methods of that class. If it is
public, it is also visible outside the class.
A local variable is visible only inside the method; in the code following its decla-
ration, within the tightest enclosing .
The parameters of a method are visible (only) inside the method and do not need
to be re-declared inside.
6.1 this
Used to refer to an instance variable that clashes with a parameter:
class A {
private int age;
//instance variable
private int x = 1;
int a = 100;
System.out.println("before test1: a = " + a);
s.test1(a);
//a is changed in test1(), but not visible outside
System.out.println("after test1: a = " + a);
s.test2();
s.print();
ScopeTester x, y;
x = new ScopeTester();
y = new ScopeTester();
if (x==y) System.out.println("same object");
else System.out.println("NOT same object");
if (x.equals(y)) System.out.println("equal");
else System.out.println("not equal");
}
}
assert (x != 0);
When the code is executed, if x is non-zero, the assertion succeeds. Otherwise,
if x is zero, the assertions fails, and the program terminates with an error.
In order to enable assertion you have to compile with the flag -ea:
javac -ea xxx.java
Assertions don’t have any effect on the code as such, they are just used to make
sure that certain conditions are true. It is good pratice and style to include asser-
tions in your code. They help you catch errors earlier.