Object Orientation
From Suhrid.net Wiki
Constructors
- NO return type.
- Can have all the access modifiers.
- Can't be marked as abstract or final or static.
Class 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.
- For classes and subclasses in the same package protected works in exactly the same way as package(default).
- For subclasses in other packages, protected members (both fields and methods) are only accessible through inheritance in the subclass.
- The subclass in a different package 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; //Wont 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 work - str only accessible through inheritance
}
}
- One more example
package pkgA;
public class Animal {
int count = 10;
protected int secret = 42;
void eat() {
System.out.println("Animal Eat");
}
protected void mope() {
System.out.println("Animal Moping");
}
}
//Same package - different class
package pkgA;
public class Zoo {
public static void main(String[] args) {
Zoo z = new Zoo();
z.go();
}
public void go() {
Animal a = new Animal();
//Package and protected are accessible
a.eat();
a.mope();
System.out.printf("Package : %d %n", a.count);
System.out.printf("Protected : %d %n", a.secret);
}
}
//Same package - subclass
package pkgA;
public class Cat extends Animal {
public static void main(String[] args) {
Cat c = new Cat();
c.catnap();
}
public void catnap() {
System.out.println("Animal count :" + count); //package variable through inheritance
System.out.println("Secret : " + secret); //protected variable through inheritance
eat(); //package method through inheritance
mope(); //protected method through inheritance
Animal a = new Animal();
//Access through super-class reference or super in all cases will work.
a.count = 20;
super.secret = 18;
super.eat();
a.mope();
}
}
//Subclass in a different package
package pkgB;
import pkgA.Animal;
public class Dog extends Animal {
public static void main(String[] args) {
Dog d = new Dog();
d.dawg();
}
public void dawg() {
//None of the package level members will be accessible.
System.out.println(secret); //This will work
System.out.println(super.secret) // OR this will too
Animal a = new Animal();
System.out.println(a.secret); //This wont. Compile error.
System.out.println(mope()); //OK
System.out.println(super.mope()); //OK
System.out.println(a.mope()); //NO. Compiler error.
}
}
Abstract Methods
As they go against the logic of overriding :
- abstract methods cannot be static.
- abstract methods cannot be private.
Overriding
- Polymorphic method invocations apply ONLY to instance methods.
- The argument list for the overriding method must EXACTLY match that of the overriden method. Covariant arguments wont work!