Clarification about Polymorphism / Inheritance

I am trying to better my understanding of polymorphism.

Say I have a base class called baseClass with one method called foo() and I have three derived classes called derived1 , derived2 and derived3 which all override the and implement their own versions of the foo() method.

If I have an array of baseClass objects can it contain objects that are derived from that base class like so:

baseClassArray: [ derivedObj1, derivedObj2, derivedObj3];

Also if I pull an object out of this array and called the foo() method would it call the method defined in baseClass or the method defined in the respective derived class?

Or does this depend on the programming language this is implemented in?

4

  • You’re describing polymorphism. You’re using the base class as the interface. So your example is like:

((BaseClass)baseClassArray[0]).foo()

or

((BaseClass)derivedObj1).foo()

The foo() method is defined by base class, but the implementation is in the subclass, at least in your example. In OO, if a subclass overrides a method, then that becomes the implementation that gets called.

  • Inheritance is a little different. Let’s say you have a derivedObj4 which does not override foo(). Then you can call:

((SubClass)derivedObj4).foo()

which would first look in derivedObj4, see that foo() is not there, and then look to BaseClass for the implementation.

  • polymorphism can rely on inheritance, which makes it a little confusing.

  • The idea behind polymorphism is that the base class defines the methods, and the subclasses define the implementation. (Although they may still inherit the implementation.) In other words, the base class masks the subclass, but the subclass does the work.

  • The idea behind inheritance is that the subclass is the interface, but inherits the implementation from the base class, so it doesn’t need to implement it. The subclass masks the base class, but the base class does the work.

The actual method that is called will be determined at run time depending what the type of the object is. If you have an array of objects of type baseClass (which can include inherited types) the method that is called will be the foo() method of whichever type the object is.

So the objects that are of type baseClass will have the base class implementation of foo() called. If your objects are one of the derived types and the derived objects override the foo() method this will be called. If the derived objects do not override the foo() method then the base class method foo() will be called.

2

This should work, but is language specific. what you are referring to is a “upcast”, taking the derived objects and upcasting them back to the base class. Depending on your language you will have to perform the cast ex:

derived1 obj1 = new derived1();

baseClass base[10];

base[0] = (baseClass)obj1;

when you call the foo() method the baseClass foo() will be used unless you perform and additional “downcast” ex:

derived1 sameobj1 = (derived1)base[0];

after performing the downcast the foo() method for derived1 will be called. some language can use the “typeOf” comparison operator instead of upcasting and downcasting ex:

if( obj1 typeOf baseClass)

base[0] = obj1; // no casting necessary

then when you pull them out of the array base just call the foo() and the foo() for each derived class will be called.

foreach( obj in base)

obj.foo()

this will call the foo() method for each derived class.

Inheritance

// this is pseudo code - isn

// base class references to derived-type objects.
BaseClass thing1 = new Derived1();
BaseClass thing2 = new Derived2();
BaseClass thing3 = new Derived3();

// assume the array elements must be of the same type
baseClassArray : [thing1, thing2, thing3];

for (int i-0; i< baseClassArray.Length; i++) {
    baseClassArray[i].Foo(); // base.Foo() is called.
}

for (int i=0; i< baseClassArray.Length; i++) {
    if (baseClassArray[i] is Derived1) {
        Derived1 thingy1 = (Derived1)baseClassArray[i];
        thingy1.Foo();   // the over-ride Foo() called
    }else if ....
}

Polymorphism

// this is pseudo code - ish

public abstract class AbstractPainter {
   public abstract void Style();
}

public class Dali : AbstractPainter {
    public override Style() {
        console.writeline("floppy clocks lying about");
}

public class Warhol : AbstractPainter {
    public override Style() {
        console.writeline("High contrast soup cans");
    }
}

// cannot instantiate abstract classes
// ie.  new AbstractPainter() is not allowed.

AbstractPainter Salvador = new Dali();
AbstractPainter Andy = new Warhol();

AbstractPainterArray = [Salvador, Andy];

for (int i=0; i< AbstractPainterArray.Length; i++) {
    AbstractPainterArray[i].Style();
}

output:
    "floppy clocks lying about"
    "High contrast soup cans"

Take Away?

Inheritance: Same-type references + derived objects, same method call.

polymorphism: Same-type references + derived objects, different method call.

The virtual method called always depends on type of concrete instance in memory and never on how this instance is accessed or type of variable the instance is accessed from. The type of variable just tells compiler what classes and it’s subclasses can be saved and what virtual methods can be called. But what method is actually called is only known from memory at runtime.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Clarification about Polymorphism / Inheritance

I am trying to better my understanding of polymorphism.

Say I have a base class called baseClass with one method called foo() and I have three derived classes called derived1 , derived2 and derived3 which all override the and implement their own versions of the foo() method.

If I have an array of baseClass objects can it contain objects that are derived from that base class like so:

baseClassArray: [ derivedObj1, derivedObj2, derivedObj3];

Also if I pull an object out of this array and called the foo() method would it call the method defined in baseClass or the method defined in the respective derived class?

Or does this depend on the programming language this is implemented in?

4

