05-29-2008 11:22 AM
hi,
I want to know diff between abstrect class and final class.
Thanks in advance.
Regards,
Agalya
Edited by: agalya devi on May 29, 2008 12:23 PM
05-29-2008 11:27 AM
An abstact clas is a class which must be sub-classed and a final
class is a class which cant be sub-classed. That means to get the functinaity of an abstract class we must sub class it and use the sub-class but on the other hand a final class must be used as it is without any alteration.......Hope this satisfies all...
in short
Abstract class -> it is not must to override all methods in abstract class. Abstract class may contain abstract methods and non abstract methods.. in this case abstract methods should be overriden and non abstract methods are need not to be overriden
Abstract class can be inherited
Final class -> overriden is not possible, this class cannot be inherited..
reward if it helps
05-29-2008 11:27 AM
An abstact clas is a class which must be sub-classed and a final
class is a class which cant be sub-classed. That means to get the functinaity of an abstract class we must sub class it and use the sub-class but on the other hand a final class must be used as it is without any alteration.......Hope this satisfies all...
in short
Abstract class -> it is not must to override all methods in abstract class. Abstract class may contain abstract methods and non abstract methods.. in this case abstract methods should be overriden and non abstract methods are need not to be overriden
Abstract class can be inherited
Final class -> overriden is not possible, this class cannot be inherited..
reward if it helps
05-29-2008 11:32 AM
sorry forgot one more thing
In this class we can create 'n' number of methods which can be prefixed by Abstract are abstract methods and which are not prefixed are non-abstract methods. There we can do declaration of the method and partial implementation. We can use all the methods declared in this class in sub classes, or we can skip the methods which we dont want. Abstract Classes doesn't create an object. We can Extend this class
thxxx
05-29-2008 3:38 PM
Hi ,
Refer the following ppt in the link provided.
[www.gridbus.org/~raj/254/Lectures/RajLec13.ppt ]
Thanks,
Surya Pydikondala
05-29-2008 4:01 PM
Hi,
Abstract classes:
One cannot create an object from an abstract class. Only subclasses can be derived from them.
CLASS <classname> DEFINITION ABSTRACT.
Abstract methods cannot be implemented in the same class. Only the subclasses of that class can implement it.
METHODS <method_name> u2026.ABSTRACT
Any class containing an abstract method has to be an abstract class. All subsequent subclasses that do not implement the method must also be abstract. To implement an abstract method in a subclass, one need to redefine this subclass using the REDEFINITION addition.
Abstract methods cannot be implemented in that class. It has to be implemented in one of its subclass. To implement an abstract method in a subclass, one need to redefine this subclass using the REDEFINITION addition.
example1:
Objects cannot be created from an abstract class. Only the subclasses of such class can be instantiated.
This program contains an abstract class C1 and its subclass C2. Object cannot be created from class C1, but possible from class C2.
Incorrect prog:
REPORT YSUBDEL.
CLASS C1 DEFINITION ABSTRACT.
PUBLIC SECTION.
ENDCLASS.
CLASS C1 IMPLEMENTATION .
METHOD : METH1.
ENDCLASS.
CLASS C2 DEFINITION INHERITING FROM C1.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
data : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2.
CREATE OBJECT oref1.----> Instantiation of abstarct class is not possible
Correct:
REPORT YSUBDEL.
CLASS C1 DEFINITION ABSTRACT.
PUBLIC SECTION.
ENDCLASS.
CLASS C1 IMPLEMENTATION .
METHOD : METH1.
ENDCLASS.
CLASS C2 DEFINITION INHERITING FROM C1.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
data : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2.
CREATE OBJECT oref2.
2) Final Classes:
Final classes cannot have subclasses. Only the class can be instantiated.
CLASS <classname> DEFINITION FINAL.
A final method cannot be redefined in subclasses
METHODS <method_name> u2026.FINAL
Example:
Subclasses cannot be inherited from a final Class. They can only be instantiated.
This program contains a final class C1 and a subclass C2. This is not allowed and is resisted at the time of compilation.
Hence, the theme is properly established.
REPORT YSUBDEL.
CLASS C1 DEFINITION FINAL.
ENDCLASS.
CLASS C1 IMPLEMENTATION .
ENDCLASS.
CLASS C2 DEFINITION INHERITING FROM C1.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
data : OREF2 TYPE REF TO C2.
CREATE OBJECT oref2.
Output:
Compilation error is generated:-
The final class C1 cannot have any subclasses.
If it is helpful rewards points
Regards
Pratap.M
05-30-2008 7:25 AM
Hello Agalya,
Abstract Classes and Abstract Methods
You can prevent the instantiation of a class by using the ABSTRACT addition
with the CLASS statement. Superclasses are a typical use for abstract classes, as
they are not intended to be instantiated themselves, but their subclasses are.
In such an abstract class, you can define abstract methods (amongst other things).
This means that you can leave their implementation open. If the subclass of that
class is not abstract, the abstract methods must be redefined there. This means
that it must be implemented for the first time.
CLASS lcl_... DEFINITION ABSTRACT. " Class connot be instatiated
...
ENDCLASS.
CLASS lcl_... DEFINITION ABSTRACT.
...
METHODS ... ABSTRACT ... " Method is not implemented in this class
...
ENDCLASS.
References to such abstract classes can therefore be used for polymorphic access
to subclass instances.
Static methods cannot be abstract because they cannot be redefined.
Final Classes and Methods
You can prevent a class from being inherited by using the FINAL addition with
the CLASS statement.
You can prevent a method from being redefined by using the FINAL addition
with the METHODS statement.
CLASS lcl_... DEFINITION FINAL [ INHERITING FROM ... ]. " Other classes cannot inherit from this one
...
ENDCLASS.
CLASS lcl_... DEFINITION.
...
METHODS ... FINAL ... " Method cannot be redefined
...
ENDCLASS.
Thus, all methods of a final class are implicitly final. Therefore, you may not
repeat the FINAL addition in the methods themselves.
Classes that are abstract and final should only contain static components.
Note: For both cases the relevant indicator is in the Class Builder on the Attributes tab for that class or method.
Peter
05-30-2008 10:41 AM
Hi,
ABSTRACT CLASS / METHOD
If you do not want to implement a method in a class but want it to be implemented in the subclass, then define the method as an abstract method. Any class having an abstract method will become an Abstract class.
E.g.
CLASS C1 DEFINITION ABSTRACT. PUBLIC SECTION.
Method FINAL.
******************************************
reward if helpful.
06-02-2008 12:18 PM
You cannot instantiate objects in an abstract class
On the contrary, they are very useful, since they can (and must) refer to instances in subclasses of the abstract class during runtime.
Abstract classes are normally used as an incomplete blueprint for concrete (that is, non-abstract) subclasses, in order to define a uniform interface.
A final class implicitly only contains final methods
A final class cannot have subclasses, and can protect itself in this way against (uncontrolled) specialization.
Classes,can usefully be both abstract and final: only static components can be used there.
06-24-2008 7:45 AM
Abstract class can have both abstract or nromal methods. These cannot be instantiated ie we cant create any instance of this class. Here we cant have implementation for abstract methods. We need to inherit this class in tonother then provide the implementation.
As we can chain upto N levels in inheritance, ie mutlilevel, If we want to stop the further drill down of the inheritance tree. We need to stop the inhertiance from going further, we go for Final class.
If a class is declared as Final, then i cannot be inherited further. Likewise, if a method is declared as final, then it cant be over ridden ie we cant provide any implementation or it cannt be redefined.
Encourage me by rewarding points.
Regards
Chandralekha
Edited by: Susheela Chandralekha on Jun 24, 2008 8:46 AM