|
-
Jul 10th, 2010, 03:41 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Method overridiing Issue
Can any body tell me ?why i am getting the following error.namespace does not directly contain members such as fields or methods.when i tried to run the following code.Kindly let me know the idea.
Code:
using System;
class A
{
public void Driver()
{
Console.WriteLine("This is the Driver method of the Class A");
}
}
Class B: A
{
New Public Void Driver()
{
Console.writeline("This is the Overridden Driver method of the Class B");
}
}
Class Test
{
Public Static Void Main()
{
B objB=New B();
oBJB.Driver();
}
}
-
Jul 10th, 2010, 04:38 AM
#2
Frenzied Member
Re: Method overridiing Issue
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jul 10th, 2010, 04:59 AM
#3
Re: Method overridiing Issue
1. Unlike VB, C# is a case-sensitive language. So public, Public, void, Void, static, Static are all different from each other. Usually the lowercase ones are valid keywords.
2. new keyword is used to initialize objects. So they can't be used in method/property/class declarations.
3. "override" keyword can be used to override base class members.
c# Code:
using System; class A { public void Driver() { Console.WriteLine("This is the Driver method of the Class A"); } } class B : A { public override void Driver() { Console.WriteLine("This is the Overridden Driver method of the Class B"); } } class Test { public static void Main() { B objB = new B(); objB.Driver(); } }
Last edited by Pradeep1210; Jul 10th, 2010 at 05:03 AM.
-
Jul 10th, 2010, 05:29 AM
#4
Thread Starter
Frenzied Member
Re: Method overridiing Issue
Still Not Working .here is the following code what i have written.Kindly let me know the idea.Any help would be highly appreciated.
Code:
using System;
class A
{
public void Driver()
{
Console.WriteLine("This is the Driver method of the Class A");
}
}
class B: A
{
Public override Void Driver()
{
Console.writeline("This is the Overridden Driver method of the Class B");
}
}
Class Test
{
Public Static Void Main()
{
B objB=New B();
oBJB.Driver();
}
}
-
Jul 10th, 2010, 05:33 AM
#5
Frenzied Member
Re: Method overridiing Issue
hay,
Code:
Public override Void Driver()
must be
Code:
public override void Driver()
it is case sensitive
and in your test class
Code:
Public Static Void Main()
it must be
Code:
public static void Main()
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jul 10th, 2010, 05:39 AM
#6
Thread Starter
Frenzied Member
Re: Method overridiing Issue
Still getting error .here is the following code .what i have written.
Code:
using System;
class A
{
public void Driver()
{
console.WriteLine("This is the Driver method of the Class A");
}
}
class B: A
{
public override void Driver()
{
console.writeline("This is the Overridden Driver method of the Class B");
}
}
class Test
{
Public static void Main()
{
B objB=New B();
oBJB.Driver();
}
}
-
Jul 10th, 2010, 05:41 AM
#7
Re: Method overridiing Issue
There's a bit of misinformation being provided here. The C# 'new' keyword IS valid on methods and property declarations. The VB equivalent is Shadows. Shadowing a member and overriding it are similar but not the same. Generally speaking, it's preferable to declare a base member 'overridable' and then declare a derived member of the same name 'override'. If the base member isn't declared 'overridable' then you must declare the derived member 'new'. If you were to try Pradeep's code you would get an error message telling you that Driver can't be overridden.
The main difference between overriding and shadowing is that overriding follows the type of the object, while shadowing follows the type of the reference. To demonstrate this, try running this code:
Code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BaseClass b = new BaseClass();
Console.WriteLine("BaseClass object via BaseClass reference:");
b.OverridableMethod();
b.NotOverridableMethod();
Console.WriteLine();
DerivedClass d = new DerivedClass();
Console.WriteLine("DerivedClass object via DerivedClass reference:");
d.OverridableMethod();
d.NotOverridableMethod();
Console.WriteLine();
b = d;
Console.WriteLine("DerivedClass object via BaseClass reference:");
b.OverridableMethod();
b.NotOverridableMethod();
Console.ReadLine();
}
}
public class BaseClass
{
public virtual void OverridableMethod()
{
Console.WriteLine("BaseClass.OverridableMethod");
}
public void NotOverridableMethod()
{
Console.WriteLine("BaseClass.NotOverridableMethod");
}
}
public class DerivedClass : BaseClass
{
public override void OverridableMethod()
{
Console.WriteLine("DerivedClass.OverridableMethod");
}
public new void NotOverridableMethod()
{
Console.WriteLine("DerivedClass.NotOverridableMethod");
}
}
}
As you'll see form the output, the first two cases are exactly as you'd expect: a BaseClass object via a BaseClass reference reports BaseClass methods and a DerivedClass object via a DerivedClass reference reports DerivedClass methods. It's the third case that gets interesting. When accessing a DerivedClass object via a BaseClass reference, the overridden method reports a DerivedClass method, while the shadowed method reports a BaseClass method. So the overridden members will always be invoked on the type of the object itself, while shadowed members will be invoked on the type of the reference, even if the actual object is a more derived type. This is why overriding is preferable, but shadowing is all you can do if you're not the author of the base class and they didn't declare the member virtual.
-
Jul 10th, 2010, 05:50 AM
#8
Frenzied Member
Re: Method overridiing Issue
hay,
this is code will work
Code:
using System;
class Test
{
static void Main()
{
B objB = new B();
objB.Driver();
}
}
class A
{
public virtual void Driver()
{
Console.WriteLine("This is the Driver method of the Class A");
}
}
class B : A
{
public override void Driver()
{
Console.WriteLine("This is the Overridden Driver method of the Class B");
}
}
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jul 10th, 2010, 05:54 AM
#9
Frenzied Member
Re: Method overridiing Issue
about the errors,
as we said it is a c# is case sensitive,
it must be
-----
you declared
Code:
B objB=New B();
oBJB.Driver();
and in the name was not the same, this object oBJB not exist., its name is objB
--------
at last you have to use virtual key word in the A class
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jul 10th, 2010, 06:14 AM
#10
Re: Method overridiing Issue
 Originally Posted by jmcilhinney
There's a bit of misinformation being provided here. The C# 'new' keyword IS valid on methods and property declarations. The VB equivalent is Shadows. ...
Great!
So I learnt a new use of new keyword today.
-
Jul 10th, 2010, 06:19 AM
#11
Frenzied Member
Re: Method overridiing Issue
hay jmcilhinney,
can you provide example for using new Key word with method ?
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jul 10th, 2010, 07:09 AM
#12
Re: Method overridiing Issue
 Originally Posted by avrail
hay jmcilhinney,
can you provide example for using new Key word with method ?
Perhaps you should read the information I've already provided before asking me to provide more.
-
Jul 10th, 2010, 07:14 AM
#13
Frenzied Member
Re: Method overridiing Issue
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jul 10th, 2010, 07:18 AM
#14
Thread Starter
Frenzied Member
Re: Method overridiing Issue
hay jmcilhinney,
can you provide example for using new Key word with method ?
Hi,avrail
see the following .you can use
Code:
public new void Driver()
or
Code:
public virtual void Driver()
or
Code:
new public void Driver()
this also works.
Code:
using System;
class A
{
public void Driver()
{
Console.WriteLine("This is the Driver method of the Class A");
Console.Read();
}
}
class B: A
{
public new void Driver() //this will also work
// public virtual void Driver() //this will work
{
Console.WriteLine("This is the Overridden Driver method of the Class B");
Console.Read();
}
}
class Test
{
public static void Main()
{
B objB=new B();
objB.Driver();
}
}
Last edited by firoz.raj; Jul 10th, 2010 at 07:24 AM.
-
Jul 10th, 2010, 07:22 AM
#15
Frenzied Member
Re: Method overridiing Issue
thanks firoz.raj
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Jul 10th, 2010, 07:26 AM
#16
Re: Method overridiing Issue
 Originally Posted by firoz.raj
