Results 1 to 7 of 7

Thread: Difference between Inherits and Imports

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Difference between Inherits and Imports

    I noticed you can only Inherit one class per class. But you can Import all the classes you want. Don't Inherit and Import basically do the same thing?
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Import is just a shortcut for namespaces. Inherit makes a class inheit the abilities of the inherited class.

    They are completely different,
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Absolutely Not!

    Inheritance allows you to extend a class, while importing only gives you access to a class (or set of classes). Pick up any VB book or search the help - it will give you details.

    Also, VS.NET 2003 does allow multiple inheritance.

  4. #4

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I see, so Inherit is like just adding the methods of another class to your class and other people could then use all those methods as if it were a single class. So if i created a class called MyClass and added 2 methods and i imported another class that had 5 methods then i compiled it, if i were to create an instance of MyClass i would have access to all 7 methods.

    Whereas if i Imported a class with 5 other methods i would have access to those inside my class but anyone using my class would not. I think that's it.?
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Think of imports as a With statment. If everytime you want to show a message box for example.

    Without an Imports:

    system.Windows.Forms.MessageBox.Show("Hello")

    if you add an Imports System.Windows.Forms, then all you need is

    MessageBox.Show("hello")
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I see, so i'd still have access to every method, it just makes it shorter to type? Thanks.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Inheritance can look very similar, but the distinction is important.

    If you have a class Fish, it will have certain properties and methods, like swim, jump, etc. If you make a class Bass, that inherits class Fish, it has the same methods (unless you override them such that a Bass swims in a peculiarly Bassy fashion).

    Thus when you make an instance of class Bass, you can call its methods, and the public properties of both it and the base class Fish. If all of the base members are public, that would behave fairly similar to Imports. However, the thing about inheritance is that it allows you to include the functionality of the base class in an object without removing the security that the encapsulation within a class provides.

    I think this is a lousy exlanation, I hope there's something worthwhile in it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width