Sunteți pe pagina 1din 16

If the Function has same parameter but different return type (int and float), Is it a overloading? 1. Yes 2. No 3.

None Here, return type (int and float) is different. It's not overloading. Overloading will return same return type. Which of these terms defines the hiding of an object's details from the other program? 1. 2. 3. 4. Encapsulation Abstraction Member hiding None

Encapsulation defines the hiding of the object's details from the other program. private access modifier is used to provide the security. Can we have an Abstract class without having any abstract method? Yes we can have Abstract class without having any abstract method. 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

Page 1 of 16

the class A is abstract class.. But it does not have any abstract methods. Can we have Sealed Method in abstract class ? Looking at first site the Keyword Sealed & Abstract are contradictory to each other.In simple terms we can Say Answer is NO... 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 Derived class. For e.g. 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()
Page 2 of 16

{ C c1 = new C(); c1.Hello();// Output is Say Hi } } Define OOPS. What are its benefits? Object Oriented Programming Stuctures: It is a programming methodology in which the programs are organized as collections of objects. Each object represents an instance of some class. The classes can be interrelated to each other through inheritance OOPS revolves around these concepts: 1) Class 2) Object 3) Inheritance 4) Polymorphism 5) Abstraction 6) Encapsulation Advantages: 1)Code reusability: define a class and n number of objects implement the class logic: example: Button class and n number of Button objects

2)Inheritance : Eliminates redundant code and extend the use of existing classes. Example: 1) we can create our own Textbox by inheriting from the Textbox class. 2) We can inherit one Windows From into another. 3) Encapsulation: The programmer can hide the data and functions in a class from other classes. It is accomplished through modifiers like private, protected, protected internal.

4)Easy to Maintain and Upgrade: If we want to make changes in a class, we can make them and save the changes in the .dll This .dll can easily be updated in the client by using Update Reference. 5) Polymorphism provides us with methods extensibility. we can have different methods with same name, but with different kinds of behavior ex: Console.WriteLine("welcome"); Console.WriteLine(100);
Page 3 of 16

6) Abstraction allows us to define a common definition of a base class that multiple derived classes can share. For example, create an abstract class bank with simple interest function this function will be implemented in the derived classes of the bank class. Ex: 1) icici class will have its own simple interest. 2) ABC class will have its own simple interest. Icici and ABC both are child classes of bank class. Difference between a Class and an object Class: 1)It is a datatype that contains the programming logic. 2)Class is visible in the source code and resides in hard disk. 3)Class is like a template or blueprint of the object. It implements reusability, encapsulation, inheritance example:Button is a class with properties like Text,BackColor, events like click, methods like Focus Object: 1)it is a chunk of memory that implements the class logic. 2) Object is in the RAM and not visible in source code. 3) It is the real world implementation of the class. Each object has its own copy of data. Example: Button1, Button2 are the objects of Button class. What are the different ways a method can be overloaded? Different parameter data types, different number of parameters, different order of parameters. example: int area(int a, int b) { return a*b; --different number of parameters } int area(int b) { return a*a; }
Page 4 of 16

--parameter return types int calc(int a) { return a; } double calc(double b) { return b*5; } Differences between a structure and class? Structure: 1)It is a Value Type 2)Its variable directly contains the data on the stack 3)Each structure variable has its independent copy of data. 4)One structure cannot inherit other 5)They do not have destructors 6)They do no have explicit parameterless constructors 7)we cannot put sealed /abstract modifiers before the structures. 8)Easier memory management 9)examples: int, short,long,DateTime, Point (predefined) Uses: Structures are typically used for handling small amounts of data or where inheritance, overriding is not required example: int a=100;

Class 1)It is a reference Type 2)Its variable has references to the data(data is stored in the object created in the heap) . 3) Two Class variables can refer to the same object 4) One class can inherit the other (unless the class is sealed/static) 5) Classes have destructors 6) They can have explicit parameter less constructors
Page 5 of 16