you can use public new void Driver() instead of
public virtual void Driver() also.
That just goes to show that you've missed the point. There is one reason and one reason only to use the 'new' keyword: to shadow a member inherited from a base class. 'virtual' and 'new' are not interchangeable because they do two completely different things. 'virtual' is used in a base class to indicate that a member can be overridden. 'new' is used in a derived class to shadow an inherited member that has not been declared 'virtual'. Overriding an inherited member is always preferable to shadowing so you would only ever use 'new' if you needed to replace the base implementation of a member in a derived class but you couldn't use 'override' because the base member wasn't declared 'virtual'.
Another point to note is that a shadowed member can return a different type to the base member while an overridden member cannot.
-
Jul 10th, 2010, 07:36 AM
#17
Thread Starter
Frenzied Member
Re: Method overridiing Issue
Last edited by firoz.raj; Jul 11th, 2010 at 12:34 AM.
-
Jul 10th, 2010, 07:47 AM
#18
Re: Method overridiing Issue
OK, that's twice now that someone has asked a question that I've already answered in post #7. If you want answers but can't make the effort to read them when they're provided then you're wasting my time.
-
Jul 11th, 2010, 12:50 AM
#19
Thread Starter
Frenzied Member
Re: Method overridiing Issue
can anyone tell me please.what is the difference between Method Overriding.
and polymorphism???????.As per my knowledge.Method Overriding.Normally when methods are available in different version.compiler are identified these methods on the basis of Method parameter.then it is called Method overriding.it is as same as Polymorphism.let me clarify my doubt.
-
Jul 11th, 2010, 01:46 AM
#20
Re: Method overridiing Issue
Overriding and polymorphism have nothing really to do with each other. Overriding is as has already been demonstrated: replacing the implementation of an inherited member in a derived class.
"Poly" means "many" and "morph" means "shape", so "polymorphism" is literally "the ability to have many shapes". Specifically, polymorphism in OOP means that you can declare something (variable, property, method parameter, etc) as one type and then you can assign objects of many types to it, as long as the object is of that type or a type that inherits that type.
Take MDI applications for instance. When you want to display a form as an MDI child, you assign the parent form to its MdiParent property. The MdiParent property is declared type Form, but you can assign any form to it because every form is of a type that inherits the Form class.
In .NET apps, polymorphism allows for any type that inherits a specific base type or implements a specific interface. An example of that is using a 'foreach' loop. The 'foreach' statement allows you to loop through any object that implements the IEnumerable interface, so when you use 'foreach' to loop through an array or List<T> or whatever, you're making use of polymorphism.
I guess where polymorphism and overriding intersect a little is exactly how I've demonstrated in my code example in post #7. You can access an instance of a derived type via a variable of a base type and it will still be the derived implementation of the member that is invoked.
Last edited by jmcilhinney; Jul 11th, 2010 at 01:49 AM.
-
Jul 11th, 2010, 08:59 AM
#21
Thread Starter
Frenzied Member
Re: Method overridiing Issue
can anybody tell me ?why saying invalid token.
Code:
using System;
class baseIO
{
public string FileName;
public int Delete()
{
Console.Writeline("This is the delete Method of the BaseIO Class");
return(0);
}
class imageIO :baseIO
{
public string imgformat;
public int alphaBlend()
{
Console.WriteLine ("This is the alpha Blending method of the imageIO Class");
return(0);
}
}
class Test
{
static void Main()
{
imageIO obj=New imageIO();
obj.FileName ="ffffffff";
obj.Delete();
}
}
Last edited by firoz.raj; Jul 11th, 2010 at 11:14 PM.
-
Jul 11th, 2010, 07:14 PM
#22
Re: Method overridiing Issue
Why does your code have such bad formatting? It would be far easier to read for both you and us if it was formatted properly, and probably less prone to errors too. As for your question, could you perhaps point out exactly where this error occurs? It feels like you're trying to make this as hard for us as possible. You certainly aren't going out of your way to make it easier. If you'd like us to take the time and make the effort to help you, I suggest that you take the time and make the effort to help us.
-
Jul 14th, 2010, 11:59 PM
#23
Thread Starter
Frenzied Member
Re: Method overridiing Issue
Can you tell me ?why i am gettiing error no suitable method found to override.
let me know please.
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace AdvanoopTest
{
class A
{
public int methodA()
{
return (100);
}
}
class B : A
{
public override int methodB()
{
return (399);
}
}
class test
{
public static void Main()
{
B ObjB = new B();
System.Console.WriteLine(ObjB.methodA());
Console.Read();
}
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 15th, 2010, 01:46 AM
#24
Re: Method overridiing Issue
Have you read the documentation for the 'override' keyword? If you have then you already have your answer. If you haven't then I suggest that you get to it. It's about 20 seconds work to answer this question for yourself.
-
Jul 17th, 2010, 12:20 AM
#25
Thread Starter
Frenzied Member
Re: Method overridiing Issue
Last edited by firoz.raj; Jul 17th, 2010 at 03:29 AM.
-
Jul 17th, 2010, 12:53 AM
#26
Re: Method overridiing Issue
You're the one who asked the question. You tell us whether that answers your question or not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|