Java |
A class can have one super class, but may
implement many interfaces.
class Sub extends Super implements InterA,
InterB, ...
//class A's super class is Object.
//class A is a subclass of Object class implicitly
class A
{
public void F() {
System.out.println("A.F");
}
}
class B extends A
{
public void G() {
System.out.println("B.G");
}
}
class Test
{
public static void main(String[] args) {
B b = new B();
b.F(); // Inherited from A
b.G(); // Introduced in B
A a = b; // Treat a B as an A
a.F();
}
}
class A
{
public void F() {
System.out.println("A.F");
}
}
class B extends A
{
public void F() {
super.F();
System.out.println("B.F");
}
}
class Test
{
public static void main(String[] args) {
B b = new B();
b.F();
A a = b;
a.F();
}
}
abstract class A
{
public abstract void F();
}
class B extends A
{
public void F() {
System.out.println("B.F");
}
}
class Test
{
public static void main(String[] args) {
B b = new B();
b.F();
A a = b;
a.F();
}
}
//A popular example
public class DrawingObject
{
public void Draw()
{
System.out.println("I'm just a generic drawing object.");
}
}
public class Line extends DrawingObject
{
public void Draw()
{
System.out.println("I'm a Line.");
}
}
public class Circle extends DrawingObject
{
public void Draw()
{
System.out.println("I'm a Circle.");
}
}
public class Square extends DrawingObject
{
public void Draw()
{
System.out.println("I'm a Square.");
}
}
public class DrawDemo
{
public static void main(String[] args)
{
DrawingObject[] dObj = new DrawingObject[4];
dObj[0] = new Line();
dObj[1] = new Circle();
dObj[2] = new Square();
dObj[3] = new DrawingObject();
for(int i= 0; i < dObj.length; i++)
{
dObj[i].Draw();
}
}
}
Output:
I'm a Line.
I'm a Circle.
I'm a Square.
I'm just a generic drawing object
|
|
C# |
A class can have one super class, but may
implement many interfaces.
class Sub: Super, InterA,
InterB, ...
//class A's base class is object
//class A is a subclass of object class implicitly
class A
{
public void F() {
Console.WriteLine("A.F");
}
}
class B: A
{
public void G() {
Console.WriteLine("B.G");
}
}
class Test
{
static void Main() {
B b = new B();
b.F(); // Inherited from A
b.G(); // Introduced in B
A a = b; // Treat a B as an A
a.F();
}
}
class A
{
public virtual void F() {
Console.WriteLine("A.F");
}
}
class B: A
{
public override void F() {
base.F();
Console.WriteLine("B.F");
}
}
class Test
{
static void Main() {
B b = new B();
b.F();
A a = b;
a.F();
}
}
abstract class A
{
public abstract void F();
}
class B: A
{
public override void F() {
Console.WriteLine("B.F");
}
}
class Test
{
static void Main() {
B b = new B();
b.F();
A a = b;
a.F();
}
}
//A popular example
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Circle.");
}
}
public class Square : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Square.");
}
}
public class DrawDemo
{
public static int Main(string[] args)
{
DrawingObject[] dObj = new DrawingObject[4];
dObj[0] = new Line();
dObj[1] = new Circle();
dObj[2] = new Square();
dObj[3] = new DrawingObject();
foreach (DrawingObject drawObj in dObj)
{
drawObj.Draw();
}
return 0;
}
}
Output:
I'm a Line.
I'm a Circle.
I'm a Square.
I'm just a generic drawing object
|
|