Karatas, FERHAT
I would love to change the world, but they won't give me the source code!
Abstract Classes versus Interfaces
28.2.2007 11:59:11 - Filed under : Java
Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation.
If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way.

Keywords : Abstract classes are similar to interfaces, except that they provide a partial implementation.
with 0 comments

Defining an Interface
28.2.2007 11:53:37 - Filed under : Java
An interface declaration consists of modifiers, the keyword interface, the interface name, a comma-separated list of parent interfaces (if any), and the interface body. For example:
public interface GroupedInterface extends Interface1,Interface2, Interface3 {
       // constant declarations
       double E = 2.718282;  // base of natural logarithms

       // method signatures
       void doSomething (int i, double x);
       int doSomethingElse(String s);
The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface.

An interface can extend other interfaces, just as a class can extend or subclass another class.
However, whereas a class can extend only one other class, an interface can extend any number of interfaces.
The interface declaration includes a comma-separated list of all the interfaces that it extends.

The interface body contains method declarations for all the methods included in the interface. A method declaration within an interface is followed by a semicolon, but no braces, because an interface does not provide implementations for the methods declared within it. All methods declared in an interface are implicitly public, so the public modifier can be omitted.

An interface can contain constant declarations in addition to method declarations. All constant values defined in an interface are implicitly public, static, and final. Once again, these modifiers can be omitted.

Keywords : Interface; can extend other interfaces, does not provide implementations, a class that implements an interface must implement all the methods declared in the interface.
with 0 comments

Abstract Methods and Classes
28.2.2007 11:09:49 - Filed under : Java
An abstract class is a class that is declared abstract-it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class GraphicObject {
       // declare fields
       // declare non-abstract methods
       abstract void draw();
When an abstract class is subclassed, the subclass provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Note: All of the methods in an interface are implicitly abstract, so the abstract modifier is not used with interface methods (it could be-it's just not necessary).

Example :

abstract class myBase{
    //A Non abstract method
    int AddTwoNumbers(int Num1, int Num2)
        return Num1 + Num2;

    //An abstract method, to be overridden in derived class
    abstract int MultiplyTwoNumbers(int Num1, int Num2);

class myBusiness extends myBase {
    /** Creates a new instance of myBusiness */
    myBusiness() {
    //implementing the abstract method MultiplyTwoNumbers
    int MultiplyTwoNumbers(int Num1, int Num2)
        return Num1 * Num2;

public class myStart {
    /** Creates a new instance of myStart */
    public myStart() {
    public static void main (String args[]){
       //You can create an instance of the derived class

       myBusiness calculate = new myBusiness();
       int added = calculate.AddTwoNumbers(10,20);
       int multiplied = calculate.MultiplyTwoNumbers(10,20);
       System.out.println( "Added :" + added + " Multiplied :" + multiplied);

Keywords : Abstract class cannot be instantiated. And subclass provides implementations for all of the abstract methods in its parent class.
with 0 comments

Accessing Superclass Members
12.2.2007 11:33:48 - Filed under : Java
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:
public class Superclass {
        public void printMethod() {
            System.out.println("Printed in Superclass.");
Here is a subclass, called Subclass, that overrides printMethod():
public class Subclass extends Superclass {

        public void printMethod() { //overrides printMethod
            System.out.println("Printed in Subclass");
        public static void main(String[] args) {
        Subclass s = new Subclass();
Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:
Printed in Superclass.
Printed in Subclass
Subclass Constructors
The following example illustrates how to use the super keyword to invoke a superclass's constructor. Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own:
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
            super(startCadence, startSpeed, startGear);
            seatHeight = startHeight;
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
super(parameter list);
With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.

Related Terminology : "Base" keyword in C#
with 0 comments

Previous | Next
Current Page: 1