My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label hiding. Show all posts
Showing posts with label hiding. Show all posts

Saturday, May 13, 2017

Method hiding Vs overriding in C# : New feature that can be cause of big troubles later

In here, I tried to show a new feature in C# or Visual studio languages. This is not in Java, and I have proactively tested that ;)

Example: Class A has a Print method; class B inherits from class A and implements the Print method as well. Now Print method will be overridden. Simple!

But now, test carefully that what happens if you change the Print method signature in class B. If you add the new keyword there, a behavior changes. In this case the method does not overrides. In fact it will hide the method. For references, i have the Github repository up and running: https://github.com/Anwar-Faiz/Method-Hiding-Demo

Most Important: In normal object calls, this one will not be caught. The behavior difference is seen when you make an object with parent class variable.


Let us start with defining some classes, and see a working example:

class A
    {
        public string Print()
        {
            return "A";
        }
    }

    class B : A
    {
        public string Print()
        {
            return "B";
        }
    }

    class A2
    {
        public string Print()
        {
            return "A2";
        }
    }

    class B2 : A2
    {
        public new string Print()
        {
            return "B2";
        }
    }

Now let us write a program to check the behavior in both ways. See attached screenshot:



The result of the run is as follows:


Demo of overriding...
 a.Getname : A
 b.Getname : B
 a2.Getname : A2
 b2.Getname : B2

Demo of method hiding...
 x.Getname : A
 y.Getname : A2



Now this clearly shows that in the second case, the method of child class becomes hidden!


Issues seen with this new feature in Visual Studio C#:
What I foresee here are a couple of problems and i am describing the same:
1. Programmers of other languages reading this part of code, will have a tough time understanding what is happening.
2. There is a huge difference between the way overriding works and this method hiding functionality behaves, this new feature may prove to be step going back to Non-OOP days!
3. More importantly, the function written in the child class, gets of no use now. It becomes hidden, in a way.
4. I further see that it is default behavior in C#. This means that you do not even need to write the new keyword. Also, if the method on the base class is marked as virtual, and you forget to mark the method on the inheriting class with override, you might land in method hiding.
5. If a bug is encountered, it will be hard to find that the trouble is cause of a new keyword.


Proposed solution to team Microsoft working on this:
As we know, since already introduced, it will be tough to remove it. Particularly, because many have already started using in their projects. I know the team Microsoft spends a lot of effort on giving backwards compatibilty, and they are really good at that!.So, the best option is to introduce the Warning on Visual Studio Editor. You can also raise a compile time warning.

PS: You can download working repository of code sample at:
https://github.com/Anwar-Faiz/Method-Hiding-Demo


Have a happy coding and testing guys!
@Anwar Faiz


Thursday, January 19, 2012

File access problem: PHP htaccess

Once I was asked a question related to File access over the web server.
The question was asked as follows...
"I have built an information maintenance system where files can be uploaded. Anyone can view those files.
But, the problem is when a file is viewed in the browser at that time if someone just copy the URL in some text file after that any body from any where can view that file by just pasting that URL in the browser's address bar.

How to prohibate this thing. PLZZZZZZZZZZZZZZZZZZZ help me...............

its verrrrrrrrrrrrrrrrry serious problem.............."

It was answered by a web expert Mr. Longor (his user id;) )as follows
"There's lots of ways to do this...the one that springs to mind is have a simple referer check at the top of your file-page and if the referer is not coming from your site, then simply redirect the browser to an error page of your choosing.

You can also use .htaccess on linux servers to accomplish this type of thing."


I answered as follows:
"yes neil dear
that was to appen
actually u r asking a questionn of basic security

what u can do is to change the access configurations from .htaccess file

or else use MySql at the back end and save those info in a database (password protected)

This will surely solve the problem!!"


To the answer by previous web expert I replied as follows...
"langsor gave a good solution

Bt basic problem is that the solution posted by him works only if your pages are php. But they may be simple text files also. Because i feel u r using flat files as your database.

so .htaccess solution as adviced by him and me both are correct.

and if in case your pages are php. then well good and fine. go on with the method of longor."


And then the person who asked the question clarified more as follows...
"Sorrrrrrry everyone,

actually i did not clear the problem to you, My files are in ".pdf" format...

Suppose,

http://localhost/information/sample.pdf

can be viewd ....... no login is required to do so..

if I cross the browser and after that in a new browser if I paste this URL then it should not be viewd......."



And then my final answer was
"ya i suppose now u gave correct explanation of ur problem

now only possible things are .htaccess

i need others to speak on this

Anyone to rescue neil of his situation??

easy bit... neil
lets see who solves first"

And guess what..
No one answered after this.
I was personally emailed to tell that the problem was solved by my approach. Gr888!!

The entire thread can be read at http://www.daniweb.com/web-development/php/threads/138376