I am a java programmer who has started to make apps for iPhone. I have stumbled with a problem. Supose the following code (in java):
Class 1 (building)
public abstract class Building
{
public void Update()
{
specialAction();
}
public abstract void specialAction();
}
Class 2 (House)
public abstract class House extends Building
{
public abstract void specialAction()
{
System.out.println("Special Action Done");
}
}
Runnable Class
public class Program
{
public void main(String[] args)
{
House h = new House();
h.Update();
}
}
When the Update method from House h (running from the superclass Building) is called in Program the method specialAction from House is called and thus the Console prints “Special Action Done”. My question is, would the following code in xcode be equivalent (in terms of inheritance and method overriding rather that console output):
Class 1 (building):
.h file:
-(void)Update;
-(void)SpecialAction;
.m file:
-(void)Update
{
[self specialAction];
}
Class 2 (House, which extends or inherits from Building):
.m file:
-(void)SpecialAction
{
NSLog("Special Action Done");
}
Runnable Class
House* h = [[House alloc] init];
[h Update];
Your example looks correct. A full program of your Java example is approximated in ObjC like so:
Formal abstract methods are not built into the language, but you can approximate it using protocols.
You can consider all ObjC methods to be virtual.