Object Orientation
From Suhrid.net Wiki
Jump to navigationJump to searchClass Modifiers
Regular Classes:
- Access Modifiers: public and default only
- Non access: strictfp, abstract and final only
- Adding any other modifier will result in a compiler error.
- A final class cannot be overriden (e.g. String)
- An abstract class cannot be instantiated (e.g. DateFormat)
Class Members
- Members can use all the access modifiers: public, private, default(package), protected
- Private members are not inherited.
- This means private members can be redeclared in subclasses.
- Instance level variables cannot be synchronized, abstract, strictfp, native.
Protected
- Protected is wider than package. Protected = package + subclasses.
- Only accessible through inheritance in the subclass.
- The subclass cannot use a reference to super to access the protected member.
package pkg1;
public class Foo {
protected String str = "Hello";
}
package pkg2;
public class Bar extends Foo {
public void go() {
Foo f = new Foo();
String s = f.str; //Won't work ! Compiler error
System.out.println(str); //Will work - through inheritance
}
}
package pkg2;
public class Fubar {
public void goo() {
Bar b = new Bar();
System.out.println(b.str); //wont'w work - str only accessible through inheritance
}
}
Abstract Methods
As they go against the logic of overriding :
- abstract methods cannot be static.
- abstract methods cannot be private.