Abstract Classes

Topics: Object-oriented programming, Java, Class Pages: 5 (546 words) Published: June 30, 2013
IT111P
WEEK 7-1
ABSTRACT CLASSES

ABSTRACT


Before anything, take a look at the code below

ABSTRACT


How do we use them?

ABSTRACT
Abstract classes CANNOT be instantiated.  So what are they good for?  How can you use them?  You might say that abstract classes are meant to be inherited  It contains abstract methods  Abstract methods are methods with no definition  If abstract methods have no definition, how can you use them? 

ABSTRACT
The derived class implements the abstract method’s definition  Why doesn’t the abstract method use the virtual modifier?  By default abstract methods are virtual methods so there is no need to explicitly declare them as such.  Abstract methods may only exist in Abstract classes 

ABSTRACT
Note: static and virtual modifiers are not allowed in abstract classes  Properties are also declared abstract 

ABSTRACT

ABSTRACT

ABSTRACT

ABSTRACT

INTERFACE
In general, an interface is an intermediary element between two things. (this is not the definition you need for your tests)  Your cell phones have either keypads or touchscreen interfaces to allow you to perform certain functions of your phone.  You don’t know the internal constructs but through the interface you know exactly what it can do 

INTERFACE
When you look at the interface, you can tell something about the class that implements the interface  You would be able to see the behavior of the class or what it’s suppose to do by looking at the interface 

INTERFACE
You declare an interface using the interface keyword  And using the convention discussed earlier, the interface name starts with an uppercase I 

INTERFACE


An interface can contain
Methods  Properties  Indexers  Events


As you may have no noticed, fields are not included  Members of an interface are public by default  You cannot modify them 

INTERFACE

INTERFACE


Members of an interface only contain signatures of methods, properties, indexers, or events

INTERFACE
This should look familiar to you  What does this remind you of?  It looks like function prototypes in C++ 

INTERFACE
When a class implements an interface, that class has to write the implementation of all members of the interface  Otherwise an error would occur  The Colon operator (just like in inheritance) is used to tell the compiler you are implementing an interface or interfaces  However, unlike inheritance, you may implement more than one interface 

INTERFACE

INTERFACE

INTERFACE


You may now create an object of the class, as usual

INTERFACE

INTERFACE


Implementing multiple interfaces

INTERFACE

INTERFACE

INTERFACE


You may derive from a base class and implement multiple interfaces at the same time

INTERFACE

INTERFACE

INTERFACE

INTERFACE
You may also explicitly implement interfaces  Use the interface name followed by the member name 

INTERFACE
Normally you cannot create an object of an interface  Explicit Implementation allows you to create an interface object  However, you will not be able to access implemented members the we accessed them previously  To access the implemented member, you have to use the interface object itself 

INTERFACE

INTERFACE

INTERFACE

INTERFACE
When do you think we need to do explicit implementation?  There would be occasions when a class implements more than one interface  In the event that the interfaces contain similar method definitions, there would be no way to distinguish them from each other. 

REFERENCE


Abolrous, Sam, A., (2008) Learn C# Includes the C# 3.0 Features, Wordware Publishing, Inc


Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Research Paper Abstract
  • Power of Abstract Art Essay
  • Abstract Expressionism Essay
  • Abstract Expressionism Essay
  • The Meaning of Abstract Art Essay
  • Abstract Expressionists of the 19th Century Essay
  • Essay on ARTISTS OF ABSTRACT EXPRESSIONISM
  • Art History: Abstract Art vs. Pop Art Essay

Become a StudyMode Member

Sign Up - It's Free