Basic Object-Oriented
concepts
Scott Lee
leesungki@gmail.com
Targets for Programming
• Repetitive Job
• Complex Process
Who are players?
• Properties
• Operations
Object
• State  Properties, Attributes, Members
• Behavior  Methods, Functions
Class and Object
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);
}
}
Approximate Terminology
• instance = object
• field = variable
• method = function
• sending a message to an object =
calling a function
• These are all approximately true
Reuse
• Similar
• Simpler
• Consistent
Solution
• Inheritance
• Encapsulation
• Polymorphism
• Interface
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.
Example of Encapsulation
class Person {
private String name;
private String age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
} …
}
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
Example of Polymorphism
class SalaryEmployee extends Employee {
private String salary;
public String mailCheck() {
return “checking “ + name + salary”;
} …
}
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
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();
}
}
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
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;
}
Example of a class variable
class Person {
String name;
int age;
static int population;
Person (String name) {
this.name = name;
this.age = 0;
population++;
}
}
The End

Basic Object Oriented Concepts

  • 1.
  • 2.
    Targets for Programming •Repetitive Job • Complex Process
  • 3.
    Who are players? •Properties • Operations
  • 4.
    Object • State Properties, Attributes, Members • Behavior  Methods, Functions
  • 5.
  • 6.
    Example of aclass 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
  • 8.
  • 9.
  • 10.
    Example of inheritance classPerson { 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 classPerson { private String name; private String age; public void setName(String name) { this.name = name; } public String getName() { return name; } … }
  • 12.
    Example of Polymorphism classEmployee 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 classSalaryEmployee extends Employee { private String salary; public String mailCheck() { return “checking “ + name + salary”; } … }
  • 14.
    Example of Polymorphism publicclass 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 interfaceAnimal { 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 makeobjects • 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 aclass variable class Person { String name; int age; static int population; Person (String name) { this.name = name; this.age = 0; population++; } }
  • 19.