Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:

Similar presentations


Presentation on theme: "1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:"— Presentation transcript:

1 1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object: The father of all classes l Casting and Classes

2 2 Inheritance ensures reusability l One of the good features of Java is reusability. That is once a class is defined, it can be used is any application where its method are required. l Java further extends the notion of re- usability through inheritance. l If we need to write a class and if there is an existing class that almost satisfies our requirements, then we do not need to start from scratch. We fully take advantage of the existing class by writing a class that provides only the additional or modified requirements. l The class we write is called an extension, child or sub-class of the existing class. l The existing class is called parent, ancestor, super or base class.

3 3 Example of Inheritance Consider the following Student class: class Student { protected long id; protected String name; protected double gpa; public Student(long id, String name, double gpa) { this.id = id; this.name = name; this.gpa = gpa; } public Student() { this(999999, "No name", 0.0); } public void changeGPA(double newGPA) { gpa = newGPA; } public void print() { System.out.print(id+" "+name+ " "+gpa); }

4 4 Example of Inheritance (Cont’d) Class TestStudent { public static void main (String[] args) { Student s1 = new Student(); s1.print(); Student s2 = new Student(991234,"Al- Ghamdi Ahmed", 3.45); s2.changeGPA(3.75); s2.print(); } l Now suppose we wish to write another class to represent Research Assistant. l Research Assistant is also a student but with the additional field for work load. l Instead of writing another class from the scratch, we extend the Student class as follows.

5 5 Example of Inheritance (Cont’d) class ResearchAssistant extends Student { private int workLoad; // in hours ResearchAssistant(long id, String name, double gpa,int workLoad){ super(id, name, gpa); this.workLoad = workLoad; } ResearchAssistant() { super(); workLoad = 0; } public void print() { super.print(); System.out.print(" " + workLoad); }

6 6 Example of Inheritance (Cont’d) class RunreserchAssistant { public static void main (String[] args) { ResearchAssistant s1 = new ResearchAssistant(); s1.print(); ResearchAssistant s2 = new ResearchAssistant(991234,"Al- Ghamdi Ahmed",3.45,15); s2.changeGPA(3.75); s2.print(); }

7 7 What is actually inherited? l Non-private fields and methods of the parent class are automatically inherited by the sub- class except those that are overridden by the subclass. l If a method is re-defined in the subclass with the same signature as that in the parent class, it is said to override the method in the parent class. l Constructors are not inherited, yet we often want to use the parent's constructor to set up the "parent's part" of the object. l The super reference can be used to refer to the parent class, and is often used to invoke the parent's constructor as shown in the above example. l Giving a field in a class the same name as a field in an ancestor hides the ancestor's field. The field exists, but cannot be accessed by its simple name. If has to be called using the super keyword.

8 8 Overloading vs. Overriding l Overloading deals with multiple methods in the same class with the same name but different signatures l Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature l Overloading lets you define a similar operation in different ways for different data l Overriding lets you define a similar operation in different ways for different object types

9 9 Object: The father of all classes l All classes inherit from the Java standard class, Object. Thus, class Parent { int size; } Is short hand for class Parent extends Object { int size; } Consider the following example: class TestObject { public static void main(String[] args) { Parent watchThis = new Parent(); String s = watchThis.toString(); System.out.println(s); // Where does toString() comes from? }

10 10 Some methods of the Object class clone() Creates a clone of the object. equals(Object) Compares two Objects for equality. Uses "==" to test for equality finalize() Code to perform when this object is garbage collected. getClass() Returns the Class of this Object. hashCode() Returns a hashcode for this Object. toString() Returns a String value of this Object. The toString and equals methods are often overridden when classes are created

11 11 Casting and Classes l Just like Java allows a smaller primitive type (e.g. int) to be assigned to a bigger type (e.g. double), It also allow a reference to a subclass object to be assigned to a reference of super class object. l Similarly, a reference of a super class can be casted to that of a subclass. class Parent { int data; } class Child extends Parent { String name; } class Casting { public static void main( String[] args ) { Object object; Parent parent; Child child = new Child(); parent = child; object = child; parent = (Parent) object; child = (Child) object; }


Download ppt "1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:"

Similar presentations


Ads by Google