Archive for the ‘OPPS’ Category
C# Language Features OOPS .NET
Quick walkthrough on C# features.
DATA TYPES
Object Types: It is parent type of all other known types in .Net. If used as an argument in Method call then passing any type to method will cause late binding assignment to that argument.
Value and Reference Type: This type categorized on the storage of value in memory. Value Type are stored in Stack. e.g type INT, DOUBLE.
Reference types are stored in heap. e.g Type String and Object.
Converting value type into reference type called as Boxing and reverse is UnBoxing.
DECLARATION
C# allows simple and component variable declaration.
ENUMRATION
It is declared as set which are mapped to integers. This type allows us to have series which can have their mapping to unique integer value inside the set.
public enum EmployeeRole{ Trainee=0, Programmer=1, Sr Programmer=2}
Arrays: This type holds list.
e.g
int [] intArray;
OPERATORS Representing symbols for operations.
e.g Arithmetic[+, _, +], Logical[||, &&], String Concatenate[&], Increment and Decrement[+=], Comparison[<=, =>], Cast[()] etc..
FLOW CONTROL Controls the flow of your programme. e.g conditional statement [if ..else, switch], loops[while, do..while, for, foreach], functions.
CLASSES Class is type which includes all types, members, methods etc. It has visibility, its lifetime, relation, inheritance, method overloading..
REFLECTION Dynamically gather information of underlined type in late bound fashion. e.g Attributes allows to attach metadata to class and its members.
INTEROPERABILITY This feature facilitates to work with other programming language and API.
COM INTEROPERABILITY: This allows to integrate legacy components with .Net classes.
INVOKING WINDOWS API: This is same as VB6 feature to invoke and use windows API. Reference dll and create wrapper functions.
[sysimport(dll="user32.dll")] private static extern int MessageBoxA(int hwind, string Message, string caption, int Type);
STRUCTURED ERROR HANDLING This is way of handling error by blocking code. e.g
Try {} Catch(){} Finally{}
C# PREPROCESSOR This feature allows programmer to do better programming like expanding code, collapsing code…
Happy programming..
Number of View :7319Understanding the Object Oriented Programming
In .net the object plays role everywhere even if you directly code or do not object has its existence.
I have small program which was Interview Question at Microsoft.
C# code and understand the life of object.
Open Visual Studio -> New Project->Select c# and then Select Console Application
Name project – ObjectLifeCycle
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ObjectLifeCycle { public class log { public Int32 i; public void TestA(log obj) { obj.i = 100; } public void TestB(log test) { test = new log(); test.i = 1000; } } class Program { static void Main(string[] args) { log o = new log(); o.i = 10; Console.WriteLine(o.i.ToString()); o.TestA(o); Console.WriteLine(o.i.ToString()); o.TestB(o); Console.WriteLine(o.i.ToString()); } } }
You can directly paste this code in your .cs file and execute the code , you should see output as followed
10
100
100
So the object of class log which has only one variable in it and methods which has argument passed as its own object is having different scope in different assignment.
In Main Method object is initialized with values first time so first writeline statement gives 10.
Second method call TestA with object o is passed. Inside the method their is just reassign of value to int as 100 so the value 10 is replaced to 100 for object o which is still having same reference.
Third method call TestB in which again object o is passed which had value of int as 100. Now inside method new object initialization happens that means new object o for which int value is 1000.
Now you must be wondering third value is not 1000, this is because TestB created new object with new memory allocation. So test object scope finished ones it came out of the method. Since the Writeline method is calling value available in object o it has given 100.
Lets do one more change on line 35
o.i = o.TestB(o);
In above case what is happening is we are copying return value of int from TestB method and assign it to object o.i int value. of course TestB return type has to be changed from void to int.
It should return
10
100
1000
For more details http://msdn.microsoft.com/en-us/library/fa0ab757%28v=vs.100%29.aspx
Happy programming….
Number of View :2701Basic Standard Action Tags In JSP Tutorial With Examples JSP Technical Advices
Standard Action Tags In JSP
Standard Action Tags In JSP are meant for performing some action at run time. As the name of the tag itself suggests these tags are used for run time behavior. Unlike scripting tags which are meant for generating related servlet code at the time of translation,Standard Action Tags In JSP generates code for run time behavior.
Examples of Standard Action Tags In JSP are as follows :
- <jsp:include file= ”AnotherJsp.jsp”/>
- <jsp:forward file=”AnotherJsp.jsp”/>
- <jsp:useBean id=”obj” type=”SomeBean” class=”SomeBean” scope=”request”>
- <c:set var=”attrName” value=”attrVal” scope=”request”/>
There are so many other tags as well amongst which these are some and important.
Let us take these tags individually as see how do they contribute for Standard Action Tags In JSP
- <jsp:include file= ”AnotherJsp.jsp”/> : This tag is used for run time inclusion of another JSP page in an existing one. This inclusion is not similar to inclusion with include directive. Include directives are meant for static inclusion. This means that the code from the included JSP will be included int the including JSP at the translation time. But in the case of <jsp:include file= ”AnotherJsp.jsp”/> tag a run time call to the included JSP is done and the response from the included JSP is added to the response of the including JSP.
- <jsp:forward file=”AnotherJsp.jsp”/> : This tag is used for forwarding the request to another JSP at run time. The difference here from run time or dynamic inclusion is that here the response from the forwarded JSP only goes to the user and the response from the first JSP from where the forward happens gets flushed out.
- <jsp:useBean id=”obj” type=”SomeBean” class=”SomeBean” scope=”request”> : This tag is used to create or fetch a java bean object ,set or get its property value/s and retrieve or set this java bean object in certain scope.
- <c:set var=”attrName” value=”attrVal” scope=”request”/> : This tag is used to set an attribute in a particular scope.This is an important tag as our MVC applications rely heavily on attributes
How To Get Servlet Init Parameters In JSP
How To Get Servlet Init Parameters In JSP
We know that we can declare init parameters for JSP file in web.xml file as we do in the case of servlet.The entry in the web.xml file will be as follows:
<servlet> <servlet-name>myJSP</servlet-name> <jsp-file>/MyJsp.jsp</jsp-file> <init-param> <param-name>paramName</param-name> <param-value>paramValue</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>myJSP</servlet-name> <url-pattern>/MyJsp.jsp</url-pattern> </servlet-mapping>
Now comes the question that how can be access or retrieve those initialization parameters in the JSP page.Among the life cycle methods of a JSP page jspInit() method is one of the methods which can be overridden.The jspInit() method is called by the init() method of the servlet which is the result of the translation of the JSP page.To access the initialization parameters is one of the cases where we override the jspInit() method.When we need to declare a method we use the declarative tag as follows :
<%! public void jspInit() { ServletConfig sConfig = getServletCOnfig(); String str= sConfig.getInitParameter(“paramName”); getSerrvletContext().setAttribute(“name”,str); } %>
Here we have overridden the jspInit() method in the declarative tag.Since this method is called by the init method of the translated servlet code we can access the servlet config object with the help of getServletConfig() method as it is passed by the server to the servlet through init() method. This method (jspInit()) will appear as an overridden method in the translated servlet.The point should be noted here that init() method in the servlet calls this jpInit() life cycle method of the jsp page so it can access the servlet config object.The life cycle method _jspService() is not permitted to be overridden .Initialization parameters for the the JSP page can be understood as the configuration parameters to be accessed by the translated servlet.After all a JSP page is a servlet at the end of the day and it is the translated servlet only which processes the requests from the clients.
Number of View :6747
Declare Servlet Init Parameters For JSP Page In web.xml File JSP Initialization FAQ
How To Declare Initialization Parameters For JSP Page In web.xml file
Servlet initialization parameters are declared inside web.xml page and are used to pass the value to the servlet as a part of configuration.Now the question comes that since we do not make an entry for JSP inside web.xml page,how to pass the init parameters to the JSP page.We should remember that a JSP page is a servlet at the end of the day.A JSP is ultimately converted into a servlet then only it processes requests.Like the entry for init parameters in web.xml in case of servlet we make an entry for the JSP page as well,but with a slight difference.The web.xml file looks like this :
<servlet> <servlet-name>myJSP</servlet-name> <jsp-file>/MyJsp.jsp</jsp-file> <init-param> <param-name>paramName</param-name> <param-value>paramValue</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>myJSP</servlet-name> <url-pattern>/MyJsp.jsp</url-pattern> </servlet-mapping>
Here we can find that the same way as we declare init paramters for servlet within the servlet tags,the same way we make an entry for the JSP page so that the init parameters can be declared for that particular JSP page.The only difference is that instead of he <servlet-class> tag we use <jsp-file> tag.The url pattern for the JSP page is same as the JSP file name as we use the JSP file name as URL pattern in the request.Inside the <jsp-file> tag we use the JSP file name with .jsp extension because we need to access the JSP page and not the servlet class.
This is the way we declare servlet init parameters for the JSP page inside the web.xml file.We call it as servlet init parameters for JSP page because these init parameters are sent to the servlet which is the result of translation of the JSP page.And these are used as the configuration parameters for the translated servlet.Now the init parameters can be accessed in he JSP page code.
Number of View :6922Basic Servlet Attributes Thread Safety Interview Questions Answers Explained
Servlet Attributes Thread Safety
An attribute is used to communicate some value from one servlet to another. Depending upon the scope of the attribute we can access the attributes at parts of the application.An attribute can be set at either of the scopes as follows :
1.Application
2.Session
3.Request
Now in order to find out which attribute/s is/are thread safe,we can ponder upon with following discussion.
1.Application Attributes : An application attribute is a value set at application level. The code to set the attribute at application level is :
getServletContext().setAttribute(“name”,”value”);
As these attributes are accessible throughout the application,any servlet or any other web entity can update or get these attributes from various parts of the application. Since application attributes can be accessed or modified by any servlet in the web application,these are not thread safe.
2.Session Attributes : A session attribute is a value set at session scope. Session is used to retain user specific data. The code to set the attribute at application level is :
req.getSession().setAttribute(“name”,”value”);
When we talk about thread safety of session attributes,at one glimpse it deceives to appear as thread safe. In cases where we have the new tab feature in the modern browsers,the same session for the web application is opened in the new tab. In such case, if session attribute are updated from one tab will be reflected in the another tab with the same session. Thus we can say that session attributes are also not thread safe.
3.Request Attributes : A request attribute is a value set at request scope.he code to set the attribute at application scope is :
req.setAttribute(“name”,”value”);
As we know that each request is a unique object , whatever attribute we set at request scope cant be updated or get from another request. Thus request attributes are thread safe.
Conclusion : Thus request attributes are thread safe and application and session attributes are not.
Number of View :7661Types Of Attributes In Servlet Servlet Scopes And Attributes Saving Data In A Servlet Attributes Interview Question
Types Of Attributes In Servlet
An attribute is an object used to pass some value from one servlet to another.An attribute in a servlet can be set at various scopes depending upon the visibility in the web application.As we know that there are three scopes at servlet level in the application.Theses scopes are :
1.Request
2.Session
3.Application
An attribute can be set on either of the scopes depending on the situation we want access the attribute value either at request,session or application level.
When we want the value of an attribute o be accessed at request level we use request attributes and likewise for session and application.
Types of attributes in servlet can be explained on the basis of scope it belongs to :
1.Request Attributes
2.Session attributes
3.Application Attributes
1.Request Attributes can be set or get as demonstrated below in the code :
req.setAttribute("name","value"); req.getAttribute("name");
2.Session Attributes can be set or get as shown below :
req.getSession(false).setAttribute("name","value"); req.getSession(false).getAttribute("name");
3.Application Attributes can be set or get as below :
getServletContext().setAttribute("name","value"); getServletContext().getAttribute("name");
Now comes the question where to use what?
Request Attributes : If we want to retain or communicate a value across servlets where request does not change,we use request attributes.This can be the situation where include or forward is happening between the servlets in a web application.In such cases where we want to communicate a value across servlets we can use request attributes which when set from one servlet can be get in other servlets.
Session Attributes : In cases where user specific data has to be retained or communicated, we use session attributes.We use session attributes when we do not want a user’s data to be interfered by other users.It should be noted that these attributes can be retrieved even when redirect is happening provided the session is maintained .
Application Attributes : In cases where some value has to be set at application level and all the entities in the web application can read that value we use application attributes.
Number of View :4655
Difference Between Abstract Class And Interface In Java Abstract Class Vs Interface When To Use An Abstract Class And An Interface
Difference Between Abstract Class And Interface In Java
An abstract class in java is a class which has at least one abstract function.
An interface in java is an entity and not a class which has all its methods as abstract.We cannot have even one implementation of function or method in an interface in java.
An abstract class with all its methods as abstract is close to being an interface.But the concept for usage of both is polls apart in context.
An abstract class looks like :
public abstract class AbstractClassExample1{ abstract void abstractMethod(); // concrete methods are still allowed in abstract classes void nonAbstractMethod() { System.out.println("This is a non abstract method."); }
Here we can see that we can have both abstract and non abstract methods in abstract class.
An interface looks like :
interface InterfaceExample{ public void method1(); public void method2(); } }
Here in the interface all the methods or functions are abstract.
All the methods in interface need to be public .
Now comes the scenario ,when to use an abstract class and when to use an interface.
Scenarios where we want re usability of some of the methods or functions and some methods or functions are left for the client to implement as per their requirement calls for the use of an abstract class.In such cases client knows that some of the default implementation is already there and some part is up to them how to implement in the form of methods or functions.In such cases it logical to extend the concrete class from an abstract class.
Now comes the scenario where two coders are working for an application or software in which one calls the function or method in his module which is written by another coder.If in future the first coder who writes the method or function ,for some reason changes the function signature it will hamper the code of the coder who is calling the function.To avoid these type of issues we can use interfaces.If the person who writes the method implements it with interface and the function call is also done with the reference type interlace,then both he coders know the standard to be followed in order to write or call a method.Thus in order to follow some standard we use interfaces.
Number of View :5259