Skip to content

Instantly share code, notes, and snippets.

@aeris
Created Nov 4, 2011
Embed
What would you like to do?
Héritage et polymorphisme
public class Main {
private static class A {
protected void foo() {
System.out.println("A");
}
}
private static class B extends A {
@Override
protected void foo() {
System.out.println("B");
}
}
public static void main(final String[] args) {
final A a = new A();
a.foo();
final B b = new B();
b.foo();
final A c = b;
c.foo();
}
}
using System;
namespace Foo {
public class Bar {
private class A {
public void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
using System;
namespace Foo {
public class Bar {
private class A {
public void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public new void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
using System;
namespace Foo {
public class Bar {
private class A {
public virtual void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public override void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
using System;
namespace Foo
{
public class Bar {
private class A {
public void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public override void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment