CS 18000 Lecture Notes - Lecture 10: Null Pointer, Call Stack

26 views6 pages
2/28/2017
Inheritance
Problem
Sometimes classes have related or overlapping functionality
Consider a program for keeping track of personnel at the university
Need a Person class to keep information
Inheritance
Rather than duplicating members among these classes, Java allows classes to share
member definitions in a hierarchical fashion
Example
Class Person has fields name, address, as well as accessors and mutators
Class Student “extends” Person
Inherits the fields and methods from Person
Adds classes and grades
Object Class
One designated class in Java-Object- is the root of all classes
Any class that doesn’t extend another class implicitly extends the Object class
A class can only extend one other class (but can implement multiple interfaces)
Object Class Methods
The Object class has a small number of public methods. Samples:
clone()- Makes a copy of the object
equals(Object e)- Compares for equality
toString() - return a String representation
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
Default Constructors
If you don’t provide any constructors in a class, Java provide sone for you:
public ClassName() {
super();
}
The statement “super();)” is a zero argument
Default Chaining
If you DO provide a constructor
By default Java inserts the statement
super();
At the beginning to enforce chaining
Explicit Chaining
The first step in each constructor is to either:
1) Call another constructor in the current class, or
2) Call a superclass constructor
To call another constructor, use this(...)
To call a superclass constructor, use super(...)
Constructor Complications
If the base class doesn’t have a parameterless constructor, the derived class constructor
must make an explicit call, with super(...) to an available constructor in the base class.
Terminology
Student extends Person
Student is a subclass of Person
Person is a superclass of Student
Parent is the parent class, Student is the child class
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Consider a program for keeping track of personnel at the university. Rather than duplicating members among these classes, java allows classes to share member definitions in a hierarchical fashion. Class person has fields name, address, as well as accessors and mutators. One designated class in java-object- is the root of all classes. Any class that doesn"t extend another class implicitly extends the object class. A class can only extend one other class (but can implement multiple interfaces) The object class has a small number of public methods. Samples: clone()- makes a copy of the object equals(object e)- compares for equality tostring() - return a string representation. If you don"t provide any constructors in a class, java provide sone for you: public classname() { super(); The first step in each constructor is to either: call another constructor in the current class, or, call a superclass constructor.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents