inheritance, exceptions - COP 3330 Slides 2/22 Topics...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Slides 2/22 COP 3330
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Inheritance - Basics - instanceof operator - Constructors revisited - Abstract classes and methods - Final classes and methods Exceptions Revisited
Background image of page 2
Inheritance Basics Inheritance defines an is-a relationship Example - Every cat is a mammal - Every mammal is an animal - Every animal is a living organism
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Inheritance Basics What about multiple inheritance? Example - Dogs are mammals - Dogs are pets Multiple inheritance can be confusing and a big hassle so Java disallows it (uses interfaces instead)
Background image of page 4
The extends Keyword The extends keyword means that this class is a subclass of another class Example - public class Cat extends Mammal { . .. }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Inheritance Basics Every class you create in Java extends some other class If you don't use the extends keyword, your class automatically extends Object
Background image of page 6
Inheritance Basics When you extend a class, everything that is part of the superclass is part of your class For example, if you have the class public class Cat extends Mammal then you can call any method on a Cat object that you could call on any Mammal object
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Polymorphism Polymorphism refers to the ability to call different code with the same method call depending on the type of the object Example: Point[] p = new Point[3]; p[0] = new Point2D(5,7); p[1] = new Point3D(1,2,3); p[2] = new Point4D(5,6,7,8); for(int i=0; i<p.length;i++) System.out.println(p[i].magnitude());
Background image of page 8
Polymorphism The term dynamic binding refers to determination of which code to branch to at run-time instead of compile-time
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The instanceof Operator Syntax: expression instanceof type Evaluates to true if the expression can be treated as an instance of the type, false otherwise Always evaluates to false if the expression evaluates to null Only valid with non-primitive types Only valid if the expression could be an object of the type in question (compile error otherwise)
Background image of page 10
The instanceof Operator Example String s = new String( Hi ); Object o = s; System.out.println(s instanceof String); // Prints true System.out.println(o instanceof String); // Prints true System.out.println(s instanceof Object); // Prints true
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 37

inheritance, exceptions - COP 3330 Slides 2/22 Topics...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online