120 likes | 131 Views
Learn about using "this" keyword to refer to the constructor and current object, using "super" to call parent class constructors, and working with abstract classes in Java.
E N D
Keyword: this • Use this to refer to the constructor • Use this to refer to the current object
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);} • ... • }
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);} • ... • }
Keyword: super • The constructor of the parent class is usually implicitly called • However, what if the constructor of the parent class has formal parameters?
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 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??
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 }
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 }
Abstract Classes • Java allows abstract classes • use the modifier abstract on a class header to declare an abstract classabstract class Vehicle{ … } • An abstract class is a placeholder in a class hierarchy that represents a generic concept Vehicle Car Boat Plane
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! }
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