I.T. Discussion Community!
-Collapse +Expand
C++
Search C++ Group:

Advanced
-Collapse +Expand C++ To/From
To/FromCODEGuides
-Collapse +Expand C++ Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C++ Store
PRESTWOODSTORE
   ► KBTo/From GuidesReferenceOOP Details  Print This    All Groups  

Prevent Derivation (Cross Ref > OOP Details)

Prevent Derivation

Languages Focus

How do you prevent another class from inheriting and/or prevent a class from overriding a member.

Access VBA:   Not Supported

ASP Classic:   Not Supported

C#:   sealed

With C#, use the sealed keyword to prevent a class from being inherited from and to prevent a method from being overridden.

A method marked sealed must override an ancestor method. If you mark a class sealed, all members are implicitly not overridable so the sealed keyword on members is not legal.

Syntax Example:
public class Machine : System.Object
{
public virtual void Speak(String pSentence)
{
MessageBox.Show(pSentence);
}
}

public class Robot : Machine
{
public sealed override void Speak(String pSentence)
{
MessageBox.Show(pSentence);
}
}
  
public sealed class Cyborg : Robot
{
}

More Info

C++: 

Make the constructor a private member of the class.

More Info / Comment

More Info

C++/CLI:   sealed

Corel Paradox:   Not Supported

Delphi:  "Sealed class" sealed, final

With Delphi, use the sealed keyword to prevent a class from being inherited from and use the final keyword to prevent a method from being overridden.

Syntax Example:
type
  Robot = class sealed(TObject)
public
  procedure Speak(pSentence: String); virtual; final;
end;

Support in Previous Versions

Delphi 7 and earlier do not suppor the Sealed keyword.

Delphi for .Net 2005

Delphi for .Net (pre Prism) supports the sealed keyword. With C#, you use the sealed keyword. As you would expect, Object Pascal for .Net does implement the sealed keyword for use with Delphi for .Net.



Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Advanced

1 Advanced Level Question

Question #1: Multiple Choice

In later versions of Delphi, you prevent class and method inheritance with what keyword(s)?

Answer:
1. 

You prevent derivation with the sealed keyword for classes and the final keyword for methods.

type
  Robot = class sealed(TObject)
public
  procedure x(); virtual; final;
end;
2. 

You prevent derivation with the final keyword for classes and the sealed keyword for methods.

type
  Robot = class final(TObject)
public
  procedure x(); virtual; sealed;
end;
3. 

You prevent derivation for classes and methods using the sealed keyword.

type
  Robot = class sealed(TObject)
public
  procedure x(); virtual; sealed;
end;
4. 

You prevent derivation for classes and methods using the final keyword.

type
  Robot = class final(TObject)
public
  procedure x(); virtual; final;
end;
5. 

Delphi for Win32 does not support preventing of inheritance with any version of Delphi including later versions.

More Info

Delphi Prism:   sealed, final

Same keywords as Delphi (sealed uses slightly different syntax). With Prism, use the sealed keyword before the class keyword to prevent a class from being inherited from and use the final keyword to prevent a method from being overridden.

Syntax Example:
type
  Robot = public sealed class(System.Object) 
public
method Speak(pSentence: String); virtual; final;
end;


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Advanced

1 Advanced Level Question

Question #1: Multiple Choice

You prevent class and method inheritance with what keyword(s)?

Answer:
1. 
Robot = public sealed class(System.Object)
public
 method x(); virtual; final;
end;
2. 
Robot = public final class(System.Object)
public
 method x(); virtual; sealed;
end;
3. 
Robot = public sealed class(System.Object)
public
 method x(); virtual; sealed;
end;
4. 
Robot = public final class(System.Object)
public
 method x(); virtual; final;
end;
5. 
Robot = public NotOverridable class(Object)
public
 method x(); virtual; NotInheritable;
end;

More Info

 

Java:  "final class" Final

In Java, there is the concept of a final class.

More Info / Comment

More Info

VB Classic:   Not Supported

VB Classic supports a form of single level inheritance where you, in essence, create an abstract class and then implement it in one or more classes that inherit from the abstract class. However, you cannot have any further descendant classes so "prevent derivation" is implemented by design of this simple inheritance model.

More Info / Comment

More Info

VB.Net:   NotInheritable, NotOverridable

With VB.Net, use the NotInheritable keyword to prevent a class from being inherited from and use the NotOverridable keyword to prevent a method from being overridden.

A method marked NotOverridable must override an ancestor method. If you mark a class NotInheritable, all members are implicitly not overridable so the NotOverridable keyword is not legal.

Syntax Example:
Public Class Machine
  Public Overridable Sub Speak(ByRef pSentence As String)
MessageBox.Show(pSentence)
End Sub
End Class
  
Public Class Robot
Inherits Machine
  Public NotOverridable Overrides Sub Speak(ByRef pSentence As String)
MessageBox.Show(pSentence)
End Sub
End Class
  
Public NotInheritable Class Cyborg
Inherits Robot
End Class

More Info






Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech