Modelleren en Programmeren Jeroen Bransen 18 december 2015 Overerving (inheritance) Constructors Overriding Inheritance demo Exceptions Zelf exceptions veroorzaken
Overerving (inheritance)
2-dimensionaal figuur public class TwoDShape { public double width; public double height; public void showdim() { System.out.println("Width and height are " + width + " and " + height);
2-dimensionaal figuur public class Triangle extends TwoDShape { public String style; public double area() { return width * height / 2; public void showstyle() { System.out.println("Triangle is " + style);
Overerving Een klasse kan een andere klasse uitbreiden En erft dan als het ware alle variabelen en methoden Een klasse kan van maximaal 1 klasse erven In Java doen we dit met extends keyword
Gebruik Triangle public class TriangleTest { public static void main(string[] args) { Triangle t = new Triangle(); t.width = 4.0; t.height = 4.0; t.style = "filled"; t.showstyle(); t.showdim();
Constructors
Constructors public class TwoDShape { public double width; public double height; public TwoDShape(int w, int h) { width = w; height = h; public void showdim() { System.out.println("Width and height are " + width + " and " + height);
Constructors public class Triangle extends TwoDShape { public String style; public double area() { return width * height / 2; public void showstyle() { System.out.println("Triangle is " + style);
Constructors public class Triangle extends TwoDShape { public String style; public Triangle(int w, int h, String s) { super(w, h); style = s; public double area() { return width * height / 2; public void showstyle() { System.out.println("Triangle is " + style);
Constructors Met super kan constructor van ouder-klasse aangeroepen worden
Constructors Met super kan constructor van ouder-klasse aangeroepen worden Dit is verplicht als die klasse geen constructor zonder parameters heeft
Constructors Met super kan constructor van ouder-klasse aangeroepen worden Dit is verplicht als die klasse geen constructor zonder parameters heeft Want: een object kan alleen gemaakt worden met constructor
Constructors Met super kan constructor van ouder-klasse aangeroepen worden Dit is verplicht als die klasse geen constructor zonder parameters heeft Want: een object kan alleen gemaakt worden met constructor super kan ook gebruikt worden om expliciet te maken welke constructor gebruikt moet worden
Overriding
Overriding public class A { public int i, j; public A(int a, int b) { i = a; j = b; // display i and j public void show() { System.out.println("i and j: " + i + " " + j);
Overriding public class B extends A { public int k; public B(int a, int b, int c) { super(a, b); k = c; // display k - this overrides show() in A public void show() { System.out.println("k: " + k);
Overriding public class B extends A { public int k; public B(int a, int b, int c) { super(a, b); k = c; // display k - this overrides show() in A public void show() { super.show(); // this calls A s show() System.out.println("k: " + k);
Overriding Een klasse mag geërfde methoden overschrijven (override) Bij aanroep wordt methode van sub-klasse uitgevoerd Met super kan methode van super-klasse aangeroepen worden
Overriding Een klasse mag geërfde methoden overschrijven (override) Bij aanroep wordt methode van sub-klasse uitgevoerd Met super kan methode van super-klasse aangeroepen worden final voor een methodedefinitie zorgt ervoor dat de methode niet overschreven kan worden
Inheritance demo
Exceptions
Wat gaat hier mis? public class Exceptions { public static void main(string[] args) { int[] getallen = {5, 7, 2, 3; for(int i = 0; i <= getallen.length; i++) { System.out.println(getallen[i]);
Wat gaat hier mis? public class Exceptions { public static void main(string[] args) { int[] getallen = {5, 7, 2, 3; for(int i = 0; i <= getallen.length; i++) { System.out.println(getallen[i]); 5 7 2 3 Exception in thread "main" java.lang.arrayindexoutofboundsexception: 4 at Exceptions.main(Exceptions.java:5)
Exceptions Als er iets mis noemen we dat Exception
Exceptions Als er iets mis noemen we dat Exception Voorbeeld:
Exceptions Als er iets mis noemen we dat Exception Voorbeeld: Deling door 0
Exceptions Als er iets mis noemen we dat Exception Voorbeeld: Deling door 0 Verkeerde index gebruikt
Exceptions Als er iets mis noemen we dat Exception Voorbeeld: Deling door 0 Verkeerde index gebruikt Wanneer het programma teveel geheugen gebruikt
Exceptions Als er iets mis noemen we dat Exception Voorbeeld: Deling door 0 Verkeerde index gebruikt Wanneer het programma teveel geheugen gebruikt etc.
Exceptions Als er iets mis noemen we dat Exception Voorbeeld: Deling door 0 Verkeerde index gebruikt Wanneer het programma teveel geheugen gebruikt etc. We kunnen exceptions afvangen met try... catch
Fouten afvangen public class Exceptions { public static void main(string[] args) { System.out.println("We rekenen nu 5/0 uit"); System.out.println(deling(5, 0)); System.out.println("Klaar!"); public static int deling(int a, int b) { return a / b;
Fouten afvangen public class Exceptions { public static void main(string[] args) { System.out.println("We rekenen nu 5/0 uit"); System.out.println(deling(5, 0)); System.out.println("Klaar!"); public static int deling(int a, int b) { return a / b; We rekenen nu 5/0 uit Exception in thread "main" java.lang.arithmeticexception: / by zero at Exceptions.deling(Exceptions.java:9) at Exceptions.main(Exceptions.java:4)
Fouten afvangen public class Exceptions { public static void main(string[] args) { System.out.println("We rekenen nu 5/0 uit"); System.out.println(deling(5, 0)); System.out.println("Klaar!"); public static int deling(int a, int b) { try { return a / b; catch(exception e) { System.out.println("Er is iets mis gegaan!"); return 0; // we kiezen hier een willekeurige waarde
Try - catch try { // code om uit te voeren hier catch(exception e) { // dit wordt uitgevoerd als iets mis gaat
Try - catch try { // code om uit te voeren hier catch(exception e) { // dit wordt uitgevoerd als iets mis gaat Code in try-blok worden uitgevoerd zoals normaal
Try - catch try { // code om uit te voeren hier catch(exception e) { // dit wordt uitgevoerd als iets mis gaat Code in try-blok worden uitgevoerd zoals normaal Zodra ergens in die code fout optreed wordt code in catch-blok uitgevoerd
Try - catch try { // code om uit te voeren hier catch(exception e) { // dit wordt uitgevoerd als iets mis gaat Code in try-blok worden uitgevoerd zoals normaal Zodra ergens in die code fout optreed wordt code in catch-blok uitgevoerd Zo niet, dan gaat executie verder na het hele blok
Type exceptions Exceptions hebben een type
Type exceptions Exceptions hebben een type Exception is een klasse
Type exceptions Exceptions hebben een type Exception is een klasse ArithmeticException is een sub-klasse daarvan
Type exceptions Exceptions hebben een type Exception is een klasse ArithmeticException is een sub-klasse daarvan ArrayIndexOutOfBoundsException ook
Type exceptions Exceptions hebben een type Exception is een klasse ArithmeticException is een sub-klasse daarvan ArrayIndexOutOfBoundsException ook Met catch kunnen we ook specifieke fouten afvangen
Meerdere exceptions public class Exceptions { public static void main(string[] args) { System.out.println("We rekenen nu 5/0 uit"); try { System.out.println(deling(5, 0)); catch(arrayindexoutofboundsexception e) { System.out.println("Array index onjuist"); catch(arithmeticexception e) { System.out.println("Berekeningsfout"); System.out.println("Klaar!"); public static int deling(int a, int b) { return a / b;
Zelf exceptions veroorzaken
Zelf exceptions veroorzaken Met throw kun je zelf exceptions maken
Zelf exceptions veroorzaken Met throw kun je zelf exceptions maken Argument is een object
Zelf exceptions veroorzaken Met throw kun je zelf exceptions maken Argument is een object Bijvoorbeeld: throw new Exception("Iets ging mis");
Zelf exceptions veroorzaken Met throw kun je zelf exceptions maken Argument is een object Bijvoorbeeld: throw new Exception("Iets ging mis"); Je kunt ook zelf exception-klasses maken
Zelf exceptions veroorzaken public class ThrowDemo { public static void main(string[] args) { try { System.out.println("Before throw."); throw new ArithmeticException(); catch (ArithmeticException exc) { // catch the exception System.out.println("Exception caught."); System.out.println("After try/catch block.");