Object Oriented [[Programming]] [[Uml]]

[[OOP]] IN JAVA

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

Design pattern :

oop.txt · Last modified: 2022/04/16 12:23 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki