Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004.

Similar presentations


Presentation on theme: "Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004."— Presentation transcript:

1 Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004

2 Inheritance Example 1 class BaseClass { public void doSomething() { System.out.println("BaseClass doSomething"); } class SubClass extends BaseClass { } public class InheritanceExample1 { public static void main(String args[]) { SubClass sc = new SubClass(); sc.doSomething(); BaseClass bc = new SubClass(); bc.doSomething(); }

3 Inheritance Example 2 class BaseClass { public void doSomething() { System.out.println("BaseClass doSomething"); } class SubClass extends BaseClass { public void doSomething() { System.out.println("SubClass doSomething"); } public class InheritanceExample2 { public static void main(String args[]) { SubClass sc = new SubClass(); sc.doSomething(); BaseClass bc = new SubClass(); bc.doSomething(); }

4 Some more terminology A derived class is called a “ subclass ” of the base class. The base class is called the “ superclass ” of a derived class.

5 Example 3 (with super) class BaseClass { public void doSomething() { System.out.println("BaseClass doSomething"); } class SubClass extends BaseClass { public void doSomething() { System.out.print("Super: "); super.doSomething(); System.out.println("SubClass doSomething"); }

6 A (slightly) more concrete example class ChessPiece { public void print() { System.out.println("Generic chess piece"); } class King extends ChessPiece { public void print() { System.out.println("King"); } class Queen extends ChessPiece { public void print() { System.out.println("Queen"); }

7 A (slightly) more concrete example class Bishop extends ChessPiece { public void print() { System.out.println("Bishop"); } public class InheritanceExample4 { public static void main(String args[]) { ChessPiece chessboard[][] = new ChessPiece[8][8]; chessboard[0][2] = new Bishop(); chessboard[0][3] = new Queen(); chessboard[0][4] = new King(); chessboard[0][5] = new Bishop(); for(int i=2; i<=5; i++) chessboard[0][i].print(); }

8 Abstract Classes abstract class ChessPiece { abstract public void print(); }

9 Interfaces interface ChessPiece { public void print(); } class King implements ChessPiece { public void print() { System.out.println("King"); } class Queen implements ChessPiece { public void print() { System.out.println("Queen"); }

10 Difference Between Classes and Interfaces An interface method cannot have a body. (interface methods are implicitly ‘ abstract ’ ) All member variables of an interface are constants (they are implicitly ‘ final ’ ). An abstract class can have bodies for one or more of its methods. An abstract class can have non-constant variables.

11 Other Differences When creating a class: –It can extend at most 1 class (i.e. a class can only have one superclass) –It can implement multiple interfaces

12


Download ppt "Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004."

Similar presentations


Ads by Google