Download - Oop basics
Internet Programming IIYildiz Technical University 2015
Object Oriented Programming
Ömer Taşkın
OUTLINE
• Class
– Property
– Methods
– Constructor
• Package
• Inheritance
• Interface
• Method Overloading
IP II - OOP 2
Classes
IP II - OOP 3
• Basically it’s a data type
• Holds attributes (variables)
• Has behaviors (methods)
class Person {
String name;
}
class Phone{
int number;
}
Classes
IP II - OOP 4
• Methods can be void() or have return type
class Phone {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
Classes
IP II - OOP 5
• Constructors are initializing method for a Class
• Phone number would be settled when Phone class initialized
class Phone {
private int number;
public Phone(int number) {
this.number = number;
}
}
• Constructors cannot have any return type!
Packages
IP II - OOP 6
• Packages have directory behavior
• Provides hierarchy and grouping
package com.otaskin.domain;
class Member {
//
}
package com.otaskin.domain;
class Product {
//
}
Packages
IP II - OOP 7
• Classes can import each other by package name + class name
• Generated value is called as FQN (Fully Qualified Name)
package com.otaskin;
import com.otaskin.domain.Member;
import com.otaskin.domain.Product;
class App {
//
}
Inheritance
IP II - OOP 8
• Basically its parent / child relationship
• Child has Parent’s behaviors
• extends is the keyword of inheritance
class Phone {
protected int number;
protected void callNumber(int
number) {
// implementation of call
}
}
class DeskPhone extends Phone {
// Phone is parent class of DeskPhone
// Also means DeskPhone is a Phone
// DeskPhone has a number so
// DeskPhone has callNumber behavior
}
Inheritance
IP II - OOP 9
• UML Diagram of extended version
Inheritance
IP II - OOP 10
• Another Example
class Person {
protected String name;
}
class Student extends Person {
// Person is parent class of Student
// Also means Student is a Person
// Student has a name so
}
Interface
IP II - OOP 11
• Interface says what can do
• Does not explain about how can do
• implements is the keyword of interfaces
interface Callable {
// cannot have body
void callNumber(int number);
}
class Phone implements Callable{
// method body is defined here
void callNumber(int number) {
// …
}
}
Overloading
IP II - OOP 12
• In OOP method names can be duplicated
• has to be parameter difference(s) between overloaded methods
interface Callable {
void callNumber(int number);
void callNumber(int number[]);
}
Overloading
IP II - OOP 13
• Also constructors are overloadable
class Person{
private String name;
private int id;
public Person() {}
public Person(String name) {
this.name = name;
}
public Person(int id) {
this.id = id;
}
public Person(String name, int id) {
this.id = id;
this.name = name;
}
}
QUESTIONS
IP II - OOP 14