6. Example of a class
class Employee {
// fields
String name;
double salary;
// a method
void pay () {
System.out.println("Pay to the order of " +
name + " $" + salary);
}
}
7. Approximate Terminology
• instance = object
• field = variable
• method = function
• sending a message to an object =
calling a function
• These are all approximately true
10. Example of inheritance
class Person {
String name;
String age;
void birthday () {
age = age + 1;
}
}
class Employee
extends Person {
double salary;
void pay () { ...}
}
Every Employee has a name, age, and birthday
method as well as a salary and a pay method.
11. Example of Encapsulation
class Person {
private String name;
private String age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
} …
}
12. Example of Polymorphism
class Employee extends Person {
private String address;
public String mailCheck() {
return “checking “ + name + address”;
} …
}
nt number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; this.address = address; this.number = nu
13. Example of Polymorphism
class SalaryEmployee extends Employee {
private String salary;
public String mailCheck() {
return “checking “ + name + salary”;
} …
}
14. Example of Polymorphism
public class VirtualDemo {
public static void main(String [] args) {
SalaryEmployee s = new SalaryEmployee("Mohd Mohtashim", "Ambehta,
UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("n Call mailCheck using Employee reference--");
e.mailCheck(); }
}
Call mailCheck using Salary reference – 3600
Call mailCheck using Employee reference – Boston, MA
15. Example of Interface
interface Animal {
public void eat();
public void travel();
}
public class MammalInt implements Animal {
public void eat() {
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
16. Concept: Constructors make objects
• Every class has a constructor to make its objects
• Use the keyword new to call a constructor
secretary = new Employee ( );
• You can write your own constructors; but if you don’t,
• Java provides a default constructor with no arguments
– It sets all the fields of the new object to zero
– If this is good enough, you don’t need to write your own
• The syntax for writing constructors is almost like that
for writing methods
17. Syntax for constructors
• Instead of a return type and a name, just use
the class name
• You can supply arguments
Employee (String theName, double theSalary) {
name = theName;
salary = theSalary;
}
18. Example of a class variable
class Person {
String name;
int age;
static int population;
Person (String name) {
this.name = name;
this.age = 0;
population++;
}
}