  • You’re describing polymorphism. You’re using the base class as the interface. So your example is like:

((BaseClass)baseClassArray[0]).foo()

or

((BaseClass)derivedObj1).foo()

The foo() method is defined by base class, but the implementation is in the subclass, at least in your example. In OO, if a subclass overrides a method, then that becomes the implementation that gets called.

  • Inheritance is a little different. Let’s say you have a derivedObj4 which does not override foo(). Then you can call:

((SubClass)derivedObj4).foo()

which would first look in derivedObj4, see that foo() is not there, and then look to BaseClass for the implementation.

  • polymorphism can rely on inheritance, which makes it a little confusing.

  • The idea behind polymorphism is that the base class defines the methods, and the subclasses define the implementation. (Although they may still inherit the implementation.) In other words, the base class masks the subclass, but the subclass does the work.

  • The idea behind inheritance is that the subclass is the interface, but inherits the implementation from the base class, so it doesn’t need to implement it. The subclass masks the base class, but the base class does the work.

The actual method that is called will be determined at run time depending what the type of the object is. If you have an array of objects of type baseClass (which can include inherited types) the method that is called will be the foo() method of whichever type the object is.

So the objects that are of type baseClass will have the base class implementation of foo() called. If your objects are one of the derived types and the derived objects override the foo() method this will be called. If the derived objects do not override the foo() method then the base class method foo() will be called.

2

This should work, but is language specific. what you are referring to is a “upcast”, taking the derived objects and upcasting them back to the base class. Depending on your language you will have to perform the cast ex:

derived1 obj1 = new derived1();

baseClass base[10];

base[0] = (baseClass)obj1;

when you call the foo() method the baseClass foo() will be used unless you perform and additional “downcast” ex:

derived1 sameobj1 = (derived1)base[0];

after performing the downcast the foo() method for derived1 will be called. some language can use the “typeOf” comparison operator instead of upcasting and downcasting ex:

if( obj1 typeOf baseClass)

base[0] = obj1; // no casting necessary

then when you pull them out of the array base just call the foo() and the foo() for each derived class will be called.

foreach( obj in base)

obj.foo()

this will call the foo() method for each derived class.

Inheritance

// this is pseudo code - isn

// base class references to derived-type objects.
BaseClass thing1 = new Derived1();
BaseClass thing2 = new Derived2();
BaseClass thing3 = new Derived3();

// assume the array elements must be of the same type
baseClassArray : [thing1, thing2, thing3];

for (int i-0; i< baseClassArray.Length; i++) {
    baseClassArray[i].Foo(); // base.Foo() is called.
}

for (int i=0; i< baseClassArray.Length; i++) {
    if (baseClassArray[i] is Derived1) {
        Derived1 thingy1 = (Derived1)baseClassArray[i];
        thingy1.Foo();   // the over-ride Foo() called
    }else if ....
}

Polymorphism

// this is pseudo code - ish

public abstract class AbstractPainter {
   public abstract void Style();
}

public class Dali : AbstractPainter {
    public override Style() {
        console.writeline("floppy clocks lying about");
}

public class Warhol : AbstractPainter {
    public override Style() {
        console.writeline("High contrast soup cans");
    }
}

// cannot instantiate abstract classes
// ie.  new AbstractPainter() is not allowed.

AbstractPainter Salvador = new Dali();
AbstractPainter Andy = new Warhol();

AbstractPainterArray = [Salvador, Andy];

for (int i=0; i< AbstractPainterArray.Length; i++) {
    AbstractPainterArray[i].Style();
}

output:
    "floppy clocks lying about"
    "High contrast soup cans"

Take Away?

Inheritance: Same-type references + derived objects, same method call.

polymorphism: Same-type references + derived objects, different method call.

The virtual method called always depends on type of concrete instance in memory and never on how this instance is accessed or type of variable the instance is accessed from. The type of variable just tells compiler what classes and it’s subclasses can be saved and what virtual methods can be called. But what method is actually called is only known from memory at runtime.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật