Different kinds of instance of Child- Parent class hierarchy in c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know the relation between instances of parent child in c#.
Suppose you have two classes Base and Derived:
Base b = new Base()
Derived d = new Derived()
Derived db = new Base()
Base bd = new Derived()
then what is the meaning of above types of objects
Please suggest I am confused.

Assuming that Base is actually the base class of Derived....
Line 1 contains an instance of the base class.
Line 2 contains a instance of the Derived class.
These are both "standard" variables, where the variable type exactly matches the instance it contains.
Line 4 is an instance of derived class, but restricted to only accessing base class methods. Think of it as a slightly restricted view of the derived class.
Line 3 should produce a compilation error. You can't place an instance of a base class in to a variable of a more restrictive type. Think of it like this "All Dogs(derived class) are Animals(base class), but not all Animals are Dogs.

Related

How to use "new" on an abstract class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I've tried to search for other questions like mine but I can't find any good result
I'm trying to use a DLL that has a method that need to be called with a new class but that class is abstract.
thanks.
i tried to use new IClientHandler but it just errors
You're trying to instantiate an abstract class in C# code. This is not possible, as an abstract class is a class that cannot be instantiated on its own. Instead, you would need to create a non-abstract class that extends the abstract class and then use that to create an object.
Here's an example:
abstract class AbstractClass
{
public abstract void AbstractMethod();
}
class ConcreteClass : AbstractClass
{
public override void AbstractMethod()
{
Console.WriteLine("I'm a non-abstract method!");
}
}
// Now we can create an instance of ConcreteClass
var instance = new ConcreteClass();
You can then call the AbstractMethod on the instance object, since it has been implemented in the ConcreteClass.
Also my small mistake, as #ewerspej noticed:
IClientHandler is an interface, not an abstract class. Neither can be instantiated. Interfaces require implementations that can be instantiated and abstract classes must be extended by non-abstract classes that can be instantiated.

In c#, can a class inherit an interface? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that an interface inherits another interface, and a class can inherit from another interface, but can a class inherit from another interface?
Interfaces are meant to be implemented by classes. when you declare an interface you expect to work with an instance that implements it - that instance must be a class, since you cannot instantiate interfaces.
The case when you declare an interface which inherits from a different interface is possible, and allows you to separate the responsibility of each interface, but force a class to implement all of those responsibilities
As mentioned in my comment on the question, MSDN example for implementing interfaces HERE
It's called "implementing" the interface.
If interface Inter1{} and class A : Inter1, then if class B : A, class B will also "inherit" the interface Inter1.
From Microsoft's Inheritance (C# Programming Guide)
A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.
So, a class:
can implement multiple interfaces (no limits)
inherits from a base class (just the one. If none is declared, the base class is object by default)

Should a class with only static methods be static? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a class with only static methods. Should the class itself be made static too? Does it matter?
Does it matter?
Making a class static ensures that it can never be instantiated by generating a compiler error should the user attempt to do so. If the class, consisting of only static members, is simply not intended to be instantiated, there is no reason not to make it static. You can choose not to do so, but instances of such a class aren't going to be very useful, and users creating these instances are going to be left quite confused.
On the other hand, if you intend for instances of this class to be created but you expect derived classes to implement their own instance members, chances are that this class should be abstract, rather than static (and perhaps those instance members should be stated upfront via abstract definitions or an interface).
In general: Yes.
You can prevent the programmer to create object instances of a certain class by making the class static. If this is what you intend, then do it. This prevents mistakes, by showing (other collegues, etc.) that the class is not intended to be instantiated.
public static class A
{
// Some static member
}
A a = new A(); // Compilation error

C# method signatures - restricting types - what's the correct terminology? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a method in the Common class of a project I am working on which is defined like this:
public static void PopulateSoapBody<TEnum>(Object obj, string[] aMessage) where TEnum : struct, IComparable, IFormattable, IConvertible
and it is called like this (from several different classes defining their own enumerated types, and populating their own soap body classes) :
DCSSCardUpdateType wsSoapBody = new DCSSCardUpdateType();
Common.PopulateSoapBody<CardPinRequest>(wsSoapBody, aMessage);
where
CardPINRequest is an Enum Type defined in the calling class
wsSoapBody is a class type defined in the web service
aMessage is a string array (used to populate the wsSoapBody)
What is it called when an enum type is passed in like that to a method, restricting the possible types (I'd like to read up on it to understand better how to make use of features d like this)
I think the term you are looking for is generic type constraints.
From the linked MSDN article:
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints.

Does interface creates objects if it is implemented by a class? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If a class instantiated it will create an object.Memory will allocated to the instance. What happens if Interface intstantiated? Does interface has constructors? Does it create an interface object.does it alllocate memory to interface object
interface IInteface {}
class Test : IInterface{}
IInterface ex1 = new Test();
what above line will create ?
Interfaces are abstract concepts that cannot be instantiated. They serve to define a contract for implementing classes to fulfill.
Then, you can create instances of concrete classes implementing the interface (usually with new), and use an interface reference to point to that instance.
Interfaces have no constructors and can't be created by themselves.
Assigning an object to variable (including variable of interface type) does not create new object, it is just another reference to the same object.
class DerivedWithInterface: Base, IEnumerable {}
Now you can create instances of DerivedWithInterface class and assign to variable of any of base classes/interfaces, but only new will create an object:
DerivedWithInterface item = new DerivedWithInterface();
IEnumerable asEnumerable = item; // asEnumerable is the same object as create before
Base asBase = item;
Now you can do casts back to original object and there still will be exactly one (or as many as you've newed up):
IEnumerable asEnumerableItem = new DerivedWithInterface();
DerivedWithInterface itemViaCast = (DerivedWithInterface)asEnumerableItem;
both asEnumerableItem and itemViaCast refer to the same single instance of and object of type asEnumerableItem

Categories

Resources