So I started learning Interfaces and now I'm wondering when to use
Interface i = new Class();
and when to use
Class c = new Class();
and I noticed that I can't use class methods if I do it the first way, only interface methods. Do you know why?
Sorry I'm still a noob in Java, Thanks for answering
Let me put it in simple way.
Interface defines the behavior of class and classes which implements interface will give implementations to that behavior.
Here is an example
Interface Shape
{
void draw();
}
Class Circle implements Shape
{
void draw()
{
... code to draw circle
}
void printRadius()
{
}
}
Class Rectangle implements Shape
{
void draw()
{
... code to draw rectangle
}
void printDiagonal()
{
}
}
now if you see same Shape Interface is implemented by 2 classes diffrently.
Now i can write like this
Shape shape = new Circle(); // This will allow you access only draw method
Circle circle = new Circle(); // This will allow you access all methods of circle
When you want your client/consumer to access only Shape specicfic methods like draw then use Shape shape = new Circle() else if you want Circle specific method such as printRadius then use Circle circle = new Circle()
The interfaces in OOP paradigm used to generalized common behavior across group of somewhat similar objects. Therefore then you using variable of more general type, e.g. interface you will be able to use only those common methods which interface defines. Since you should be able to assign to interface variable any of interface descendants (classes which implements given interface) and be able to work with it. Therefore while you assign
Interface i = new Class();
the only methods you will be able to access is those defined in the Interface. Additionally need to note, that variable will be dynamically binded to the runtime type, e.g. to the Class in your example, thus the calls for methods defined in you interface will be dispatched to the implementation of the class.
Also think of the following, for example you have definitions:
interface Vehicle {
public void drive();
public void stop();
}
Now if you write code:
Vehicle v = new BMW()
v.drive()
// do something else
v.stop()
it should behave same when you replace new BMW() with new Mitsubishi(), regardless the fact that probably in your BMW class you might have
class BMW {
public void listenMusic()
}
This is also called "Liskov substitution principle"
Liskov's notion of a behavioral subtype defines a notion of substitutability for objects; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program.
Related
I have this UML diagram
Ad this is the corresponding C++ code
//Parent class Flight
class Flight
{
private:
int callNumber;
Airplane plane;
vector<Passenger> passengers;
public:
//Constructor
Flight();
//Functions
int getCallNum();
void setCallNum();
Airplane getPlane();
//What parameters are taken in these functions.
//I know they are of type Airplane and passenger but are they vectors?
void setPlane(Airplane);
void addPassenger(Passenger);
void removePassenger(Passenger);
};
//Airplane class, child of Flight
class Airplane : public Flight
{
private:
int firstClassSeats;
int economySeats;
public:
//Constructor
Airplane();
//Functions;
int getFirstClassSeats();
int getEconomySeats();
void setFirstClassSeats();
void setEconomySeats();
};
//Passenger class, child of FLight
class Passenger : public Flight
{
private:
string name;
int age;
string address;
public:
//Constructor
Passenger();
//Functions
string getName();
int getAge();
string getAddress();
void setName(string);
void setAge(int);
void setAddress(string);
};
I wonder:
do constructors of all classes run when an object of either parent or base class is created?
Can base class access functions or data of child classes?
I do not know how set plane function in parent class would look like. Would it take an object of type Airplane as an argument? Similarly, will addpassenger function in parent class take a vector of type Passenger as an argument?
In short
If A inherits B (or A specializes B), then you should be able to say A is a (kind of) B. When in doubt, prefer object composition over inheritance.
More details
The parameters taken by the member functions, are the parameters that you indicate for the operations in the diagram. No parameter in the diagram leads to no parameters in the code.
The inheritance here is ambigous. There is no inheritance in your diagram. There is some in your code, but it does not make so much sense: is a passenger really a flight? E.g. can a passenger fly, have a crew, etc.?
If the inheritance would be suitable, as a general rule in C++: the constructor of an object is always called when the object is created. In case of inheritance, all the constructors of the class hierarchy are invoked, starting with the base constructor, until the most derived constructor (the rules can be more tricky, for example in case of multiple inheritance). In UML, the rules on constructors are not fully specified as far as I know.
By default, a class can only access public members of another class. If a class is derived from a base class (in UML: if a class is a specialisation of a more general class), the derived class has only access to the public and protected members of the base class. Try to avoid protected, since it's a frequent cause of nasty bugs.
WHen implementing in C++ an UML class diagram, there is a tricky issue about the types of the properties and arguments, because C++ has a value semantic: if you pass an Airplane as argument, the original airplane object is copied. Same if you have an Airplane property. However, in UML, properties and associations have a reference semantic (except for datatypes), meaning that the airplane argument would still refer to the same original airplane. So in your specific case, you'd probably want to pass a reference or a (smart) pointer to an Airplane.
I have a common interface that describes access to the output stream like this:
interface IOutput {
function writeInteger(aValue:Int):Void;
}
And I have an abstract implementation of this interface based on standard haxe.io.BytesOutput class:
abstract COutput(BytesOutput) from BytesOutput {
public inline function new(aData:BytesOutput) {
this = aData;
}
public inline function writeInteger(aValue:Int):Void {
this.writeInt32(aValue);
}
}
Though this abstract is truly implementing interface described above there's no direct reference to interface and when I'm trying to use it like this:
class Main {
public static function out(aOutput:IOutput) {
aOutput.writeInteger(0);
}
public static function main() {
var output:COutput = new BytesOutput();
out(output); // type error
}
}
Compiler throws an error: COutput should be IOutput. I can solve this problem only through using common class that wraps BytesOutput and implements IOutput.
My question is how to show the Haxe compiler that the abstract implements the interface.
Abstracts can't implement interfaces because they're a compile-time feature and don't exist at runtime. This conflicts with interfaces, they do exist at runtime and dynamic runtime checks like Std.is(something, IOutput) have to work.
Haxe also has a mechanism called structural subtyping that can be used as an alternative to interfaces. With this approach, there's no need for an explicit implements declaration, it's good enough if something unifies with a structure:
typedef IOutput = {
function writeInteger(aValue:Int):Void;
}
Unfortunately, abstracts aren't compatible with structural subtyping either due to the way they're implemented.
Have you considered using static extensions instead? At least for your simple example, that seems like the perfect solution for making a writeInteger() method available for any haxe.io.Output:
import haxe.io.Output;
import haxe.io.BytesOutput;
using Main.OutputExtensions;
class Main {
static function main() {
var output = new BytesOutput();
output.writeInteger(0);
}
}
class OutputExtensions {
public static function writeInteger(output:Output, value:Int):Void {
output.writeInt32(value);
}
}
You could even combine this with structural subtyping so writeInteger() becomes available on anything that has a writeInt32() method (try.haxe link):
typedef Int32Writable = {
function writeInt32(value:Int):Void;
}
As #Gama11 states, abstracts cannot implement interfaces. In Haxe, for type to implement an interface, it must be able to be compiled to something class-like that can be called using the interface’s methods without any magic happening. That is, to use a type as its interface, there needs to be a “real” class implementing that type. Abstracts in Haxe compile down to their base type—the abstract itself is entirely invisible after compilation happens. Thus, at runtime, there is no instance of a class with the methods defined in your abstract which implement the interface.
However, you can make your abstract appear to implement an interface by defining an implicit conversion to the interface you are trying to implement. For your example, the following might work:
interface IOutput {
function writeInteger(aValue:Int):Void;
}
abstract COutput(BytesOutput) from BytesOutput {
public inline function new(aData:BytesOutput) {
this = aData;
}
#:to()
public inline function toIOutput():IOutput {
return new COutputWrapper((cast this : COutput));
}
public inline function writeInteger(aValue:Int):Void {
this.writeInt32(aValue);
}
}
class COutputWrapper implements IOutput {
var cOutput(default, null):COutput;
public function new(cOutput) {
this.cOutput = cOutput;
}
public function writeInteger(aValue:Int) {
cOutput.writeInteger(aValue);
}
}
class Main {
public static function out(aOutput:IOutput) {
aOutput.writeInteger(0);
}
public static function main() {
var output:COutput = new BytesOutput();
out(output);
out(output);
}
}
Run on try.haxe.org
Note that, each time an implicit conversion happens, a new instance of the wrapper will be constructed. This may have performance implications. If you only access your value through its interface, consider setting the type of your variable to the interface rather than the abstract.
This is similar to “boxing” a primitive/value type in C#. In C#, value types, defined using the struct keyword, are allowed to implement interfaces. Like an abstract in Haxe, a value type in C# is compiled (by the JITter) into untyped code which simply directly accesses and manipulates the value for certain operations. However, C# allows structs to implement interfaces. The C# compiler will translate any attempt to implicitly cast a struct to an implemented interface into the construction of a wrapper class which stores a copy of the value and implements the interface—similar to our manually authored wrapper class (this wrapper class is actually generated by the runtime as part of JITing and is performed by the IL box instruction. See M() in this example). It is conceivable that Haxe could add a feature to automatically generate such a wrapper class for you like C# does for struct types, but that is not currently a feature. You may, however, do it yourself, as exemplified above.
I was given a class with pure virtual function like the following:
class IRecordingHour{
public:
virtual int getData() const = 0;
}
Now, I have another class that uses the IRecordingHour class:
class ProcessRecordingHours {
public:
ProcessRecordingHours (IRecordingHour &);
proteted:
IRecordingHour & recordingHour;
}
I was told that I am not allowed to implement the IRecordingHour class (the one with the pure virtual function).
My question is: without implementing the IRecordingHour clas, how do I use it in the ProcessingRecordingHours? That is, how do I create an instance of the IRecordingHour and pass it to the constructor of the ProcessRecordingHours?
You should create a subclass of IRecordingHour and implement the method getData, like
class ARecordingHour : public IRecordingHour
{
public:
int getData() const override //override is valid from C++11
{
return 42;
}
}
And then you can do:
ARecordingHour arh{};
ProcessRecordingHours prh{arh}; //{}- Modern C++ initialization
You can find similar examples in a good C++ programming book, such as The C++ Programming Language
Though you equate them, your two questions are in fact quite different.
how do I use it in the ProcessingRecordingHours?
There is no particular problem in implementing ProcessingRecordingHours without implementing a subclass of IRecordingHour. You don't need to do anything special. You simply avoid relying on anything not declared by IRecordingHour, which is no different than you ought to do anyway.
how do I create an instance of the IRecordingHour and pass it to the constructor of the ProcessRecordingHours?
You cannot. A class with a pure virtual method cannot be directly instantiated. Your ProcessRecordingHours can be used in conjunction with classes that extend IRecordingHour, but if you are not permitted to create such a class then you cannot exercise those parts of your ProcessRecordingHours class that depend on an IRecordingHour.
Perhaps, however, you have misunderstood the problem. You may be forbidden from implementing IRecordingHour::getData(), but not from implementing a subclass that overrides that method with a concrete one. Such a subclass could be instantiable, and would be usable in conjunction with ProcessRecordingHours.
I think your teacher plan to inject an implementation of IRecordingHour into ProcessRecordingHours.
Also, you can't use that class unless you generate a stub for IRecordingHour or you implement one yourself with some dummy return values.
/// <summary>
/// in C# and interface can only contain virtual methods. so no need to say virtual.
/// </summary>
interface IRecordingHour
{
int getData();
}
class MockRecordingHour : IRecordingHour
{
public int getData()
{
//just return something. This will be enough to get ProcessRecordingHours to work
return 100;
}
}
/// <summary>
/// this class expects a IRecordingHour.
///
/// how we get a IRecordingHour depends on who's implementing it. You, some 3rd party vendor or another developer who's using this class that you've written.
///
/// Oh wait. Since you're building ProcessRecordingHours, you need a IRecordingHour to get against. You can use a mocking tool or create one yourself that returns some dummy data
/// </summary>
class ProcessRecordingHours
{
private IRecordingHour _recording;
public ProcessRecordingHours(IRecordingHour recording)
{
this._recording = recording;
}
public void DoSomething() {
Console.WriteLine("Recording Data: {0}", this._recording.getData());
}
}
Let's say I am making a game of chess. Would it be more effective to have a base class of Piece()? With sub-classes for each type of piece.
Or, an enum
enum Piece{
King,
Queen,
Knight,
etc;
}
It also brings me onto a common problem I have when refining data.
GameObjects
Piece extends GameObjects
Should I stop here and declare the objects with their individual properties?
Piece King = new Piece("King",5,10); //just made up values, no significance.
Or refine it further and have:
King extends Piece
and then handle King Pieces in a polymorphic way:
Piece king = new King("King,5,10);
thanks
Polymorphism
It depends on how you want to structure the logic of your game, but it probably makes sense to define common behavior (methods) and attributes (fields) in an abstract Piece class and then have each subtype implement abstract methods (or override default methods) and set values of inherited fields based on the ways that they vary. Maybe something like:
public abstract class Piece {
protected int value;
public int getValue() {
return value;
}
public abstract boolean isValidMove(String move);
public void move(String move) {
//common logic here
doMove(move);
}
protected abstract void doMove(String move);
}
public class King extends Piece {
public King() {
this.value = 20;
}
public boolean isValidMove(String move) {
//specific king logic here
}
protected void doMove(String move) {
//more specific king logic
}
}
This would allow you to use polymorphism to define various pieces with a common Piece API, while the important differences are handled by each concrete type.
Piece king = new King();
//...
if(king.isValidMove(move)) king.move(move);
Enums
Enums allow you to create a set of optimized singleton instances of a common type, which can also define behavior, but they don't support overriding/implementing type-specific behavior very well because you end up having to check which enum the current instance when implementing variations. You would also end up with a problem of only having a single KING or PAWN instance when you really need multiples of those for a game (one white king and one black king, 8 white pawns and 8 black pawns).
public enum Piece {
KING(20),
PAWN(1);
private int value;
private Piece(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isValidMove(String move) {
switch(this) {
case KING:
//king specific logic
break;
case PAWN:
//pawn specific logic
break;
}
}
public void move(String move) {
if(this == KING) {
//king specific logic
} else if(this == PAWN) {
//pawn specific logic
}
}
}
So an enum probably wouldn't work very well in this scenario.
Use an enum when there are a limited and defined number if instances.
In this case, clearly there are a defined number of pieces, so use an enum.
Note that enums can have methods just like a regular class, so enums don't have to be just "values", they can do stuff.
I would not try to cram too much into your enum, and in fact I would name it PieceType to make it clear what it represents, and perhaps have a class Piece which is an instance of a piece that has a PieceType, a location (board square) and a color.
The main distinction is that the set of sub-classes is open-ended: you or other people who use your code can create new sub-classes without breaking old code.
Enums are the opposite: other people cannot add new items to your enum and even you, the maintainer of the code, cannot add new enum constants without double-checking every single switch-case statement.
So, use enums if you do not expect new items to be added to the set. Use sub-classes otherwise. This does not mean that sub-classes are better: if you know the set of options to be closed-ended then you should NOT use sub-classes. If you do, you will find yourself writing if (... instanceof ...) chains!
If with enums you find yourself adding new cases to your switch statements (or you can anticipate that) then switch to sub-classes. And if with sub-classes, you find yourself writing instanceof chains, then switch to enums.
I'm writing a program using TypeScript. The problem is I implemented HTMLElement interface.
export class IEElement implements HTMLElement {
// something here
}
The compiler shows many errors that I have some properties missing (IEElement declares an interface but does not implement it). I have implemented about 5 properties that I need to. The rest is redundant. How to avoid errors? Do I need to implement all the interface members?
Yes, you need to implement all non-optional interface members.
The interface is a contract, if you have a class that implements that contract you are promising to implement everything in that contract.
The HTMLElement interface has a lot to implement - but if you just want to add a bit of behaviour, perhaps you could start with an existing implementation...
interface SpecialElement extends HTMLElement {
myCustomFunction: () => void;
}
var element = <SpecialElement>document.getElementById('example');
element.myCustomFunction = function () { };