Inherited Classes in Java CSCI 392 Ch 6

  • Slides: 13
Download presentation
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly

Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly

Recap from last week… n Scope of Fields and Methods can be n public,

Recap from last week… n Scope of Fields and Methods can be n public, private, protected, … n Constructors n syntax is just like C++ n optional since fields can be initialized n "static" fields and methods belong to the entire class, not individual instances

What is Inheritance? n Building a new class by reusing everything in an existing

What is Inheritance? n Building a new class by reusing everything in an existing class. n Subclasses can add to methods and fields of their base class, and can replace inherited methods. Animal Fish Mammal

Why use Inheritance? n Reuse existing code n Better Maintenance: Correcting/Improving code in the

Why use Inheritance? n Reuse existing code n Better Maintenance: Correcting/Improving code in the base class fixes all the subclasses.

Declaring an Inherited Class public class stack extends list { The new stack class

Declaring an Inherited Class public class stack extends list { The new stack class is a subclass of the list class

What gets Inherited? n All fields marked as "protected" or "public". n "private" fields

What gets Inherited? n All fields marked as "protected" or "public". n "private" fields are only visible to the class that declared them n "protected" fields are only visible to the class that declared them, and any subclasses n All public and protected methods.

public class list { protected int[] values; private int size; . . . public

public class list { protected int[] values; private int size; . . . public class stack extends list {. . . public void some_method () { values[i] = myinteger; // legal size++; // illegal

Overload vs Override Alert n Overloading n creating multiple methods with the same n

Overload vs Override Alert n Overloading n creating multiple methods with the same n example: multiple constructors may have the same name if they have different parameters n Overriding n replacing inherited methods n example: see next page

public class Super. Class { public void method 1 () {. . . }

public class Super. Class { public void method 1 () {. . . } public void method 1 (int param 1) {. . . } public void method 2 () {. . . } } public class Sub. Class extends Super. Class { public void method 1 () {. . . } public void method 2 (int param 2) {. . . } }

public class Super. Class { public void method 1 () {. . . }

public class Super. Class { public void method 1 () {. . . } public void method 1 (int param 1) {. . . } public void method 2 () {. . . } } public class Sub. Class extends Super. Class { public void method 1 () {. . . } public void method 2 (int param 2) {. . . } } Override Overload

public class Super. Class { public void method 1 () {. . . }

public class Super. Class { public void method 1 () {. . . } public void method 1 (int param 1) {. . . } public void method 2 () {. . . } } public class Sub. Class extends Super. Class { public void method 1 () {. . . } public void method 2 (int param 2) {. . . } }. . . Sub. Class bob = new Sub. Class(); bob. method 1(); bob. method 2(); bob. method 1(99);

Using the Parent's Constructor/Method public class Parent. Class { public Parent. Class (int param)

Using the Parent's Constructor/Method public class Parent. Class { public Parent. Class (int param) { do your initializations } public class Child. Class extends Parent. Class { public Child. Class (int param) { super (param); super. method. Name(argument. List); }

Java hierarchy n The Java API uses inheritance extensively in its own classes. n

Java hierarchy n The Java API uses inheritance extensively in its own classes. n The Object class in the java. lang package is the superclass for all classes. n Common methods of the Object class get. Class() Returns a Class object that represents the type of this object. 1/10/2022 Murach’s Java SE 6,