7)Sealed/abstract modifiers can be put before classes. 8) Comparatively Difficult memory management 9) example: SqlConnection, DataView (predefined classes) Classes are typically used where inheritance, overriding is required or we need to create objects capable of handling large data example: DataSet, ArrayList can handle large data.

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. Default Access modifiers in C#? An enum has default modifier as public A class has default modifiers as Internal. It can declare members (methods etc) with following access modifiers: public internal private protected internal An interface has default modifier as public A struct has default modifier as Internal and it can declare its members (methods etc) with following access modifiers: public internal private
Page 6 of 16

A methods, fields, and properties has default access modifier as "Private" if no modifier is specified. What is "this" pointer? This pointer is a pointer which points to the current object of a class. this is actually a keyword which is used as a pointer which differentiate the current object with global object. What is Abstract Class? Abstract class is a class that cannot be instantiated, it exists extensively for inheritance and it must be inherited. There are scenarios in which it is useful to define classes that are not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classs abstract classes. Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods. Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract.

Questions on Abstract class are very frequently asked in interviews :) Apart from interviews Abstract class is also very important to know when you are designing or working on a real time applications that needs proper design. I am not expert in this however trying to explain what I know out of my limited knowledge. This article tries to cover Abstract class, Abstract method, Abstract property and difference between abstract method and virtual method. Abstract class is a class that cannot be instantiated. To use it you need to inherit it. This class can be used as a base class where you can define certain method that must be implemented in derived class (Class that is going to inherit it) along with that you can also define certain methods that is frequently used and can be directly used by Derived class or overridden in the derived class if needed. In the abstract class, you can define following: 1. Normal property - this property is similar to any other property we define in a normal class 2. Abstract property - this will have only get and set accessor but no implementation. 3. Normal method - this method is similar to any other method that you define in a normal class 4. Abstract method - this will not have any implementation 5. Virtual method - this will have implementation but can also be overridden in the derived class to provide additional logic or completely replace its logic
Page 7 of 16

A constructor can be private. True or False True. A constructor can be private. We can declare a constructor as private.

Name the operators that cannot be overloaded. Sizeof,., .*, .->, ::, ?:

What is the work of a constructor? Constructor is used to initialize class members .it has same name as class.it automatically call when object of class is created. It cannot have return type. Static datamembers should be initialized inside the constructor. True or False. False. Static datamembers should not be initialised inside constructor. Static methods cannot use non static members. True or False? True 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. 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.

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.

Can you override private virtual methods? No, you cannot access private methods in inherited classes.

Diversities between an abstract method & virtual method?

Page 8 of 16

An Abstract method does not provide an implementation and forces overriding to the deriving class (unless the deriving class also an abstract class), where as the virtual method has an implementation and leaves an option to override it in the deriving class. Thus Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.

When to Use Abstract Classes and When Interfaces. If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members. What are Constructors ? Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization. If no constructor defined then the CLR will provide an implicit constructor which is called as Default Constructor. A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures. Constructors do not return a value Constructors can be overloaded What are the various types of Constructors Public : Accessible to All Private: Those classes in which only static members are there and you don't want there objects to be created in any class. Static: Used for initializing only the static members of the class. These will be invoked for the
Page 9 of 16

very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers. Intern: implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly (Namespace). and External What is Polymorphism? In OPPS, polymorphism(Greek meaning having multiple forms) is the ablity of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a a function, or an object to have more than one forms. In C# : Parent classes may define and implement virtual methods(Which is done using the virtual keyword), and derived classes can override them(using the override keyword), which means they provide their own definition and implementation.At run-time, when users code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code when a method of the base class is called it executes the overriden method. 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 doesnt 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

Page 10 of 16

Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an object for Class B that constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get compilation error. What is Method Overriding? How to override a function in C#? Use the override modifier to modify a method, a property, an indexer, or an event. An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override. What is Method overloading? Method overloading allows us to write different version of the same method in a class or derived class. Compiler automatically selects the most appropriate method based on the parameter supplied. public class MultiplyNumbers { public int Multiply(int a, int b) { return a * b; } public int Multiply(int a, int b, int c) { return a*b*c; } } To call the above method, you can use following code. MultiplyNumbers mn = new MultiplyNumbers(); int number = mn.Multiply(2, 3) // result = 6 int number1 = mn.Multiply(2, 3, 4) // result = 24

You can't have a overload method with same number parameters but different return type. In order to create overload method, the return type must be the same and parameter type must be different or different in numbers.

Page 11 of 16

What is overriding? Method overriding is a feature that allows to invoke functions (that have the same signatures) and that belong to different classes in the same hierarchy of inheritance using the base class reference. In C# it is done using keywords virtual and overrides. When to use Interface over abstract class?

Abstract Classes: Classes which cannot be instantiated. This means one cannot make a object of this class or in other way cannot create object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class. Abstract classes contains have one or more abstarct methods, ie method body only no implementation. Interfaces: These are same as abstract classes only difference is we can only define method definition and no implementation. When to use wot depends on various reasons. One being design choice. One reason for using abstarct classes is we can code common functionality and force our developer to use it. I can have a complete class but I can still mark the class as abstract. Developing by interface helps in object based communication. 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. BASIC CONCEPT OF OOPS: 1.OBJECTS: An object is an abstraction of a real world entity. It may represent a person,a placea number and icons or something else that can be modelled.Any data in an object occupy some space in memory and can communicate with eachother . 2.CLASSES: A class is a collection of objects having common features .It is a user defined datatypes which has data members as well functions that manupulate these datas. 3.ABSTRACTION: It can be definr\ed as the seperation of unnecessary details or explation from system requirments so as to reduce the complaxities of understanding requirments. 4.ENCAPTULATION: It is a mechanism that puts the data and function together. It is bthe result of hiding implimintation details of an object from its user .The object hides its data to de accessed by only those functions which are packed in the
Page 12 of 16

class of that object. 5.INHERITANCE: It is the relationship between two classes of object such that one of the classes ,the child takes all the relevent features of other class -the parent. Inheritance bring about reusablity. 6.POLYMORPHISM: polymorphism means having many forms that in a single entity can takes more than one form.Polymorphism is implemented through operator overloading and function overloading. 7.DYNAMIC BINDING: Dynamic binding is the proces of resolving the function to be associated with yhe respective functions calls during their runtime rather than compile time. 8.MESSAGE PASSING: Every data in an objest in oops that is capable of processing request known as message .All object can communicate with each other by sending message to each other

* interface contains methods that must be abstract; abstract class may contain concrete methods. * interface contains variables that must be static and final; abstract class may contain non-final and final variables. * members in an interface are public by default, abstract class may contain non-public members. * interface is used to "implements"; whereas abstract class is used to "extends". * interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance. * interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces. * interface is absolutely abstract; abstract class can be invoked if a main() exists. * interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces. * If given a choice, use interface instead of abstract class. What is an Abstract Class? An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

Page 13 of 16

What is an Interface? An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn t support multiple inheritance, interfaces are used to implement multiple inheritance. Both Together When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface. When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes. There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:

Difference between abstract class and interface? (1)An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods. (2)An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation. (3)An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class. (4)A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
Page 14 of 16

(5)Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces. (6)Abstract classes are faster than interfaces. 1. Abstract class is a class which contain one or more abstract methods which has to be implemented by sub classes. Inteface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method defination for all the methods.2. Abstract class is a Class prefix wtih a abstract keyword followed by Class definaton. Interacace is a Interface which starts with interface keyword.3. Abstract class contatins one or more abstract methods. where as Interface contains all abstract methods and final declarations.4. Abstract class contains the method defination of the some methods. but Interface contains only method declaration no defination provided.5. Abstract classes are useful in a situation that Some general methods should be implemented and specialization behaviour should be implemented by child classes. Interafaces are useful in a situation that all properties should be implemented we can use this scenario
What is the difference between a constructor and a method? A: A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers. A: public : Public class is visible in other packages, field is visible everywhere (class must be public too) private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature. protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature. default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.

Q: What is static in java? A: Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with

Page 15 of 16

a nonstatic method. In other words, you can't change a static method into an instance method in a subclass. What does it mean that a method or field is "static"? A: Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work out is a static field in the java.lang.System class.

Q: What is final? A: A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).

Page 16 of 16

S-ar putea să vă placă și