Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey.

Similar presentations


Presentation on theme: "Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey."— Presentation transcript:

1 Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey

2 Inheritance The concept of classes automatically containing the variables and methods defined in their super classes is kown as inheritance. Inheritance allows us to extend the capabilities of existing classes and hence it allows us to reuse the existing classes and obtain new classes from them. Person Student Teacher

3 Forms of Inheritance ● Single Inheritance – A Student is a Person. ● Hierarchical Inheritance – A Student is a Person. A Teacher is a Person... ● Multilevel Inheritance – A Student is a Person. An O/L Student is a Student... ● Multiple Inheritance (Not Directly Supported) – A Student is a Person and a Child and a...

4 A Closer Look A Student is a Person Parent/Super or Base class subclass/ derived class or child class

5 Defining a Subclass class Student extends Person { byte year; String regNo; void showInfo() { // Overriding.... } class subclassname extends superclassname { additional variables; additional methods; }

6 Exercise Complete the showInfo method in the above class to show all the information of an instace of a Student class. Write a constructor for the Student class that will initialize all the variables: name, age, year and regNo. The constructor should display an error message if the age of a student is found to be greater than 24 years.

7 Answers for the First Exercise // Here is one possible answer: void showInfo() { System.out.println(name + " is " + age + " years old."); System.out.println(name + " is in year " + year); System.out.println(name + "\'s registration number is " + regNo); } // Here is the prefered answer: void showInfo() { super.showInfo(); // calls the showInfo of super class System.out.println(name + " is in year " + year); System.out.println(name + "\'s registration number is " + regNo); }

8 Answer for the Second Exercise class Student extends Person { byte year; String regNo; Student(String name, byte age, byte year, String regNo) { super(name, age);// calls the super class constructor this.year = year; if(year > 24) {// produce the error message System.out.println("Age is greater than 24"); } this.regNo = regNo; }... } Shoud be the first statement in the constructor

9 Method Overriding Overriding allows us to redefine methods in the super class to obtain different behavior in subclasses. Now both the super class and the subclass have methods with equivalent signatures. If you call an overridden method of of an instance of the subclass, then the super class method is NOT executed. Example: Student st = new Student(...); st.showInfo();// Overridden method in the Student class // will be executed here.

10 Final Variables/Methods If you declare a method as final in a super class, the method cannont be overridden in a subclass. final void showInfo() { // should appear in super class... } If you declare a final variable in a super class, the value of it cannot be changed in subclasses. final int SIZE = 110;// should appear in super class

11 Final Classes A final class cannot be subclassed. You may declare a class to be final mostly for security purposes. final class A {// Declare a final class... } class B extends A {// This does not work!... } final class C extends D { // This will work if D is not a final class....// But C is now final and you cannot say }// class E extends C

12 "finalize()" Method If the Garbage Collector determins that a cirtain object is not going to be used anymore, it is removed from the memory. Before the removal GC calls the finalize() method of the corresponding object. finalize() is a method of the "Object" class and hence all the classes we write may override this method. Next slide illustrates a simple example of using the finalize method to find the number of objects created when GC is removing the first created object.

13 class A {// A simple test class static int i = 0;// Why did I declare this as static? A() {i++; }// The constructor public void finalize() { System.out.println(i + " objects created"); System.exit(0); // Exit when the first object is being removed } class FinalizeTest { public static void main(String[] args) { while(true) {// This loop will never terminate new A();// Instantiate A } Creates lots of A

14 Abstract Methods/Classes If we declare variables/methods as final, they cannot be changed in subclasses. Opposed to final, if we declare a method as abstract in a class, we cannot instantiate that class and always we should subclass and define the abstract method and then we can instantiate the subclass. Abstract methods are simply declarations of methods and they do not have a method body (ie {...}). Subclasses should always define them. Now the subclasses can be instantiated. A class having abstract methods should be declared abstract. We cannot define abstract constructors of abstract static methods.

15 Abstract Methods/Classes (Cont.) abstract class Person { String name; byte age; abstract void showInfo();// No method body void setInfo(String _name, byte _age) { name = _name; age = _age; } We cannot say new Person().

16 Abstract Methods/Classes (Cont.) Here is a Student class: class Student extends Person { byte year; String regNo; void showInfo() {// Define the method body System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Year: " + year); System.out.println("Reg: " + regNo); }

17 Abstract Methods/Classes (Cont.) Here is a Teacher class: class Student extends Person { int epf; String subject; void showInfo() {// Define the method body System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("EPF Number: " + epf); System.out.println("Subject " + subject); }

18 Abstract Methods/Classes (Cont.) class PeopleTest {// Testing our classes public static void main(String[] args) { Student st = new Student(); st.setInfo("Nimal", (byte) 22); st.year = 2; st.regNo = new String("K/2003/34"); Teacher jTeacher = new Teacher(); jTeacher.setInfo("Kamal", (byte)26); jTeacher.epf = 742; jTeacher.subject = "Java"; st.showInfo(); jTeacher.showInfo(); } Polymorphism!

19 Visibility Control Access to the members of a class can be limited by using visibility modifiers of access modifiers.

20

21 Exercises Do all the exercises in your book!


Download ppt "Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey."

Similar presentations


Ads by Google