Inheritance question

  • Thread starter Thread starter Michael A. Covington
  • Start date Start date
Mark Broadbent said:
Yes, certainly there is an argument to be said for this however you are then
moving into the area of distinction between what is a subclass and what is a
subtype.
Now from an entire OOP design point of view I'd say that if you are needing
to hide public methods like this in your subclass then potentially you've
possibly implemented too much functionality in your base class. But there
are certain base classes that we want to use which are not under our control
and yet still provide all (and more that we don't) of the behaviour that
needs to be inherited as is the case here.
Best OOP example. What would you do with a Platypus class that inherits a
Mammal class?
Mammal m = new Mammal();
Mammal n = m.GivesBirth();

Platypus p = new Platypus();
Platypus q = p.GivesBirth(); //whoops! thats not valid for a Platypus
Platypus.Egg r = p.LaysEgg();
Platypus s = r.Hatch();
//cute aint he!

abstract class Mammal{}
abstract class Monotreme:Mammal
{
public abstract Egg LayEgg();
}
abstract class LiveBirthingMammal:Mammal
{
public abstract LiveBirthingMammal GiveBirth();
}
abstract class Marsupial:LiveBirthingMammal{}
abstract class Eutherian:LiveBirthingMammal{}

class Platypus:Monotreme{}

:o)
 
:) very good.

Only problem is the Microsoft wrote the Mammal base class. It came with the
Biology Resource Kit for .NET Framework 1.1

LOL.
 
:) very good.

Only problem is the Microsoft wrote the Mammal base class. It came with the
Biology Resource Kit for .NET Framework 1.1

"This behaviour is by design". Damned Microsoft creationists!
 
Mark-
we can use like below. This will give some amount of flexibility.
we can use public functions if we want, else we can shitch it off if we do
not require.

Hope this may help.
Thanks-
Karthik . S. Rao

using System;

namespace InheritanceAnswer
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
Iderived aa = run.GetInstance();
aa.mypublicfunct();

// public function of class "HideTest" is only visible when we use like
below...
derived bb = (derived)run.GetInstance();
bb.stuff();

Console.ReadLine();
}
}
public class HideTest
{
public HideTest()
{
}
public void stuff()
{
// this is the public method to be hidden
Console.WriteLine("Stuff from base class.");
}
}
public interface Iderived
{
void mypublicfunct();
}
public class derived : HideTest, Iderived
{
protected derived()
{
Console.WriteLine("Constructor in derived executed.");
}
void Iderived.mypublicfunct()
{
Console.WriteLine("Myfunct from Interface Iderived");
}
}
public class run : derived
{
public static Iderived GetInstance()
{
Iderived ret = new derived();
return ret;
}
}

}
 
Mark-
One more way

Hope this may help you
Thanks-
Karthik.S

using System;

namespace InheritanceAnswer
{

class MainClass
{
[STAThread]
static void Main(string[] args)
{
Iderived cc = derived.GetInstance();
cc.mypublicfunct();

// public function of class "HideTest" is only visible when we use like
below...
derived bb = (derived)derived.GetInstance();
bb.stuff();
Console.ReadLine();
}
}
public class HideTest
{
public HideTest()
{
}
public void stuff()
{
// this is the public method to be hidden
Console.WriteLine("Stuff from base class.");
}
}
public interface Iderived
{
void mypublicfunct();
}
public class derived : HideTest, Iderived
{
protected derived()
{
Console.WriteLine("Constructor in derived executed.");
}
void Iderived.mypublicfunct()
{
Console.WriteLine("Myfunct from Interface Iderived");
}
public static Iderived GetInstance()
{
Iderived ret = new derived();
return ret;
}
}



/////////////////////////////////////////////////////////////////////
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top