Interface Abc (Void Bounce // Compiler Will See Public Abstract Void Bounce Void Setbouncefactor (Int F) )
Interface Abc (Void Bounce // Compiler Will See Public Abstract Void Bounce Void Setbouncefactor (Int F) )
the implementing class must do.(All interface methods must be implemented, and must be marked public.) Class Tire implements Bounceable public void bounce( ){...} public void setBounceFactor(int bf){ }
Think of an interface as a 100-percent abstract class. Like an abstract class, an interface defines abstract methods that take the following form:
abstract void bounce(); // Ends with a semicolon rather than // curly braces
But while an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. All interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract.
All variables defined in an interface must be public, static, and final in other words, interfaces can declare only constants, not instance variables.
Interface methods must not be static. Because interface methods are abstract, they cannot be marked final, strictfp, or native. (More on these modifiers later.) An interface can extend one or more other interfaces. An interface cannot extend anything but another interface. An interface cannot implement another interface or class. An interface must be declared with the keyword interface. Interface types can be used polymorphically
Typing in the abstract modifier is considered redundant; interfaces are implicitly abstract whether you type abstract or not. You just need to know that both of these declarations are legal, and functionally identical:
public abstract interface Rollable { } public interface Rollable { } void bounce(); public void bounce(); abstract void bounce(); public abstract void bounce(); abstract public void bounce();
final void bounce(); // final and abstract can never be used // together, and abstract is implied static void bounce(); // interfaces define instance methods private void bounce(); // interface methods are always public protected void bounce(); // (same as above) doubt: protected will work or not?
Because interface constants are defined in an interface, they don't have to be declared as public, static, or final. They must be public, static, and final, but you don't have to actually declare them that way.
interface Foo { int BAR = 42; void go(); } class Zap implements Foo { public void go() { BAR = 27; } }//error
You can't change the value of a constant! Once the value has been assigned, the value can never be modified doubt: can we declare variable without initialization? And it be can be initialized in sub class?
public int x = 1; // Looks non-static and non-final, // but isn't! int x = 1; // Looks default, non-final, // non-static, but isn't! static int x = 1; // Doesn't show final or public final int x = 1; // Doesn't show static or public public static int x = 1; // Doesn't show final public final int x = 1; // Doesn't show static static final int x = 1 // Doesn't show public public static final int x = 1; // what you get implicitly
Any combination of the required (but implicit) modifi ers is legal, as is using no modifi ers at all! On the exam, you can expect to see questions you wont be able to answer correctly unless you know, for example, that an interface variable is fi nal and can never be given a value by the implementing (or any other) class.
Constructor: Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you.
class Foo { protected Foo() { } // this is Foo's constructor protected void Foo() { } // this is a badly named, // but legal, method }
Constructor declarations can however have all of the normal access modifiers, and they can take arguments (including var-args), just like methods. Constructors can't be marked static (they are after all associated with object instantiation), they can't be marked final or abstract (because they can't be overridden). Here are some legal and illegal constructor declarations:
class Foo2 { // legal constructors Foo2() { } private Foo2(byte b) { } Foo2(int x) { } Foo2(int x, int... y) { } // illegal constructors void Foo2() { } // it's a method, not a constructor Foo() { } // not a method or a constructor Foo2(short s); // looks like an abstract method static Foo2(float f) { } // can't be static final Foo2(long x) { } // can't be final abstract Foo2(char c) { } // can't be abstract Foo2(int... x, int t) { } // bad var-arg syntax }
Primitive variables can be declared as class variables (statics), instance variables, method parameters, or local variables. integer types the sequence from small to big is byte, short, int, long, and that doubles are bigger than floats. All six number types in Java are made up of a certain number of 8-bit bytes, and are signed, meaning they can be negative or positive. The leftmost bit (the most significant digit) is used to represent the sign, where a 1 means negative and 0 means positive, The positive range is one less than the negative range because the number zero is stored as a positive binary number. We use the formula -2(bits-1) to calculate the negative range, and we use 2(bits-1)1 for the positive range. Again, if you know the first two columns of this table, you'll be in good shape for the exam.
Bytes Minimum Range Maximum Range
byte 8 1 -27 27-1 short 16 2 -215 215-1 int 32 4 -231 231-1 long 64 8 -263 263-1 float 32 4 n/a n/a double 64 8 n/a n/a