Professional Documents
Culture Documents
Topics
Introduction to Programming 2
Introduction to Programming 2
Object-Oriented Concepts
Object-Oriented Concepts
Object-Oriented Design
Object
Focuses on object and classes based on real world scenarios Emphasizes state, behavior and interaction of objects Advantages:
An entity that has a state, behavior and identity with a well-defined role in problem space An actual instance of a class Created every time you instantiate a class using the new keyword.
Faster development Increased quality Easier maintenance Enhanced modifiability Increase software reuse
Attribute
Data element of an object Stores information about the object A.K.A. Data member, instance variable, property, data field
Class
Object-Oriented Concepts
Object-Oriented Concepts
Method
Encapsulation/abstraction
Principle of hiding design or implementation information not relevant to the current object
Constructor
Method-like For creating and initializing a new object Not members of an object
Introduction to Programming 2
Introduction to Programming 2
6/20/2013
Object-Oriented Concepts
Object-Oriented Concepts
Inheritance
Polymorphism
Relationship between classes wherein one class is the superclass or the parent class of another Refers to the properties and behaviors received from an ancestor Also know as a "is-a" relationship
"poly" means many while "morph" means form Ability of an object to assume many different forms
Interface
SuperHero
Contract in the form of a collection of method and constant declarations Implementing class promises to follow the contract.
FlyingSuperHero
UnderwaterSuperHero
Introduction to Programming 2
Introduction to Programming 2
Syntax
<modifier> class <name> { <attributeDeclaration>* <constructorDeclaration>* <methodDeclaration>* }
class SuperHero { String superPowers[]; void setSuperPowers(String superPowers[]) this.superPowers = superPowers; } void printSuperPowers() { for (int i = 0; i < superPowers.length; i++) { System.out.println(superPowers[i]); } } }
Introduction to Programming 2 10
<classDeclaration> ::=
where
Modifier
9 10 11
Name
Syntax:
<modifier> <type> <name> [= <default_value>];
public class AttributeDemo { private String studNum; public boolean graduating = false; protected float unitsTaken = 0.0f; String college; }
<attributeDeclaration> ::=
<type> ::= byte | short | int | long | char | float | double boolean | <class> |
5 6
Introduction to Programming 2
11
Introduction to Programming 2
12
6/20/2013
Syntax:
<modifier> <returnType> <name>(<parameter>*) { <statement>* }
class MethodDemo { int data; int getData() { return data; } void setData(int data) { this.data = data; } void setMaxData(int data1, int data2) { data = (data1>data2)? data1 : data2; } }
Introduction to Programming 2 14
<methodDeclaration> ::=
Introduction to Programming 2
13
Syntax:
<modifier> <className> (<parameter>*) { <statement>* }
class ConstructorDemo { private int data; public ConstructorDemo() { data = 100; } ConstructorDemo(int data) { this.data = data; } }
<constructorDeclaration> ::=
where
Modifier
7 8 9
Default constructor
Syntax:
new <constructorName>(<parameters>)
Example:
1. 2. 3. 4. 5. 6. 7. 8. 9.
class ConstructObj { int data; ConstructObj() { /* initialize data */ } public static void main(String args[]) { ConstructObj obj = new ConstructObj(); } }
Introduction to Programming 2 17
An example:
String myString = new String(My String); //Access length method System.out.println(Length: + myString.length()); Output: Length: 9
Introduction to Programming 2
18
6/20/2013
class ConstructObj { int data; ConstructObj() { /* initialize data */ } void setData(int data) { this.data = data; } public static void main(String args[]) { ConstructObj obj = new ConstructObj(); obj.setData(10); } }
Introduction to Programming 2 20
Another example
int intArr = {1, 2, 3, 4, 5}; //Access length attribute System.out.println(Length: + intArr.length); Output: Length: 5
7 8 9 10 11 12 13
//access setData()
Introduction to Programming 2
19
14
Introduction to Programming 2
21
Introduction to Programming 2
24
What if I do not want to allow other classes to modify the value of secret? Answer: Make the setSecret() method private.
class Encapsulation { private int secret; public boolean setSecret(int secret) { if (secret < 1 || secret > 100) return false; this.secret = secret; return true; } public getSecret() { return secret; }
Introduction to Programming 2 25 Introduction to Programming 2 26
6/20/2013
java.awt.*; Point {
int x; int y;
5.}
6.class 7. 8.}
Color color;
Introduction to Programming 2
27
Introduction to Programming 2
28
Subclass defines a method whose signature is identical to a method in the superclass Signature of a method
class Superclass { void display(int n) { System.out.println("super: " + n); } } class Subclass extends Superclass { void display(int k) { System.out.println("sub: " + k); }
4. 5.
9. 10.}
Introduction to Programming 2
29
11.//
continued...
Introduction to Programming 2
30
OverrideDemo { Subclass SubObj = new Subclass(); Superclass SuperObj = SubObj; SubObj.display(3); ((Superclass)SubObj).display(4);
Introduction to Programming 2
31
Introduction to Programming 2
32
6/20/2013
class Superclass { void overriddenMethod() { } } class Subclass1 extends Superclass { public void overriddenMethod() { } } class Subclass2 extends Superclass { void overriddenMethod() { }
Based on actual data type of the object that invoked the method
4. 5. 6. 7. 8. 9.
Same access modifier as that of the overridden method Less restrictive access modifier
13.//continued...
Introduction to Programming 2
34
class Superclass { void overriddenMethod() { } */ Subclass3 extends Superclass { protected void overriddenMethod() { } Subclass4 extends Superclass {
Introduction to Programming 2
35