CLASS Point to remember
*Constructor can not be declare as static
*In a class specified , data or function desined private are accessible to member function
of the class
*Private data member can be accessed both from the base class and from its derived class
*A nonmember function apart from friend can not access nonpublic member of a class
*Protected members cannot be inherited
*A static variable declared inside a function exits till the execution of that function only
*The members of a class by default are private
*Member function cannot be called from within a constructor
*In case of nested class , enclosing class can directly access the private data member of nested class
*To initialise an object of a class A ,which is a data member of a class B,the construct of A has to be invoked in the constructor of class B
*If a friend function declare inside a class it can access all data members of the class
*A friend function declared insede a class does not need scope resolution operator while defing it
*Member of a derived class can only be accessed by the object or pointer to the same class
*A derived class can access the private members of its public base class directly
*Friend function can be inherited
*A virtual function cannot be declared a friend in another class
1)How can you prevent your class to be inherited
further?
You can prevent a class from being inherited
further by defining it with the sealed keyword.
2) Is it possible for a class to inherit the
constructor of its base class?
No, a class cannot inherit the constructor of its base class.
3) Can you declare an overridden method to be
static if the original method is not static?
No. Two virtual methods must have the same signature.
4) Why is the virtual keyword used in code?
The
virtual keyword is used while defining a class to specify that
the methods and the properties of that class can be overridden in derived
classes.
5) Can you allow a class to be inherited, but
prevent a method from being overridden in C#?
Yes. Just declare the class
public and make the method
sealed.
6) Does .NET support multiple inheritance?
.NET does not support multiple inheritance directly because in .NET, a class
cannot inherit from more than one class. .NET supports multiple inheritance
through interfaces.
7) Is it possible to execute two catch blocks?
You are allowed to include more than one
catch block in your program;
however, it is not possible to execute them in one go. Whenever, an exception
occurs in your program, the correct
catch block is executed and the
control goes to the
finally block.
8) A structure in C# can implement one or more
interfaces. Is it true or false?
Yes, it is true. Like classes, in C#, structures can implement one or more
interfaces.
9) What is a static constructor?
Static constructors are introduced with C# to initialize the static data of
a class. CLR calls the static constructor before the first instance is created.
The static constructor has the following features:
- No access specifier is
required to define it.
- You cannot pass parameters in
static constructor.
- A class can have only one
static constructor.
- It can access only static
members of the class.
It is invoked only once, when the program execution
begins.
10)What is Static Method?
It is possible to declare a method as Static provided that they don't attempt to access any instance data or other instance methods.
11)What is New modifiers?
The new modifiers hides a member of the base class. C# supports only hide by signature.
12)What is Sealed modifiers?
Sealed types cannot be inherited & are concrete.
Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can't be applied to static members.
Sealed members are allowed in sealed and non-sealed classes.
13)What is Protected access modifier in C#?
The protected keyword is a member access modifier. It can only be used in a declaring a function or method not in the class ie. a class can't be declared as protected class.
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declare this member. In other words access is limited to within the class definition and any class that inherits from the class
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.
14)What is Internal access modifier in C#?
The internal keyword is an access modifier for types and type members ie. we can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly.
15)What is a private constructor? Where will you use it?
When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.If you declare a Constructor as private then it doesn’t allow to create object for its derived class, i.e you loose inherent facility for that class.
Class A
{
// some code
Private Void A()
{
//Private Constructor
}
}
Class B:A
{
//code
}
B obj = new B();// will give Compilation Error
16)Can we declare private class in a Namespace?
No. If you try to create a private class in a Namespace, Compiler will throw a compile time error “Namespace elements cannot be explicitly declared as private, protected, or protected internal”.
17)What is Early binding and late binding?
Calling a non-virtual method, decided at a compile time is known as early binding. Calling a virtual method (Pure Polymorphism), decided at a runtime is known as late binding.
18)Difference between sealed and static classes
sealed classes:
1)we can create their instances, but cannot inherit them
ex:
sealed class demo
{
}
class abc:demo
{
--Wrong
}
2)They can contain static as well as nonstatic members.
static classes:
1)we can neither create their instances, nor inherit them
ex:
static class Program
{
}
2)They can have static members only.
19)Static methods can not use non static members. True or False.
True
20)Static datamembers should be initialized inside the constructor. True or False.
False. Static datamembers should not be initialised inside constructor.
21)Why can't you specify the accessibility modifier for methods inside the interface?
you are not allowed to specify any accessibility, it's public by default.
22)Can you allow class to be inherited, but prevent the method from being over-ridden?
Yes, just leave the class public and make the method sealed.
23)Can you prevent your class from being inherited and becoming a base class for some other classes?
Yes, that's what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It's the same concept as final class in Java.
24)Can you override private virtual methods?
No, you cannot access private methods in inherited classes.
25)Can you declare the override method static while the original method is non-static?
No, you can't, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.
26)What does the keyword virtual mean in the method definition?
The method can be over-ridden.
27)Can we have Sealed Method in abstarct class ?
Looking at first site the The Keyword Sealed & Abstract are contradictory to each other..In simple terms we can Say Answer is NO.Look the code below
using System;
abstract class A
{
public abstract void Hello();
public sealed void Hi();
}
when we will complie the code.. we will get the Compile time Error as below
'A.Hi()' cannot be sealed because it is not an override..
But the Crux is We can have Sealed methods in abstract class when the abstract class is Dervided class .. for Eg.
using System;
class A
{
public virtual void Hello()
{
Console.WriteLine(" Say Hello");
}
}
abstract class B : A
{
public sealed override void Hello()
{
Console.WriteLine(" Say Hi");
}
}
class C : B
{
}
class Demo
{
public static void Main()
{
C c1 = new C();
c1.Hello();// Output is Say Hi
}
}
28)Can we have an Abstract class without having any abstract method ?
Yes we can have Abstract class without having any abstract method ..
See the code below
using System;
abstract class A
{
public void Hello()
{
Console.WriteLine(" Say Hi");
}
}
class B:A
{
}
class Demo
{
public static void Main()
{
B b1 = new B();
b1.Hello();
}
}
// Output is Say HI
the class A is abstract class.. but it does not have any abstract methods..
29)Can we define Method Overloading in different classes?
No,we can not define Method Overloading in different classes.It must be on the same class.
As we know that Function Overloading is defined in the same class.
We can understand it by an example:-
Below function are an example of Overloaded Constructor,which are defined on the same class.
public class Person
{
public Person()
{
}
public Person(int person_id)
{
}
}
30)Can Function Overriding be defined in Same Class?
No,Function Overriding can only be defined in different class because it treated as Base and Derived or parent-child relationship.
We can understand it by an example:-
public class Base_Class
{
public Base_Class()
{
}
public virtual void display()
{
Console.WriteLine("base class");
}
}
public class Derive_Class:Base_Class
{ public Derive_Class()
{
}
public override void display()
{
Console.WriteLine("derive class");
}
}
31)What are the advantages of partial classes?
Following are the advantages of Partial classes:-
1). We can separate User Interface part code and business logic code so that it is easy to read and understand by anybody.
2). More than one developers can work at a time.
3). As Partial classes can be separated then it can again be reunited of compilation.