joeyyyyy
»» READMORE...
Friday, June 14, 2013
Monday, December 19, 2011
Class Definition » toString
Override toString() for Box class.
»» READMORE...
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
public String toString() {
return "Dimensions are " + width + " by " + depth + " by " + height + ".";
}
}
class toStringDemo {
public static void main(String args[]) {
Box b = new Box(10, 12, 14);
String s = "Box b: " + b; // concatenate Box object
System.out.println(b); // convert Box to string
System.out.println(s);
}
} | override the toString method in your classes | ||||||
|
| Reflection based toString() utilities | |||||||||||
|
Friday, December 16, 2011
Class Definition » Static Import
| Static import | ||||||||||||
| In Java 5 you can import static fields using the import static keywords. | ||||||||||||
| Before Java 5, to use a static final field in the Calendar class, you must first import the Calendar class. | ||||||||||||
| ||||||||||||
| Then, you can use it by using the notation className.staticField. | ||||||||||||
| ||||||||||||
| In Java 5, you can do | ||||||||||||
|
Thursday, December 15, 2011
Class Definition » import
Importing Other Classes
import statements
»» READMORE...
| ava provides the keyword import to indicate that you want to use a package or a class from a package. For example, to use the java.io.File class, you must have the following import statement: | |
|
| |||||||||||||||||
|
Monday, December 12, 2011
Class Definition » Abstract Class
Abstract Classes
»» READMORE...
| |
| |
| |
|
Sunday, December 11, 2011
Class Definition » Final Class
Final Classes
You can prevent others from extending your class by making it final using the keyword final in the class declaration.
»» READMORE...
You can prevent others from extending your class by making it final using the keyword final in the class declaration.
final class FinalClass{
} Using final to Prevent Overriding
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
} http://www.java2s.com/Tutorial/Java/0100__Class-Definition/Catalog0100__Class-Definition.htm Friday, December 9, 2011
Class Definition » super
Variable in subclass hides the variable in the super class
http://www.java2s.com/Tutorial/Java/0100__Class-Definition/Catalog0100__Class-Definition.htm
»» READMORE...
class A {
int i;
}
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
Subscribe to:
Posts (Atom)