Let's say Java has these hierarchical classes:
class A
{
}
class B extends A
{
public void m()
{
System.out.println("B\n");
}
}
class C extends B
{
public void m()
{
System.out.println("C\n");
}
}
class D extends C
{
public static void main(String[] args)
{
A a = new D();
// a.m(); // doesn't work
B b = new D();
b.m();
C c = new D();
c.m();
D d = new D();
d.m();
}
}
This is the (blind) duplication of the same code in C#:
using System;
class A
{
}
class B : A
{
public void M()
{
Console.WriteLine("B");
}
}
class C : B
{
public void M() // I need to use public new void M() to avoid the warning
{
Console.WriteLine("C");
}
}
class D : C
{
public static void Main(String[] args)
{
A a = new D();
// a.M(); // doesn't work
B b = new D();
b.M();
C c = new D();
c.M();
D d = new D();
d.M();
}
}
When I execute the Java code, I got C-C-C
whereas C# returns B-C-C
.
To me C#'s result makes more sense, as reference B invokes its own method.
C-C-C
instead of B-C-C
? I mean, why reference B uses the overriding method in C? What's the advantage of this approach?B-C-C
just like C# does? I mean, how can I teach java to invoke the method of the exact reference it uses?C-C-C
? I mean, how can I teach C# to invoke the overriding method?When you create a class in Java, it automatically inherits from the Object Class. However, in C++, there is a forest of classes; when we create a class that does not inherit from another, we are creating a new tree in the forest. The Test class inherits from the Object class by default, as shown in the Java example.
In java all methods are virtual by default. In C++, we have to specify the virtual keyword. In C++, we can use the multiple inheritance. In Java, we cannot create multiple inheritance directly.
Java is a very simple language, and the inheritance model is simpler too. If you need to "inherit" from multiple sources you have to use interfaces which don't have these problems but they are also less flexible. cont. C++ doesn't have interfaces, because interfaces usually don't make sense in C++.
No it doesnt. C is not an Object Oriented language. Inheritance is a property of OO languages.
It's for the virtual function definition:
a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).
In C#,you should declare the method as virtual in order to be overriden, as shown in MSDN:
Since the M
method is not virtual, it will execute b.M()
even if b
variable is actually a D
instance.
In Java, every non-static method is virtual by default, so you when you override a method (even without the @Override
annotation) the behavior of the b.M()
will be the d.M()
that inherits the c.M()
method behavior.
How can I change Java code to print out B-C-C just like C# does? I mean, how can I teach java to invoke the method of the exact reference it uses?
You simply can't do this in Java. The M
method in C
class would override the M
method in B
. Adding the final
modifier to B#M
will just make that C
or other B
children can't override the M()
method.
How can I change C# code to print out C-C-C? I mean, how can I teach C# to invoke the overriding method?
Change the M
method in B
class to virtual
and override it in C
class:
class B : A
{
public virtual void M()
{
Console.WriteLine("B");
}
}
class C : B
{
public override void M() // I need to use public new void M() to avoid the warning
{
Console.WriteLine("C");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With