Difference between revisions of "Object Orientation"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 22: Line 22:
 
* For classes and subclasses in the same package protected works in exactly the same way as package(default).  
 
* 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.
 
* For subclasses in other packages, protected members (both fields and methods) are only accessible through inheritance in the subclass.
* The subclass cannot use a reference to super to access the protected member.
+
* The subclass in a different package cannot use a reference to super to access the protected member.
  
 
<syntaxhighlight lang="java5">
 
<syntaxhighlight lang="java5">
Line 37: Line 37:
 
   public void go() {
 
   public void go() {
 
     Foo f = new Foo();
 
     Foo f = new Foo();
     String s = f.str; //Won't work ! Compiler error
+
     String s = f.str; //Wont work ! Compiler error
 
     System.out.println(str); //Will work - through inheritance
 
     System.out.println(str); //Will work - through inheritance
 
   }
 
   }
Line 47: Line 47:
 
   public void goo() {
 
   public void goo() {
 
         Bar b = new Bar();
 
         Bar b = new Bar();
         System.out.println(b.str); //wont'w work - str only accessible through inheritance
+
         System.out.println(b.str); //wont work - str only accessible through inheritance
 
   }
 
   }
 
}
 
}
 +
 +
</syntaxhighlight>
 +
 +
* One more example
 +
 +
<syntaxhighlight lang="java5">
 +
 +
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 - 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() {
 +
System.out.println(secret); //This will work
 +
                System.out.println(super.secret) // OR this
 +
                Animal a = new Animal();
 +
                System.out.println(a.secret); //This wont. Compile error.
 +
 +
}
 +
}
 +
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 02:19, 22 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

  • 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 - 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() {
		System.out.println(secret); //This will work
                System.out.println(super.secret) // OR this
                Animal a = new Animal();
                System.out.println(a.secret); //This wont. Compile error.

	}
}

Abstract Methods

As they go against the logic of overriding :

  • abstract methods cannot be static.
  • abstract methods cannot be private.