Java is a Object language, it does not feature all OOP concept but mosts. They are described briefly :
class Dog {
String name;
public Dog() {
name=new String("unknown");
}
}
is equivalent to
class Dog {
String name = "unknown";
}
compiler generate a Default contructor (same as 1st Dog class)
Let's introduce Inheritance
class Animal
{
public void sleep() { System.out.println("sleeping");}
}
class Bird extends Animal
{
public void fly() { System.out.println("flying");}
}
public static main(String args)
{
Bird titi = new Bird();
titi.sleep(); // sleeping
titi.fly(); // flying
}
hineritance is like factorizing ( common code is placed in a mother class , then it is not duplicated)
class Dog extends Animal
{
public void bark() { System.out.println("barking");}
}
public static main(String args)
{
Bird titi = new Bird();
titi.sleep(); // sleeping
titi.fly(); // flying
Dog droopy = new Dog(); droopy.sleep(); // sleeping droopy.bark(); // barking
}
—- function overloading
class Animal
{
public void sleep() { System.out.println("sleeping");}
}
class Bird extends Animal
{
public void fly() { System.out.println("fly over the airs");}
public void reproduce() { System.out.println("eggs"); }
}
class Pinguin extends Bird {
public void fly() { System.out.println("stay on ground & move wings");
}
main() {
Animal tux = new Pinguin();
tux.fly(); // "stay on ground & move wings" // Pingouin.fly() called // ***
tux.reproduce(); // "eggs" // Bird.reproduce() called
tux.sleep(); // "Sleeping" // Animal.sleep() called
}
read it as A pinguin is a Bird that dont fly like a bird
Introduce super keyword
class Human extends Animal {
public void dress() { System.out.println("i am dressed"); }
public void undress() { System.out.println("i am naked"); }
public void sleep() {
undress(); // i am naked
super.sleep(); // sleeping
}
}
OOP Basics explained , see Java
Model View Controller aka MVC : http://www.ootips.org/mvc-pattern.html
Book : modern cpp design, andrei alexandrescu
http://www.dofactory.com/Patterns/Patterns.aspx
Livre http://www.amazon.fr/exec/obidos/ASIN/2841773507/rzr-21#