Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi Friends,

Can anyone give me a explnation for this program??

in Java the output :
Hi From derived

in C# the Output :
Hi From base


Why oops concept diffrent in both langauges ?


but in both language - the object type is derived class

C#
class Program
 {
     static void Main(string[] args)
     {
         MyClass A= new MyClass1();
         A.h1();

         System.Type type = A.GetType();
         Console.WriteLine(type.ToString());
         
     }
 }
 public class MyClass
 {
     
     public void h1()
     {
         Console.WriteLine("Hi From base");
     }

 }
public class MyClass1 :MyClass
 {
     public void h1()
     {
         Console.WriteLine("Hi From derived");
     }
     
 }
Posted
Comments
Shahin Khorshidnia 30-Mar-12 15:41pm    
Good Question!
Sergey Alexandrovich Kryukov 30-Mar-12 22:01pm    
I think this is no more but an elementary bug. And if the question was good, it would contain sample code in two languages, not just one. Nevertheless, it's easy to see what's the problem.
--SA
Shahin Khorshidnia 31-Mar-12 6:11am    
Of course, but it's a very common question (Although simple) I've seen many beginners (in C#) that are making the mistake.

If you want the method to be overridden in a derived class you need to mark it as virtual in the base.

C#
public class MyClass
 {

     public virtual void h1()
     {
         Console.WriteLine("Hi From base");
     }

 }
public class MyClass1 :MyClass
 {
     public override void h1()
     {
         Console.WriteLine("Hi From derived");
     }

 }


As you have it your instance of MyClass, A, only knows of the h1 method it contains.
 
Share this answer
 
v3
Comments
Shahin Khorshidnia 30-Mar-12 15:38pm    
My +5
Sergey Alexandrovich Kryukov 30-Mar-12 21:59pm    
Exactly, a 5.
That said, OOP concepts are not really different in those language in this respect; this can be considered as a bug in C#, that's it.
--SA
Andreas Gieriet 31-Mar-12 1:59am    
I might disagree: I don't see this as a bug. C# provides more fine grained control over the method declaration compared to Java. If it was a wise decision to have non-virtual as default in C# is another question. May be, it has to be viewed as a legacy.
Sergey Alexandrovich Kryukov 1-Apr-12 0:09am    
Perhaps you misunderstood what I meant as a bug (because I did not express myself clearly). The bug is OP's code: it looks like OP assumed overridden function without abstract/virtual/override syntax, and also ignored the warning. That is a bug.

--SA
[no name] 31-Mar-12 13:32pm    
Since this functionality is present in C++ and, I believe Delphi, which is the basis for C# I would say it is a well-thought design decision not a bug.
Fix the warnings first! Each warning might be a problem.
And you run now into a hiding issue.
I guess you had a warning while you did compile C#, right?

IIRC, in Java, all is virtual, while in C# only the so declared methods are.
(I'm not a Java programmer, though)

[EDIT]
This is the warning you get for C# in Visual Studio 2010:
Warning 1 'ConsoleApplication1.MyClass1.h1()' hides inherited member 'ConsoleApplication1.MyClass.h1()'. Use the new keyword if hiding was intended.


But I have to admit: when pressing F1 in Visual Studio 2010 on that warning to get more details on it, the help text from Microsoft is crap. No word about virtual/override. The simplest solution is not given: rename the hiding member, nor any word about virtual/overriding is given...
I would say: "no one" ever wants to hide a memeber - calls for maintenance trouble.
Any example why one would want to hide a member by applying the new keyword?
[/EDIT]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 30-Mar-12 21:59pm    
But that a good point (about warnings), my 5.
--SA
Andreas Gieriet 31-Mar-12 2:02am    
Thanks for your 5! Why the other vote is a 2 would be interesting though... in this case, the warning would have told the problem, assuming one knows what hiding means...
Sergey Alexandrovich Kryukov 1-Apr-12 0:15am    
Always a please to mark a good answers. As to the 2... I noticed that somebody often down-vote, for example, any strong opinion, or sometimes, any answer not presenting a ready-to-use cookbook code sample.

Now, about this warning, we has some misunderstanding when I considered the OP's code sample as a bug. (Please see other comments.) I believe, originally, that was mostly the lack of knowledge of the syntax, but paying attention for the warning could help to resolve the problem by learning it, quite easily. That's how such warning could be critically important.

--SA
Andreas Gieriet 1-Apr-12 1:39am    
I initially got your statement on the bug wrong - with your explanation it is now clear that you meant the OP's code to have a bug.
Cheers
Andi
Sergey Alexandrovich Kryukov 1-Apr-12 14:23pm    
As two other experts got it wrong, I guess I did not express myself clearly. Hopefully, we now understand each other.
Thank you,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900