Software Technology - I - PowerPoint PPT Presentation

About This Presentation
Title:

Software Technology - I

Description:

Software Technology - I A Baby is a Monkey Classes Objects Methods (Cont.) Inheritance Forms of Inheritance Single Inheritance A Student is a Person. – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 22
Provided by: free254
Learn more at: http://sky.freeshell.org
Category:

less

Transcript and Presenter's Notes

Title: Software Technology - I


1
Software Technology - I
A Baby is a Monkey
Classes Objects Methods (Cont.)
2
Inheritance
The concept of classes automatically containing
the variables and methods defined in their super
classes is kown as inheritance.
Person
Student
Teacher
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.
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
Parent/Super or Base class
A Student is a Person
subclass/ derived class or child class
5
Defining a Subclass
class subclassname extends superclassname
additional variables additional methods
class Student extends Person byte
year String regNo void showInfo() //
Overriding ....
6
Exercise
  • Complete the showInfo method in the above class
    to show all theinformation of an instace of a
    Student class.
  • Write a constructor for the Student class that
    will initialize all thevariables name, age,
    year and regNo. The constructor should displayan
    error message if the age of a student is found to
    be greater than 24years.

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 gt 24) // produce the
error message System.out.println("Age is
greater than 24") this.regNo
regNo ...
Shoud be the first statement in theconstructor
9
Method Overriding
Overriding allows us to redefine methods in the
super class toobtain 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 bechanged 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 finalmostly 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 goingto be used anymore, it is
removed from the memory. Before theremoval 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 methodto
find the number of objects created when GC is
removing the firstcreated 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 insubclasses. Opposed to
final, if we declare a method as abstract in a
class, we cannot instantiate that class and
always we should subclass and definethe 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("Nima
l", (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.subjec
t "Java" st.showInfo() jTeacher.showInf
o()
Polymorphism!
19
Visibility Control
Access to the members of a class can be limited
by using visibility modifiers of access modifiers.
20
(No Transcript)
21
Exercises
Do all the exercises in your book!
Write a Comment
User Comments (0)
About PowerShow.com