Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)

Similar presentations


Presentation on theme: "CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)"— Presentation transcript:

1 CS 2430 Day 9

2 Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)

3 Features of OOP Encapsulation Data hiding Abstraction / abstract data type NEW: Inheritance

4 A new keyword: extends public class A { private int x = 0; public void doIt(int y) { x += y; } public void print() { System.out.println(x); } } public class B extends A { // empty! } public static void main(String args[]) { B b = new B(); // The method is inherited: b.doIt(10); // x is printed out! b.print(); }

5 Inheritance example UML: Class B inherits from class A AB Inheritance represents an “is a” relationship public class A {... } public class B extends A {... }

6 Is-a relationship public class Student extends Person {... } public class Square extends Rectangle {... } public class Civic extends Car {... }

7 An example project // Assume Class A and B exist and B a sub-class of A public class C { public static void main(String args[]) { A a = new A(); B b = new B(); a.doIt(5); a.print(); b.doIt(10); b.print(); } }

8 UML for example project Class C uses classes A and B Class B inherits from class A AB C

9 Any questions?

10 Why use inheritance? Basic idea: when you want to create a new class and there is already a class that includes some of the code you want Simply extend the existing class! Can reuse the fields and methods in the existing class Less typing!

11 Some inheritance details

12 Subclass and superclass A class that is derived from another class is called a subclass (a.k.a., child class) The class from which a class is derived is called a superclass (a.k.a., parent class)

13 Subclasses and constructors A subclass inherits all members of its parent Constructors are (technically) NOT members, so they are not inherited by subclasses The constructor of the superclass can (and MUST) be invoked from the subclass using super() –Must be invoked on the first line of the constructor!

14 Any questions?

15 Inheritance example: class Person

16 public class Person { private String name; private int age; // in years public Person(String inName, int inAge) { name = inName; age = inAge; } public void speak() { System.out.print("Hello, my name is: " + name); } public void happyBirthday() { age += 1; } }

17 A subclass of Person : Student

18 public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); // NECESSARY! GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; } }

19 Subclasses can modify public and protected methods from the superclass by overriding them

20 public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; } @Override public void speak() { System.out.println("Hello, my name is " + name + " and my GPA is " + GPA); // syntax error! } }

21 public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; } @Override public void speak() { super.speak(); // can't access name directly! System.out.println(" and my GPA is " + GPA); } }

22 public class Person { protected String name; protected int age; // in years public Person(String inName, int inAge) { name = inName; age = inAge; } public void speak() { System.out.print("Hello, my name is: " + name); } public void happyBirthday() { age += 1; }

23 public class Student extends Person { private float GPA; // grade point average public Student(String inName, int inAge, float inGPA) { super(inName, inAge); GPA = inGPA; } public void setGPA(float inGPA) { GPA = inGPA; } @Override public void speak() { System.out.println("Hello, my name is " + name + " and my GPA is " + GPA); // NOT a syntax error anymore! } }

24 Any questions?


Download ppt "CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)"

Similar presentations


Ads by Google