Total Pageviews

Friday, February 18, 2011

Abstract Class

Abstract Class in java
Abstract classes can be implemented in java. A class is declared abstract using the keyword "abstract" as a prefix. An abstract class may or may not contain any
abstract method. Now lets understand what exactly is an abstract class. Abstract class can be considered as a generic class implemented. Consider the following example :
Their are diffrent classes for various shapes like Rectangle,Square,Circle,Ellipse....
All share some comman properties like color,dimension,fillColor. These comman features/functionality can be kept in a generic class called Shapes. Also each individual class differs in size. Lets say each needs a method changeSize(), The method cannot be kept in superclass Shapes as each class has its own implementation and Shapes being a generic class does not requires this function. If we declare this function as abstract, its signature can be kept in the Shapes class. Each class which extends the Shapes class can write the changeSize method as per its own implementation.

Note that the same can be achieved using interfaces also, but interface cannot have implemenaion of the methods. So the comman methods would be required to be defined in all the classes implementing the interface. Interfaces will be discussed later in the blog,but for the time being just consider it as an implementaion of inheritance in java.

Some key points in abstract classes:
1.A subclass extending an abstract class may or may not define the abstract methods. To do so make the subclass itself as abstract.
2. Abstract class cannot be instantiated.
3. If implementation contains only method signatures and those methods need to be overridden in every sub class then make it as interface rather than abstract class.


An advantage of abstract class over interace is that abstract classes can contain concrete methods also. So in guture if a new method is to be introduced, just add it as a concrete method in abstract class and can be used by the subclass.

Abstract class example:


abstract class Shapes{
private String color; // Should have used Color class instead
public void setColor(String color){
.....
}
public void abstract changeShape(....); //No implementation
}

public class Rectangle extends Shapes{

public void changeShape(...){
//implement the specialises method
}
}


It can also be possible for a class not implementing all the abstract methods. Add another abstract method named "repostion" in Shapes class.
Now do as followed


abstract class Rectangle extends Shapes{
public void changeShape(...){
//implement the specialises method
}
}


class AnotherClass extends Rectangle {

public void reposition(){
//implemet method here
}
}

The above class contaiins all the abstract methods of the Shapes class which were not implemented by Rectangle class.

For more details on abstract class click here .
For comparison between abstract class and interface click here

Thursday, February 17, 2011

Inheritance Continued....

Lets look deeply into inheritance.
Consider the following example :
public class SuperClass{
public String name = "SuperClass Name";
public static String staticName = " SuperClass Static Name";
public static String staticString = "SuperClass Staic String";

public static void printVal(){
.............
}
public void checkVal(){
........
}
}

public class SubClass extends SuperClass{
public String name = "SubClass name";
public static String staticName = "SubClass static name";
protected int staticString = 1;


public static void printVal(){
.............
}
public void checkVal(){
........
}
}

public static void main(....){
SuperClass superWithSuperCon = new SuperClass();
SuperClass superWithSubCon= new SubClass();
SubClass subWithSubCon = new SubClass();

sout(superWithSuperCon.name);
sout(superWithSuperCon.staticName); /*Though being static should have been accessed by using Class name,but java converts the same automatically at compile time(Not recommended)*/
sout(superWithSuperCon..staticString);

sout(superWithSubCon.name);
sout(superWithSubCon.staticName);
sout(superWithSubCon.staticString);

sout(subWithSubCon.name);
sout(subWithSubCon.staticName);
sout(subWithSubCon.staticString);

superWithSuperCon.printVal(); /*SuperClass methods called - Compile time binding*/
superWithSuperCon.checkVal(); /*SuperClass methods called - Run time binding*/

superWithSubCon.printVal(); /*SuperClass methods called - Compile time binding*/
superWithSubCon.checkVal(); /*SubClass methods called - Run time binding*/

subWithSubCon.printVal(); /*SubClass methods called - Run time binding*/
subWithSubCon.checkVal(); /*SubClass methods called - Run time binding*/
}
The output obtained is :
SuperClass Name
SuperClass Static Name
SuperClass Staic String
SuperClass Name
SuperClass Static Name
SuperClass Staic String
SubClass name
SubClass static name
1
The strange behaviour of variable in case 4 is because java does not supports instance variable overriding.Since static variables are class variables they are resolved during compile time only.
Note : Methods of type private/final/static or constructors are resolved at compile time only ie static binding.

Thursday, February 3, 2011

Files with no public classes can have a name that does not match any of the classes in the file.


if you declare a class A in a file called X.java as follows ::

class A{

------

}


the class will compile correctly.

But if you declare the class A as ::

public class A{

}

this will result in an error as --- ( class A is public, must be declared in a file A.java )



This comes under Source File Declaration Rules as defined by java Developers.