1 / 12

Class Inheritance (Cont.)

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.

itodd
Download Presentation

Class Inheritance (Cont.)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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 classabstract 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

More Related