Você está na página 1de 37

OOP Fundamentals and Interview Questions www.DotNetFunda.Com www.Questpond.

Com
In case you are having issues regarding audio and video please contact at questpond@questpond.com or you can call on the phone, these training are available as recordings later , please do not disturb during training. Please Mute your mic and mobile during training. Ask your question using the Q and A tab in live meeting. Speak one at a time so that every one is benefited.

Agenda
A word about www.dotnetfunda.com Introduction What's the problem with functional programming ? OOP's Fundamentals Abstraction Encapsulation Inheritance Polymorphism Acces*s modifiers Aggregation and Composition Implementing OOPS in tiered architecture Most asked Inter*view questions in OOPS

DotNetFunda.Com
DotNetFunda right from its commencement in the year 2007 has been dedicated to render qualitative services to aid the IT community on an assortment of technological facets/confronts and has been triumphant thus far. Looking at the incredible applauds from our users and visitors across the globe on the services DotNetFunda has offered over the years; we are committed to serve even better now. We call you to help us in that direction and please participate more and more in the website; either by asking questions, answering in the Forums or writing articles, submitting interview questions and code snippets.

Wishing you all the very best and a very good Luck.

Whats the problem with Functional Programming ?


Problem of Functional programming

R eusability E xtensibility S implicity M aintainability

RESM

Different copies of same code in both the UI Increase in redundant code

New Functions created rather than extending

Extensibility and simplicity Issues

Reusability

Extensibility

Maintainability

Simplicity

Can you define OOP and the 4 principles of OOP ?

Its a design philosophy where we visualize in terms of self sustained objects.

Reusability Classes and object Extensibility Inheritance ,aggregation and composition Simplicity Abstraction , encapsulation and polymorphism Maintainability All the above 3 combined helps us to maintain the code better.

Abstraction Encapsulation Polymorphism Inheritance

Reusability

Extensibility

Classes & Objects

Inheritance aggregation Composition

Abstraction Encapsulation Polymorphism

All the above combine to make the code maintainable

Simplicity Maintainability

Summarizing definition

Remember the word APIE


Allows complex world to represented in a simplified manner by exposing only the necessary properties. For instance a color can be abstracted by RGB

Abstraction

Encapsulation

Process of hiding inner details from the external world with that making the object interface simple.

Inheritance Polymorphism

They are used to depict parent child relationship. So we can have a class with just set and get , we can then inherit and create a new specialized class which does database operations.

We want a object to play different role depending on different situations. For instance we can class which does database operations on SQL Server and Oracle. Depending on connection string it will MORPH its role to do SQL operations or Oracle operations. Poly means many and morph means change

What are classes and objects ?

Addresses reusability

Classes and objects address reusability.

Think in term of objects

Rather than thinking in terms of functions think in terms of objects.

What is a class ?

A class is simply a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

What is Object ?

Its an instance of a class. Class is brought live by creating objects. An object can be considered a "thing" that can perform activities. The set of activities that the object performs defines the object's behavior.

Class

Class used as object

Object

Same class reused in both UIs by creating objects

What is Inheritance ?

What is Inheritance ?

Addresses Extensibility

Inheritance addresses the problem of extensibility.

By using inheritance we can create a fresh child class which has all the qualities of parent plus some extra new Parent child qualities of himself also. relationship

New Functions created rather than extending

Problems Extensibility and simplicity Issues

clsProduct class is extended and clsProductDicount class is created using inheritance.

Colon represent inheritance

What is Polymorphism , overloading , overriding and virtual ?

What is Polymorphism , overloading , overriding and virtual ?

By using polymorphism you can have more simplified interfaces and neat code. Addresses Simplicity

'poly' means many, 'morphism' means forms. Its a ability of object or method to take different forms as per situation. Meaning of polymorphism

This will invoke the calculation of total cost with out discount.

Both the method names are same but they act differently as per inputs This will invoke the calculation of total cost with discount.

Two types of polymorphism

In Static Polymorphism the method to be called is decided at compile-time only. Method Overloading is an example of Static Polymorphism. Static Polymorphism

In dynamic polymorphism a call to a overridden function is resolved during runtime. Dynamic polymorphism is acheived using inheritance and overriding.
Dynamic Polymorphism

Static polymorphism is achieved by using method overloading

This will invoke the calculation of total cost with out discount.

Both the method names are same but they act differently as per inputs This will invoke the calculation of total cost with discount.

Parent Product class

Virtual keyword necessary to override the method

Class inherited and getTotalCost overridden with new functionality Inherited Child Product class

Override keyword necessary to define new implementation

Dynamic polymorphism is achieved by using overriding


objProduct = new clsProduct(); objProduct.getTotalCost(intQty, intPerProductCost,"INR") This invokes the parent class code

clsProduct objProduct;

This invokes the child class code objProduct = new clsProductWithDiscount(); objProduct.getTotalCost(intQty, intPerProductCost,"INR")

Can you explain encapsulation and abstraction ?

Encapsulation means hiding complexity clsProduct obj = new clsProduct(); Obj. IsQuantityGreater (intQty);

Extra exposed functionalities.

Obj. isProductCostZero (intPerProductCost);


obj.PerProductCost = intPerProductCost; obj.Quantity = intQty; Complication encapsulated int totalCost = obj.getTotalCost(10,100);

clsProduct obj = new clsProduct();


obj.PerProductCost = intPerProductCost; obj.Quantity = intQty;

Private: Only members of class have access.

Public: - All members have access in all classes and projects

Protected:- All members in current class and in derived classes can access the variables.

Friend (internal in C#):- Only members in current project have access to the elements.

Protected friend (protected internal in C#) :- All members in current project and all members in derived class can access the variables.

Abstraction, difference between abstraction and encapsulation?

Abstraction means show only what is necessary.

Abstraction and Encapsulation both compliment each other.

Abstraction is achieved through encapsulation

Abstraction is achieved through encapsulation..Abstraction solves the problem in the design side while encapsulation is the implementation.

What is an abstract class ?

Whats a abstract class ?


Abstract classes represent base classes, and we can not create objects of these classes. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'. An abstract class means that, no object of this class can be instantiated, but can make derivations of this.

An abstract class can contain either abstract methods or non abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
An abstract class cannot be a sealed or a static class.

An abstract class cannot be a sealed class.

An abstract method cannot be private.

The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.

An abstract member cannot be static.

Define Interface and Whats the difference between abstract and interface ?

Whats a interface ?

An interface looks like a class, but has no implementation. It only contains definitions of events, indexers, methods and/or properties. The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined. What are interfaces ?

They help decoupling Enforce standardization of vocabulary So what good are empty methods

Interface keyword is used to define interfaces.

Interface can not have field variables.

Interfaces can not have implemented functions and methods.

All methods in a interface needs to be implemented.

Interface can be used to implement run time polymorphism.

You can implement multiple interfaces.

All interface methods are public

Whats a difference between interfaces and abstract classes ?


An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.

Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.

Defining an abstract class with abstract members has the same effect to defining an interface.

Questions

Você também pode gostar