Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly."— Presentation transcript:

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

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

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

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

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

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

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

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

9 public class SuperClass { public void method1 () {... } public void method1 (int param1) {... } public void method2 () {... } } public class SubClass extends SuperClass { public void method1 () {... } public void method2 (int param2) {... } }

10 public class SuperClass { public void method1 () {... } public void method1 (int param1) {... } public void method2 () {... } } public class SubClass extends SuperClass { public void method1 () {... } public void method2 (int param2) {... } } Override Overload

11 public class SuperClass { public void method1 () {... } public void method1 (int param1) {... } public void method2 () {... } } public class SubClass extends SuperClass { public void method1 () {... } public void method2 (int param2) {... } }... SubClass bob = new SubClass(); bob.method1(); bob.method2(); bob.method1(99);

12 Using the Parent's Constructor/Method public class ParentClass { public ParentClass (int param) { do your initializations } public class ChildClass extends ParentClass { public ChildClass (int param) { super (param); super.methodName(argumentList); }

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


Download ppt "Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly."

Similar presentations


Ads by Google