1. What is OOPS?
Object-oriented programming (OOP) is a
programming language model organized around objects rather than
"actions" and data rather than logic. Historically, a program has
been viewed as a logical procedure that takes input data, processes it, and
produces output data.
2. Basic Concepts of OOPs?
Abstraction.
Encapsulation.
Inheritance.
Polymorphism.
3. What is a class?
A set or category of things having some property
or attribute in common and differentiated from others by kind, type, or
quality.
OR
A class contains the data and
behavior of an entity. For example, the aircraft class can contain data, such
as model number, category, and color, and behavior, such as duration of the flight,
speed, and a number of passengers. A class inherits the data members and
behaviors of other classes by extending from them.
4. What is an object?
Objects are created from Classes, in C#, is an
instance of a class that is created dynamically. Object is also a keyword that
is an alias for the predefined type System.
OR
They are instance of
classes. It is a basic unit of a system. An object is an entity that has
attributes, behavior, and identity. Attributes and behavior of an object are
defined by the class definition.
5. What is Encapsulation?
Encapsulation is the packing of data and
functions into a single component. The features of encapsulation are supported
using classes in most object-oriented programming languages, although other
alternatives also exist.
It allows selective hiding of properties and
methods in an object by building an impenetrable wall to protect the code from
accidental corruption.
6. What is Polymorphism?
In programming languages and type theory,
polymorphism is the provision of a single interface to entities of different
types.
A polymorphic type is a type whose operations
can also be applied to values of some other type, or types.
7. What is Inheritance?
Inheritance is when an object or class is based
on another object or class, using the same implementation (inheriting from a
class) specifying implementation to maintain the same behavior (realizing an
interface; inheriting behavior).
It is a mechanism for code reuse and to allow
independent extensions of the original software via public classes and
interfaces.
8. What is Constructor?
A is a special method of the class that will be
automatically invoked when an instance of the class is created is called as
constructor.
Constructors are mainly used to initialize
private fields of the class while creating an instance for the class.
When you are not creating a constructor in the
class, then the compiler will automatically create a default constructor in the
class that initializes all numeric fields in the class to zero and all string
and object fields to null.
Syntax.
[Access Modifier] ClassName([Parameters])
{
}
9. Types of Constructors
Basically constructors are 5 types those are
Default Constructor
Parameterized Constructor
Copy Constructor
Static Constructor
Private Constructor
10. Define Destructor?
A destructor is a method which is automatically
invoked when the object is destroyed.
Its main purpose is to free the resources
(memory allocations, open files or sockets, database connections, resource
locks, etc.)
11. What is Inline function?
In the C and C++ programming languages, an
inline function is one qualified with the keyword inline; this serves two
purposes.
Firstly, it serves as a compiler directive,
which suggests (but does not require) that the compiler substitute the body of
the function inline by performing inline expansion,
The second purpose of inline is to change
linkage behavior; the details of this are complicated.
12. What is operator overloading?
In programming, operator overloading—less
commonly known as operator ad hoc polymorphism—is a specific case of polymorphism,
where different operators have different implementations depending on their
arguments. Operator overloading is generally defined by the language, the
programmer, or both.
13. Different between method overriding and
method overloading?
In Overriding methods it will create two or more
methods with same name and same parameter in different classes.
While overloading it will create more than one
method with same name but different parameter in same class.
14. What is this keywords?
Every instance method in every object in Java
receives a reference named this when the method is invoked. The reference
named this is a reference to the object on which the method was invoked.
It can be used for any purpose for which such a reference is needed.
15. What is super keyword?
The super keyword is a reference variable that
is used to refer immediate parent class object.
Whenever you create the instance of subclass, an
instance of parent class is created implicitly i.e. referred by super reference
variable.
16. What is an abstract class?
An abstract class is a class that is
declared abstract—it may or may not include abstract methods. Abstract classes
cannot be instantiated, but they can be sub classed.
17. What is final keywords?
The final keyword in java is used to restrict
the user. The java final keyword can be used in many context.
Final can be: variable, method, class.
The final keyword can be applied with the
variables, a final variable that have no value it is called blank final
variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be
initialized in the static block only.
18. What are the Access Modifiers in C#?
Different Access Modifier
are - Public, Private, Protected, Internal, Protected Internal
·
Public
– When a method or attribute is defined as Public, it can be accessed from
any code in the project. For example, in the above Class
“Employee” getName() and setName() are public.
·
Private
- When a method or attribute is defined as Private, It can be accessed by any
code within the containing class only. For example, in the above Class
“Employee” attributes name and salary can be accessed
within the Class Employee Only. If an attribute or class is defined without
access modifiers, its default access modifier will be private.
·
Protected
- When attribute and methods are defined as protected, it
can be accessed by any method in the inherited classes and any method within
the same class. The protected access modifier cannot be applied to classes and
interfaces. Methods and fields in an interface can't
be declared protected.
·
Internal
– If an attribute or method is defined as internal, access is restricted to
classes within the current project assembly.
·
Protected
Internal – If an attribute or method is defined as Protected Internal, access
is restricted to classes within the current project assembly and types derived
from the containing class.
19. What is the difference between arrays and
collection?
Array:
- You
need to specify the size of an array at the time of its declaration. It
cannot be resized dynamically.
- The
members of an array should be of the same data type.
Collection:
- The
size of a collection can be adjusted dynamically, as per the user's
requirement. It does not have fixed size.
- Collection
can have elements of different types.
20.
What is Method Overloading?
Creating a multiple methods in a class with
same name but different parameters and types is called as method overloading.
Method overloading is the example of Compile time polymorphism which is done at
compile time.
Eg:-
public class Methodoveloading
{
public int add(int a, int b) //two int type Parameters method
{
return a + b;
}
public int add(int a, intb,int c) //three int type Parameters with same method
same as above
{
return a + b+c;
}
public float add(float a, float
b,floatc,float d) //four float type
Parameters with same method same as above two method
{
return a + b+c+d;
}
}
21.
What is Method overriding?
Creating the method in a derived class with
same name, same parameters and same return type as in base class is called as
method overriding.
Method overriding is the example of run time
polymorphism,howits is the part of run time polymorphism i will explain in
detail.
classparentClass
{
public
virtual void Disp(inti)
{
System.Console.WriteLine("parentClass
Display" + i);
}
}
classchildClass : parentClass
{
public
override void Disp(inti)
{
System.Console.WriteLine("childClass
Display" + i);
}
}
22.
What is an Abstract Class?
- An abstract class is a class that is declared
abstract
- Abstract classes cannot be instantiated
- Abstract classes can be subclassed
- It may or may not include abstract methods
- When an abstract class is subclassed, the
subclass usually provides implementations for all of the abstract methods in
its parent class
- If a subclass doesn’t provide implementations
then the subclass must also be declared abstract.
OOPs interface vs abstract class
Interface
|
Abstract Class
|
Interface support multiple inheritance
|
Abstract class doesn't support multiple inheritance
|
Interface does not Contains Data Member
|
Abstract class contains Data Member
|
Interface does not contains Constructors
|
Abstract class contains Constructors
|
An interface Contains only incomplete member (signature of member)
|
An abstract class Contains both incomplete (abstract) and complete
member
|
An interface cannot have access modifiers by default everything is
assumed as public
|
An abstract class can contain access modifiers for the subs,
functions, properties
|
Member of interface cannot be Static
|
Only Complete Member of abstract class can be Static
|
23. What is a Static Class?
Ans: C#
provides the important feature to create static classes, there are two main
features of a static class, one is no object of static class can be created and
another is, a static class must contain only static members, then it is
important that what is the main benefit to create a static class, the main
benefit of making static class, we do not need to make any instance of this class,
all members can be accessible with its own name.
Declaration:
A
static class is created by using keyword 'Static' as shown here:
Static
class Clasname
{
//C#
}
One
more thing that is notable-within static class, all members must be explicitly
specified as static, static class does not automatically make its members
static. Static class can contain a collection of static methods.
Example:
using System;
static class Shape
{
public static double GetArea(double Width, double height)
{
return Width
* Height;
}
}
class Ractangle
{
private void GetRactangleArea()
{
Double Area;
Area
= Shape.GetArea(10, 5);
}
}
Shape
is static class, it contain staic function GetArea.Ractangle is other class and
with in GetArea function can be access without creating instance of Class Shape.
Although
a static class cannot have an instance constructor, it can have a static
constructor.
24.
What is Sealed Class?
A
sealed class is a class that cannot be inherited. Sealed classes are used to
restrict the inheritance feature of object oriented programming.
Sealed
classes:
1) Can create instances,
but cannot inherit
2) Can contain static as
well as non-static members.
Static
classes:
1) Can neither create their
instances, nor inherit them
2) Can have static members
only.
·
A
static class can only contain static members
(it is just a container for methods that do not logically belong to an instance
of any standard class)
·
An
abstract class can contain all usual kinds of members (static, abstract and
also instance)
The
key difference is that you can inherit from an abstract class,
but you cannot inherit from a static class.
Technically speaking, the .NET runtime doesn't have any notion of static classes, so the C# compiler
complies them as classes that are both abstract and sealed(meaning that you cannot inherit
from them).
So, static classes are abstract classes that are
also sealed (although this is not the
usual way to look at the problem if you are C# programmer) and contain
only static members (which is enforced
by the C# compiler).
27. Difference between Early Binding and Late
Binding?
Early
Binding
Compiler knows at compile time which function to invoke.
Most of the function calls the compiler encounters will be direct function calls.
e.g.,
|
int
sum(int a, int b) {
return a + b;
}
int
main() {
std::cout << sum(2, 3); // This is a direct function call
return 0;
}
|
Direct function calls can be resolved using a process known as early
binding. Early binding (also called static binding)
means the compiler is able to directly associate the identifier name (such as a
function or variable name) with a machine address. Remember that all functions
have a unique machine address. So when the compiler encounters a function call,
it replaces the function call with a machine language instruction that tells
the CPU to jump to the address of the function.
Late
Binding
Compiler
doesn't know until runtime which function to invoke.
In some programs, it's not possible to know which function will be called until
runtime. This is known as late binding (or dynamic
binding). In C++, one way to get late binding is to use function pointers
or the other way is the use of virtual functions in inheritance. e.g.,
|
int
add(int x, int y) {
return x + y;
}
int
subtract(int x, int y) {
return x - y;
}
int
main() {
int x, y;
std::cin >> x >> y;
int operation;
std::cout << "choose 0 for add & 1 for subtract\n";
std::cin >> operation;
int (*p)(int, int); // Function Pointer
// Set p to point to the function the user chose
switch (operation) {
case 0 : p = add;
break;
case 1 : p = subtract;
break;
}
// Call the function that p is pointing to
std::cout << "The answer is: " << p(x, y) << std::endl;
return 0;
}
|
28. What is Loose and Tight coupling?
Short Introduction Loose and Tight
Coupling
Loose Coupling means
reducing dependencies of a class that use a different class directly. In tight
coupling, classes and objects are dependent on one another. In general, tight
coupling is usually bad because
it reduces flexibility and re-usability of code and it makes changes much more
difficult and impedes testability, etc.
Tight Coupling
A Tightly Coupled Object
is an object that needs to know quite a bit about other objects and is usually
highly dependent on each other's interfaces. Changing one object in a tightly
coupled application often requires changes to a number of other objects. In a
small application, we can easily identify the changes and there is less chance
to miss anything. But in large applications, these inter-dependencies are not
always known by every programmer or there is a chance of overlooking changes.
But each set of loosely coupled objects are not dependent on each other. (Stackoverfow-Jom George)
Code
namespace
TightCoupling
{
public class Remote
{
private Television Tv
{ get; set;}
protected Remote()
{
Tv = new Television();
}
static Remote()
{
_remoteController
= new Remote();
}
static Remote _remoteController;
public static Remote Control
{
get
{
return _remoteController;
}
}
public void RunTv()
{
Tv.Start();
}
}
}
Difficulties
Tight Coupling creates
some difficulties. Here, the task of the control object, the object needs to be
able to television, the television remote control is dependent on the other
phrase. So, what's the harm of the following dependencies:
·
TV without a remote
control does not work.
·
TV changes the control
directly affected by this change.
·
The Control can only
control the TV, cannot control other devices.
Loose Coupling
Loose coupling is a design goal that seeks to reduce
the inter-dependencies between components of a system with the goal
of reducing the risk that changes in one component will require changes in any
other component. Loose coupling is a much more generic concept intended to
increase the flexibility of a system, make it more maintainable, and makes the
entire framework more "stable".
Code
public interface IRemote
{
void Run();
}
public class Television : IRemote
{
protected Television()
{
}
static Television()
{
_television
= new Television();
}
private static Television _television;
public static Television Instance
{
get
{
return _television;
}
}
public void Run()
{
Console.WriteLine("Television
is started!");
}
}
We need a managing class
that will produce an instance. The instance is generated from the implemented
class. The Management Class constructor needs an interface which implements to
any Class.
Code
public class Remote
{
IRemote _remote;
public Remote(IRemote remote)
{
_remote
= remote;
}
public void Run()
{
_remote.Run();
}
}
Usage
class Program
{
static void Main(string[]
args)
{
Remote remote
= new Remote(Television.Instance);
remote.Run();
Console.Read();
}
}
Advantages
It will save you a lot
of time for any project that isn't trivially small, where I define trivially
small as less than a couple thousand lines of code (depending on the language).
The reason is that once you get past super small projects, each change or
update gets harder the more tightly coupled it is. Being loosely coupled
enables you to keep moving forward, adding features, fixing bugs, etc.
At a certain point, I
think any program becomes a nightmare to maintain, update and add on to. The
more loosely coupled the design is, the further that point is delayed. If it's
tightly coupled, maybe after about 10,000 lines of code it becomes
unmaintainable; adding features becomes impossible without essentially
rewriting from scratch.
Being loosely coupled
allows it to grow to 1,000,000 - 10,000,000 lines of code while still being
able to make changes and add new features within a reasonable amount of time.
These numbers aren't meant to be taken literally as they're just made up, but
to provide a sense of where it becomes helpful. If you never need to update the
program and it's fairly simple then sure, it's fine to be tightly coupled. It's
even okay to start that way but understand that when it's time to separate
stuff out, but you still need experience writing loosely coupled code to know
at what point it becomes beneficial.; (From Stackoverflow-Davy8)
·
It improves test-ability.
·
It helps you follow the
GOF principle of Program to Interfaces, not implementations.
·
The benefit is that it's
much easier to swap other pieces of code/modules/objects/components when the
pieces aren't dependent on one another.
·
It's highly changeable.
One module does not break other modules in unpredictable ways
Summary
As with all OO designs,
there are trade-offs you have to make; is it more important for you to have
highly modular code that is easy to swap in and out? Or is it more important to
have easily understandable code that is simpler? You'll have to decide that.
29. Why do we need to use
Abstract Classes Interfaces?
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.
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 a 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 inheritances, interfaces are used to
implement multiple inheritances.
30. What is virtual function?
A virtual function is the member function of a class that
can be overridden in its derived class. It is declared with a virtual keyword. A virtual function call is resolved at run-time (dynamic binding) whereas the
non-virtual member functions are resolved at compile time (static binding).
31.
When would you use an Abstract class?
Use Abstract class
when there is an 'IS-A' relationship between classes. For example, Lion is an
Animal, Cat is an Animal. So, animals can be an abstract class with a common
implementation like no. of legs, tail, etc.
32. Why do we need to use Abstract Classes
& Interfaces??
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 sub classes. In simple words, it is a kind of
contract that forces all the sub classes to carry on the same hierarchies or
standards.
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
sub classes 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.
Main difference
between abstract classes and interface is that a abstract class provides
default implementation for which sub classes does not need any implementation
and can be used as it is. While interfaces force all sub classes to implement
its all methods. Abstract class method can have different access modifier like
private and public while interface will always have public. Abstract can have
member variables however interface cannot have.
if you are building
car of the same model with different engine types -Petrol, Diesel, CNG and
Hybrid..all other parts chasis,Body,Wheels, dashboard remain same and these are
implemented in default implementation of the Car abstract class because these
parts do not change while the engine abstract method in the abstract Car class
lets subclasses like PetrolCar, DieselCar,LPGCar and HybridCar to provide its
own implementation.This is an example of abstract class.
Use interface when
you are building components which will be distributed over many clients
machines.
Abstract classes are
faster than interfaces.
33. Types of classes in C#?
Ans: Types of classes in C#.Net:
• Abstract Class (sometimes called a
Pure Virtual Class)
• Partial Class
• Sealed Class
• Static Class
Abstract Class:
An Abstract Class
means that, no object of this class can be instantiated, but can make derivation
of this. It can serve the purpose of base class only as no object of this class
can be created.
Abstract Class is
denoted by the keyword abstract.
Partial Class:
This special type of
class called "Partial Class" is introduced with .Net Framework 2.0.
Partial Class allows its members – method, properties, and events – to be
divided into multiple source files (.cs). At compile time these files get
combined into a single class.
Partial Class is
denoted by the keyword partial.
Some do's and don'ts
about partial class:-
• All the parts of a partial class must
be prefixed with the keyword partial.
• Accessibility, signature etc. must be
same in all parts of the partial class.
• You cannot sealed one part of the
partial class. In that case entire class in sealed.
• If you define any part of the partial
class abstract, entire class will become abstract.
• Inheritance cannot be applied to a
part of partial class. If you do so, it applies to entire class.
Sealed Class:
A sealed class is a
class which cannot be inherited. A sealed class cannot be a base class. The
modifier abstract cannot be applied to a sealed class. By default, struct
(structure) is sealed. It is the last class in hierarchy. To access the members
of a sealed class, you must create objects of that class.
Sealed Class is
denoted by the keyword sealed.
Static Class:
A Static Class is one
which cannot be instantiated. The keyword new cannot be used with static
classes as members of such class can be called directly by using the class name
itself.
Following are the
main characteristics of a static class:-
• A Static Class can only have static
members.
• A Static Class cannot be
instantiated.
• A Static Class is sealed, so cannot
be inherited.
• A Static Class cannot have a
constructor (except static constructor).
Static Class is
denoted by the keyword static.
34. Difference between structure and class?
Ans: 1) Structure: - In
structure have a by default public. In class have a by default private.
2) Structure cannot be inherited. But
class can be inherit.
3) There is no data hiding features comes
with structures. Classes do, private, protected and public.
4) A structure can't be abstract, a class
can.
5) A structure is a value type, while a
class is a reference type.
6) A structure is contain only data
member, but class contain data member and member function.
7) In a Structure we can't initials the
value to the variable but in class variable we assign the values.
8) Structure are value type, they are
stored as a stack on memory. Whereas class are reference type. They are stored
as heap on memory.
35. Why do we use a constructor
in c#?
Constructor is not like simple methods.
It is called every time when the object of that particular class is created.
You don't need to call it explicitly.
There are something’s that we need to do
immediately when the object is created, for instance when you create a GUI kind
of thing you want to set many properties on the time of creation like size of
window etc. Another benefit of constructor is security of class. You cannot
create a object unless you know the right perimeters of constructor.
In other words, If you create a new Object of MyClass it
will automatically call the constructor - you can initialize all members within
it, and be sure that this object´s members are all initialized.
Generally:
A constructor is always called once when
you create a new Object of this class, and you can´t call it manually.
And don´t do "real" work in a
constructor, as it will slow down the creation of objects of this class - only
initialize your class/members there.
You can also use different constructors,
depending on your needs - but if you create a constructor, there is no more
default constructor!
No comments:
Post a Comment