Difference between revisions of "Object Orientation"
From Suhrid.net Wiki
Jump to navigationJump to search| Line 10: | Line 10: | ||
* An abstract class cannot be instantiated (e.g. DateFormat) | * 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. | ||
| + | |||
| + | <u> Protected </u> | ||
| + | * Only accessible through inheritance in the subclass. | ||
| + | * The subclass cannot use a reference to super to access the protected member. | ||
| + | |||
| + | <syntaxhighlight lang="java5"> | ||
| + | |||
| + | 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 | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </syntaxhighlight> | ||
| + | |||
| + | <u> Abstract Methods </u> | ||
| + | |||
| + | As they go against the logic of overriding : | ||
| + | |||
| + | * abstract methods '''cannot''' be static. | ||
| + | * abstract methods '''cannot''' be private. | ||
[[Category:OCPJP]] | [[Category:OCPJP]] | ||
Revision as of 23:36, 21 August 2011
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
- 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.