Handout 4 Basic Inheritance Info Hiding Overloading
Handout 4 Basic Inheritance Info Hiding Overloading
inheritance methods
and constructors,
Information Hiding and
method overloading
Java methods
Statements
//Return return_type value/variable // if method //return type is not void it
must have return //statement
}
Class constructors
A class must have the following characteristics
1. Class constructors have same name as class
2. Class constructors are called when object is
created
3. Class constructors don’t return a value so they
cannot return type
• When program creates an instance of a class
(object) java invokes class constructor.
• The constructor is called only once when
object is created with the operator new.
• We have used new when creating Scanner
objects for inputting data. Scanner(System.in)
is constructor for scanner class
Write a program that creates an account object and uses constructor to
initialize the balance and display the balance.
Acount class will have balance class field
Class Fees1 with a constructor
package fees;
public class Fees {
double balance;
String name;
int age;
}
this keyword
This keyword is used when you want to refer to instance of a variable from its class.
Previous fees1 names of constructor were different from those of class variables
Program below need this keyworld to differential constructor parameter names and class variable as the have
same names
Class Fees with a constructor
package fees;
public class Fees {
double balance;
String name;
int age;
Fees(double bal, String name, int age) { // class constructor method
//initializing class variables
/* this enable differentiating balance for class and balance parameter passed to constructor*/
this.balance = balance ;
this.balance = balance ;
this.age= age;
}
public static void main(String[] args) {
Fees f = new Fees(50000, "Mandela", 26); //creating variable
System.out.println("Student name:"+f.name);
}
}
Inheritance - extends
Object
Account
Scanner
Java inheritance & UML symbol
•UML use an arrow with triangle arrow
head
•Lower classes inherit methods form upper
classes as indicated by inheritance symbol
•Employeestudent are students who are
also employeee. E.g. some students may be
employees. staffFalculty - E.g. PhD
students may be lecturers. These are two
examples of double inheritance.
•Java doesn’t support multiple inheritance
C++ Common Lisp support multiple
inheritance.
Java inheritance & UML symbol
•UML use an arrow with triangle arrow
head
•Lower classes inherit methods form upper
classes as indicated by inheritance symbol
•Employeestudent are students who are
also employeee. E.g. some students may be
employees. staffFalculty - E.g. PhD
students may be lecturers. These are two
examples of double inheritance.
•Java doesn’t support multiple inheritance
C++ Common Lisp support multiple
inheritance.
Community members UML class diagram
CommunityMember
-memberName
-memberName
`` Alumus
Student
Employee -memberName
-memberName
-memberName
-memberName -memberName
-memberName
-memberName
EmployeeStudent
`Falculty Staff
-memberName
-memberName -memberName -memberName
-memberName -memberName
StaffFalcuty
-memberName
-memberName
Java and multiple inheritance
•Java doesn’t support multiple
inheritance C++ Common Lisp support
multiple inheritance.
•Multiple inheritance allows a class to
inherit from more than one class
•Java designers thought benefits of
multiple inheritance were not worthy
increased complexity caused by
increased complexity
•C++ and Java designers had different
perspective on benefits and costs of
multiple inheritance.
Java and interfaces
•Java has interfaces feature enables
achieving some goals of multiple
inheritances
•An interface is method without an
implementation
•A class implements an interface by
providing the method implementation
•interface is Java keyword used to
create interfaces
Information hiding
Save as mtotoclass.java
public class mtotoclass {
public static void csub() {
System.out.println("print hello from mtoto class");
} }
Method overloading
ContractEmployee
Manager
-section:string
-department:string -overtime:double
-bonus:double +setSection():void
+setDepartment():void +getSection():string
+getDepartment():string +setovertime():void
+setBonus():void +getOvertime():double
+getBonus():double
Creating Gratuity program
•We implement as four classes
1. GratuitySystemTest that will
contain main class
2. Employee
3. Manager
4. ContractEmployee