Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 2nd, 2004, 12:56 AM
Authorized User
 
Join Date: Jul 2004
Location: Irving, Texas, USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Abstract Classes

I have been trying to make the following example work from my book "Visual C#.net - A guide for VB6 Developers' page 179.
************************************************** ****************
   Note: This is a Console Application:
The problem: I have an ABSTRACT base class called person. I have two other classes called Student and Instructor that derive from "person". These three classes are driven by the MyExecutableClass (which follows here immediately). This all runs fine if I don't use the keyword "abstract" in my person class. I can not figure out why I can't make things work when I declare the person class Abstract! I get the error :"Inconsistent accessibility: base class :'PersonExample.Person' is less accessible than class
'PersonExample.Student' ".

I have separated the 4 pieces here with "*******"'s :

***************** Main process driver ***********************
using System;

namespace PersonExample
{
    class MyExecutableClass
    {
        static void Main(string[] args)
        {
            Person person;

            Instructor instructor = new Instructor("Mr. Bean");
            Student student = new Student("Joey");

            person = instructor;
            person.DescribeYourself();

            person = student;
            person.DescribeYourself();
        }
    }
}
****************** Class "Instructor" ****************************

using System;

namespace PersonExample
{

    public class Instructor: Person
    {
        public Instructor(string name): base (name)
        {}
        public override void DescribeYourself()
        {
            Console.WriteLine ("Good morning class. My Name is {0}, and I am your instructor.", name);
        }
        public void Teach()
        {
            Console.WriteLine ("Instructor babbles incomprehensibly.");
        }
    }
}
******************** Class "Student" ***************************

using System;

namespace PersonExample
{
    public class Student: Person
    {
        public Student(string name): base(name)
        {
        }
        public override void DescribeYourself()
        {
            System.Console.WriteLine ("Hello man, I'm {0}. I am a cool student.", name);
        }
        public void Learn()
        {
            System.Console.WriteLine ("Student wakes up, yawns, scratches head and tries to look interested.");
        }
    }
}
*************** Abstract Base Class "Person" ***********************
using System;

namespace PersonExample
{
    abstract class Person
    {
        protected string name;

        public Person(string name)
        {
            this.name = name;
        }

        public virtual void DescribeYourself()
        {
            System.Console.WriteLine ("Hi there! My name is {0} and I am a nice person.", name);
        }
    }
}
************************************************** ***************
Can anybody reveal the error here for me? I am at a loss on this. Thank you.

Cmarek
__________________
Cmarek
Reply With Quote
  #2 (permalink)  
Old September 2nd, 2004, 07:40 AM
Authorized User
 
Join Date: Jan 2004
Location: Des Moines, IA, USA.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

        public abstract virtual void DescribeYourself()


Carl Olsen
www.carl-olsen.com
Reply With Quote
  #3 (permalink)  
Old September 2nd, 2004, 09:24 AM
Authorized User
 
Join Date: Jul 2004
Location: Irving, Texas, USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CarlOlsen...Thanks for your reply. However that did not solve my problem, I simply got one more error out of it, Namely: The abstract method PersonExample.Person.DescribeYourself() can not be marked "virtual"...But Carl, you gave me an idea. I backed out your suggestion and placed the keyword "public" before the keyword "abstract", and the project now works! My new problem is this... I am a new student to this stuff, I do not know what the author is telling me if I must pioneer my own solutions to book examples that don't work as the concepts are being introduced to me for the first time!!! Am I supposed to define abstract classes like this : [u]public abstract class Person</u> instead of how the book did it ([u]abstract class Person</u>)?? Now as I understand it, one should not be able to instantiate an object from an abstract class. So after making my "correction" to the way the book declares an abstract class, I tried to instantiate an object from my person class, and the compiler did indeed refuse to instantiate an object as is, I suppose, the desired effect. So am I understanding the concept ? Or am I just slapping code around until it behaves as I think things are supposed to behave?

Cmarek
Reply With Quote
  #4 (permalink)  
Old September 2nd, 2004, 05:57 PM
Authorized User
 
Join Date: Jan 2004
Location: Des Moines, IA, USA.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was just slapping the code around. I looked in a book that had a method in an abstract class and the method was also marked "abstract". When I made that simple change, your code compiled for me. I really have no idea what I'm doing. All I really want to do is write ASP.NET applications, but I finally realized I was going to have to learn a object oriented programming language first. C# looked like a good language to learn, because it looks similar to JavaScript and PHP. The more I learn about C#, the more I like it.

Yes, it's my understanding that you cannot instantiate an abstract class. You create another class which inherits from it to use it's methods. That's about all I know.

Carl Olsen
www.carl-olsen.com
Reply With Quote
  #5 (permalink)  
Old September 3rd, 2004, 02:50 PM
Authorized User
 
Join Date: Jul 2004
Location: Irving, Texas, USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I now know what is wrong. You will notice that I have declared class Instructor and class student as being with the accessor of - PUBLIC. I left the accessor type off of the abstract declaration for person class, so person class defaulted to accessor of Private. This made things tough for Public class's Instructor and class student, both of whom inherit from the "private" abstract class Person. Just as the compiler said: " Person is less accessible than class Instructor and Student. It is incompatible scopes I am dealing with here.

Cmarek
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract classes/Interfaces vasancer BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 October 22nd, 2008 02:06 PM
polymorphism and abstract classes elvisfeverr C++ Programming 3 October 16th, 2008 01:39 AM
Why abstract class can't be instantiated? anisurr C++ Programming 5 July 23rd, 2008 01:32 AM
Diffrences of Abstract Classes and Interfaces jimblanc Java Basics 4 September 4th, 2006 01:30 PM
Abstract classes guppyheart C# 1 September 9th, 2003 05:23 PM



All times are GMT -4. The time now is 06:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.