Lecture4-Jan23-Inheritance

Lecture4-Jan23-Inheritance - 1 1 / 71 COP 3503 SPRING 2012...

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

View Full Document Right Arrow Icon
1 / 71 COP 3503 SPRING 2012 Shayan Javed Lecture 4 Programming 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 / 71 Inheritance Derive new classes ( subclass ) from existing ones ( superclass ). Only the Object class (java.lang) has no superclass Every class = subclass of Object .
Background image of page 2
3 / 71 Inheritance Code reuse – methods and properties . Use classes to model objects of the same type Define general class Then define specialized classes.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 / 71 Inheritance Can only inherit from one class. Can inherit from multiple classes in other languages (C++)
Background image of page 4
5 / 71 Example Let’s say you create the following classes: Circle Rectangle Triangle etc. Share certain properties, but also specialized properties
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 / 71 Example public class GeometricObject { private String Color; private String name; private float area; // constructors. .. // get/set methods, etc. }
Background image of page 6
7 / 71 Example public class Circle extends GeometricObject { private double radius; // specific property public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; } // get/set methods, etc. public double getArea() { return radius * radius * Math.PI;
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 / 71 Example An error in the constructor! public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; } Why can’t we use “this.color” and “this.name”?
Background image of page 8
9 / 71 Example They are private properties. NOT accessible by subclasses. Should use setColor() instead. public methods/properties inherited.
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 / 71 Example public class Rectangle extends GeometricObject { private double width, height; // specific properties public Rectangle(double w, double h, String color, String name) { this.height = h; this.width = w; setColor (color); setName (name); } // get/set methods, etc. public double getArea() {
Background image of page 10
11 / 71 The super keyword Are constructors inherited? Yes. super used for: calling a super class constructor calling a super class method
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 / 71 The super keyword Constructors: super() calls the default constructor super(arguments) calls the constructors according to the arguments
Background image of page 12
13 / 71 The super keyword Constructors: public Circle(double radius, String color, String name) { super(color, name); this.radius = radius; }
Background image of page 13

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

View Full DocumentRight Arrow Icon
14 / 71 The super keyword Constructors: public Circle(double radius, String color, String name) { super(color, name); this.radius = radius; } public GeometricObject(String color, String name) { this.color = color; this.name = name; }
Background image of page 14
15 / 71 The super keyword What if we don’t add the super(. .) constructor call?
Background image of page 15

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

View Full DocumentRight Arrow Icon
16 / 71 The super keyword public Circle(double radius, String color, String name) { this.radius = radius; }
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 71

Lecture4-Jan23-Inheritance - 1 1 / 71 COP 3503 SPRING 2012...

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

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