is it possible to inherit a class but methods declared in
the class should not be inheritable i possible how?

Answers were Sorted based on User's Feedback



is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / aakanchha

Yes..!!
We can define a method as "private" ,so the method will not
be inherited.

and by 'sealed' we can prevent overriding but can not
prevent inheriting .

Is This Answer Correct ?    17 Yes 1 No

is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / arunkumar

s it is possible by doing Sealed keyword to the method in
first class

Is This Answer Correct ?    21 Yes 8 No

is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / ashish modi

class X
{
protected virtual void F() { Console.WriteLine("X.F"); }
protected virtual void F2() { Console.WriteLine
("X.F2"); }
}
class Y : X
{
sealed protected override void F() { Console.WriteLine
("Y.F"); }
protected override void F2() { Console.WriteLine
("X.F3"); }
}
class Z : Y
{
// Attempting to override F causes compiler error
CS0239.
// protected override void F() { Console.WriteLine
("C.F"); }

// Overriding F2 is allowed.
protected override void F2() { Console.WriteLine
("Z.F2"); }
}

Is This Answer Correct ?    12 Yes 2 No

is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / vishal

That's Correct.
Using private keyword in the method signature is a way of
preventing inheritance of that method in child class.

Please note that using "sealed" keyword in method signature
only prevents overriding of the method in child class, but
the method is still inheritable in the child class.

Is This Answer Correct ?    8 Yes 0 No

is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / vimal

Use 'Sealed' Keyword with Method

Is This Answer Correct ?    0 Yes 1 No

is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / vimal

Use 'Sealed' Keyword with Method

Is This Answer Correct ?    0 Yes 1 No

is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / bikas pandey

Let The Class To Public And The Method To Be Sealed

Is This Answer Correct ?    1 Yes 4 No

is it possible to inherit a class but methods declared in the class should not be inheritable i po..

Answer / rajni

yes it is possible for method not to inherit make it final.

Is This Answer Correct ?    0 Yes 5 No

Post New Answer

More C Sharp Interview Questions

What is difference between Enum and Struct?

0 Answers   UGC Corporation,


Is void a class?

0 Answers  


Can you inherit from multiple classes in c#?

0 Answers  


Where does the dispose method lie and how can it be used to clean up resources?

2 Answers   TCS, Wipro,


How are methods overloaded?

0 Answers  






You have got 1 million parking slots. At a time a parking slot can be free or not. To get next slot easily which data structure to implement?

0 Answers  


Can a loop recorder detect a heart attack?

0 Answers  


How do you inherit from a class in C#?

3 Answers   Visual Soft,


What is typeof c#?

0 Answers  


When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)?

2 Answers   Mind Tree,


C# Code for How to set readonly property to Id(database) value?

2 Answers   HCL,


Explain the process of inheriting a class into another class?

0 Answers  


Categories