paradigm. • One way of describing the structure of the application • Several paradigms available. – Procedural, Functional, ... • OO is nowdays one of the most popular. – C++ – Java – C# – PHP 5 – ...
reuse it! • Management – Application is programmed using classes and objects. Pieces that communicate with each other. • Maintanence – When changing the code, it does not influence the whole application.
of objects: – cars, buildings, trees, ships, humans, flowers.. • Every object has actions (=methods!) that can incluence other objects – jack drives ferrari. – Object jack has a method drive that influences some way to object ferrari. – jack.drive(ferrari); • In OO, you should implement the app so that it consists of objects that influence each other!
• Datsun 100A has different actions or methods: drive, brake, park... • Datsun 100A has information or attributes: color, amount of gears, amount of doors...
object, Car is a class. • If one wants to create Datsun 100A, you have to have first the blueprints of the Datsun. • Blueprints of an object: Class • Class Car -> Object Datsun 100A
– class to Java class Person { public String firstname; public String lastname; public int age; public String profession; public int phonenumber; public void eat() { System.out.println("Eating!"); } public void sleep() { System.out.println("Sleeping!"); } public void drinkBeer() { System.out.println("Drinking!"); } }
main-method • Let's test the Person – class • This creates a variable a which type is integer – int a; • This creates a object jack which type is Person – Person jack;
class JustTesting { public static void main(String [] args) { Car datsun = new Car(); datsun.amountOfGas = 100; datsun.drive(); System.out.println(datsun.amountOfGas); Car ferrari = new Car(); ferrari.amountOfGas = 300; ferrari.drive(); System.out.println(ferrari.amountOfGas); } }
The reason for this is that other objects cannot change the values as they will • You don't for example want that every object in the world can change person's weight to 500kg...
class JustTesting { public static void main(String [] args) { Person jack = new Person(); jack.name = "Jack Smith"; jack.weight = 500; } } RESULT: TB308POHJUS-L-2:temp pohjus$ javac Person.java Person.java:9: name has private access in Person jack.name = "Jack Smith"; ^ Person.java:10: weight has private access in Person jack.weight = 500; ^ 2 errors
double, float... • Class types are spelled with uppercase – String, Scanner, Person, Cat, Car ... • Primitive type declaring and initialization – int a = 5; • Class type declaring and initialization with new – Dog spot = new Dog();
1; b[1] = 2; int [] a = b; // prints 0x01 System.out.println(b); // prints 0x01 System.out.println(a); RAM address value 0x01 1 0x02 2 address value 0x09 0x01 variable b address value 0x19 0x01 variable a
String is a class type that acts like primitive type • String is the only class type that can be initialized without the new word. – String a = "hello"; • String is passed by value in methods, so String is copied when moving strings in methods.
when an object is created • Java provides default constructor (= constructor with no parameters) • Constructor has the same name than the class • Constructor does not return anything • Constructor usually initalizes class members
something } public Car(String brand) { // Do something else } } class Test { public static void main(String [] args) { Car datsun = new Car(); Car ferrari = new Car("Ferrari"); } }
or more classes where derived class inherites behaviour and attributes of pre-existing (base) classes • Intended to help reuse of existing code with little or no modification
inherit another class, which inherits another class and so on – When changing the base class all the derived classes changes also • Example: – Mammal <– Human <– Worker <- Programmer • Could mammal be a derived class? If so, what would be the base class?
multiple base classes • C++ supports multiple base classes, Java don't Driver - license - Year of approval Conductor - Account number Taxi Driver - area House Boat Houseboat
the base class • public – Is accessible everywhere (base class, derived class, othe classes) • protected – Is accessible by the base class and derived classes
cannot instantiate (create objects) • You can inherit abstract class and create objects from the inherited class, if it is concrete one • Abstract class in C++ has abstract methods, that do not have implementations • These methods forces derived classes to implement those methods
System.out.println("Human sleeps"); } } class Programmer extends Human { } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); jussi.sleep(); // "Human sleeps" } }
sleeps"); } } class Programmer extends Human { public void sleep() { System.out.println("Programmer sleeps"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); jussi.sleep(); // "Programmer sleeps" } }
sleeps"); } } class Programmer extends Human { public void sleep() { super.sleep(); System.out.println("Programmer sleeps"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); jussi.sleep(); } } > java Test Human sleeps Programmer sleeps
} } class Programmer extends Human { public Programmer() { System.out.println("Programmer"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); } } > java Test Human Programmer
Programmer extends Human { public Programmer() { System.out.println("Programmer"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); } } > java Test Human Programmer
this! Calls base classes contructor System.out.println("Human"); } } class Programmer extends Human { public Programmer() { super(); // Java adds this! Calls base classes contructor System.out.println("Programmer"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); } } > java Test Human Programmer
class Programmer extends Human { public Programmer() { System.out.println("Programmer"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); } } > javac Test.java > DOES NOT COMPILE!!! Why?
class Programmer extends Human { public Programmer() { super(); // Java adds this and it calls constructor // Human() that does not exist.. System.out.println("Programmer"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); } }
class Programmer extends Human { public Programmer() { super(5); // Now it works: Human(int a) exists. System.out.println("Programmer"); } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer(); } }
this.name = name; } } class Programmer extends Human { private int salary; public Programmer(String name, int salary) { super(name); this.salary = salary; } } class Test { public static void main(String [] args) { Programmer jussi = new Programmer("Jussi", 5000); } }
• Abstract class is usually used with inheritance • Abstract class may contain abstract methods. • Abstract method forces derived classes to implement the abstract method.
"normal" methods and abstract methods. • Interface holds only abstract methods • Abstract class: – class A extends someAbstractClass • Interface – class A implements someInterface
void stop(); } class Car implements Movable { // You have to implement these public void start() { // Do something } public void stop() { // Do something } }
methods and abstract methods • Interface can hold only abstract methods • Class can inherite only one base class • Class can implement several interfaces!
have to implement these public void start() { // Do something } public void stop() { // Do something } public void reduceGasoline() { // Do something } public void addGasoline() { // Do something } }
{ } class Dog extends Mammal { } class Exercise13 { public static void main(String [] args) { Human jack = new Human(); Dog spot = new Dog(); Mammal mammal = new Mammal(); // these work! You can pass mammals, dogs and humans to the method! myMethod(jack); myMethod(dog); myMethod(mammal); } public static void myMethod(Mammal a) { } }
[] args) { Human jack = new Human(); Dog spot = new Dog(); Mammal mammal = new Mammal(); // these work! You can pass every object to the method myMethod(jack); myMethod(dog); myMethod(mammal); myMethod("hello"); // String } public static void myMethod(Object a) { } }
extends Mammal { public void bark() { System.out.println("Bark!"); }; } class Dog extends Mammal { } class Exercise13 { public static void main(String [] args) { Human jack = new Human(); Dog spot = new Dog(); Mammal mammal = new Mammal(); myMethod(jack); myMethod(dog); myMethod(mammal); } public static void myMethod(Mammal a) { a.bark(); // Why this does not work? } }
{ Human jack = new Human(); Dog spot = new Dog(); Mammal mammal = new Mammal(); myMethod(jack); myMethod(dog); myMethod(mammal); } public static void myMethod(Mammal a) { // Now it works if(a instanceof Dog) { Dog spot = (Dog) a; spot.bark(); } } }
System.out.println("Giving birth"); }; } class Human extends Mammal { } class Dog extends Mammal { } class Exercise13 { public static void main(String [] args) { Human jack = new Human(); Dog spot = new Dog(); Mammal mammal = new Mammal(); myMethod(jack); myMethod(dog); myMethod(mammal); } public static void myMethod(Mammal a) { a.giveBirth(); // Why this works? } }
class Vehicle { } class Car extends Vehicle implements Movable { public void start() { // Do something } public void stop() { // Do something } } class Exercise13 { public static void main(String [] args) { Car c = new Car(); myMethod(c); } // You can pass every object that implements the Movable! public static void myMethod(Movable a) { a.start(); } }