Download presentation
Presentation is loading. Please wait.
1
Class Inheritance (Cont.)
2
Keyword: this Use this to refer to the constructor
Use this to refer to the current object
3
Keyword: this Example 1: Use this to refer to the constructor
public class MyInteger { private int value; public MyInteger(int init) { value = init;} public MyInteger() { this(0);} ... }
4
Keyword: this Example 2: Use this to refer to the current object
public class MyInteger { private int value; public MyInteger(int value) { this.value = value;} public MyInteger() { this(0);} ... }
5
Keyword: super The constructor of the parent class is usually implicitly called However, what if the constructor of the parent class has formal parameters?
6
Keyword: super MyChildInteger integer = new integer (2);
public class MyParentInteger { private int value; public MyParentInteger(int value) {this.value = value;} public MyParentInteger() {this(0);} ...//some other method definitions } public class MyChildInteger extends MyParentInteger { private int value2; public MyChildInteger(int value2) {this.value2 = value2;} public MyChildInteger() {this(0);} ...//some other method definitions } MyChildInteger integer = new integer (2); what is the value of integer.value? How to give it a different value??
7
Keyword: super public class MyParentInteger { private int value;
public MyParentInteger(int value) {this.value = value;} public MyParentInteger() {this(0);} ...//some other method definitions } public class MyChildInteger extends MyParentInteger { private int value2; public MyChildInteger(int value, int value2) super(value); this.value2 = value2; } public MyChildInteger() {this(0);} ...//some other method definitions
8
Keyword: super public class MyParentInteger { private int value;
public MyParentInteger(int value) {this.value = value;} public MyParentInteger() {this(0);} ...//some other method definitions } public class MyChildInteger extends MyParentInteger { private int value2; public MyChildInteger(int value, int value2) super(value); this.value2 = value2; } public MyChildInteger() {this(0);} ...//some other method definitions
9
Abstract Classes
10
Abstract Classes Java allows abstract classes
use the modifier abstract on a class header to declare an abstract class abstract class Vehicle { … } An abstract class is a placeholder in a class hierarchy that represents a generic concept Vehicle Car Boat Plane
11
Abstract Class: Example
An abstract class often contains abstract methods, though it doesn’t have to Abstract methods consist of only methods declarations, without any method body public abstract class Vehicle { String name; public String getName() { return name; } \\ method body abstract public void move(); \\ no body! }
12
Abstract Classes An abstract class often contains abstract methods, though it doesn’t have to Abstract methods consist of only methods declarations, without any method body The non-abstract child of an abstract class must override the abstract methods of the parent An abstract class cannot be instantiated (why?) The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.