Intro To Object-Oriented Programming
Intro To Object-Oriented Programming
in Java
Introduction to Programming 2
Topics
Object-Oriented Concepts
Introduction to Programming 2
Object-Oriented Concepts
Object-Oriented Design
Advantages:
Faster development
Increased quality
Easier maintenance
Enhanced modifiability
Class
Blueprint
Introduction to Programming 2
Object-Oriented Concepts
Object
Created every time you instantiate a class using the new keyword.
Attribute
Object-Oriented Concepts
Method
Constructor
Method-like
Introduction to Programming 2
Object-Oriented Concepts
Package
Analogous to a directory
Encapsulation
Abstraction
Object-Oriented Concepts
Inheritance
SuperHero
FlyingSuperHero
UnderwaterSuperHero
Introduction to Programming 2
Object-Oriented Concepts
Polymorphism
Interface
Introduction to Programming 2
Syntax
<classDeclaration> ::=
<modifier> class <name> {
<attributeDeclaration>*
<constructorDeclaration>*
<methodDeclaration>*
}
where
Modifier
Name
class SuperHero {
String superPowers[];
4
5
void printSuperPowers() {
for (int i = 0; i < superPowers.length; i++) {
System.out.println(superPowers[i]);
10
11
}
Introduction to Programming 2
10
Syntax:
<attributeDeclaration> ::=
<modifier> <type> <name> [= <default_value>];
<type> ::=
byte | short | int | long | char | float | double
| boolean | <class>
Introduction to Programming 2
11
String college;
Introduction to Programming 2
12
Syntax:
<methodDeclaration> ::=
<modifier> <returnType> <name>(<parameter>*) {
<statement>*
}
<parameter> ::=
<parameter_type> <parameter_name>[,]
Introduction to Programming 2
13
class MethodDemo {
int data;
int getData() {
return data;
7
8
10
11
12
}
Introduction to Programming 2
14
Syntax:
<constructorDeclaration> ::=
<modifier> <className> (<parameter>*) {
<statement>*
}
where
Modifier
Default constructor
No arguments
Empty body
Introduction to Programming 2
15
class ConstructorDemo {
public ConstructorDemo() {
data = 100;
4
5
ConstructorDemo(int data) {
this.data = data;
8
9
Introduction to Programming 2
16
Syntax:
new <constructorName>(<parameters>)
Example:
1
class ConstructObj {
int data;
ConstructObj() {
/* initialize data */
4
5
8
9
Introduction to Programming 2
17
Dot notation:
<object>.<member>
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
Another example
int intArr = {1, 2, 3, 4, 5};
//Access length attribute
System.out.println(Length: + intArr.length);
Output: Length: 5
Introduction to Programming 2
19
class ConstructObj {
int data;
ConstructObj() {
/* initialize data */
4
5
7
8
10
11
obj.setData(10);
12
13
14
//access setData()
Introduction to Programming 2
20
Introduction to Programming 2
21
22
package registration.reports;
import registration.processing.*;
import java.util.List;
import java.lang.*;
class MyClass {
/* details of MyClass */
6
7
//imported by default
Introduction to Programming 2
23
Introduction to Programming 2
24
Example
1
class Encapsulation {
return false;
5
6
this.secret = secret;
return true;
public getSecret() {
return secret;
10
11
12
Introduction to Programming 2
25
Introduction to Programming 2
26
Syntax:
class <childClassName> extends <parentClassName>
Introduction to Programming 2
27
import java.awt.*;
2
3
class Point {
int x;
int y;
7
8
9
10
Introduction to Programming 2
28
Return type
Method name
Introduction to Programming 2
29
class Superclass {
void display(int n) {
4
5
6
7
10
11
12
13
// continued...
Introduction to Programming 2
30
class OverrideDemo {
public static void main(String args[]) {
15
16
17
18
SubObj.display(3);
19
((Superclass)SubObj).display(4);
}
20
21
Introduction to Programming 2
31
Introduction to Programming 2
32
Based on actual data type of the object that invoked the method
Introduction to Programming 2
33
class Superclass {
void overriddenMethod() {
10
void overriddenMethod() {
11
12
13
//continued...
Introduction to Programming 2
34
/* class Superclass {
15
void overriddenMethod() {
16
17
} */
18
19
20
21
22
23
24
25
}
Introduction to Programming 2
35
Introduction to Programming 2
36
Syntax:
abstract <modifier> <returnType> <name>
(<parameter>*);
Introduction to Programming 2
37
Constructor
static method
Introduction to Programming 2
38
String superPowers[];
this.superPowers = superPowers;
4
5
void printSuperPowers() {
for (int i = 0; i < superPowers.length; i++) {
System.out.println(superPowers[i]);
9
10
11
12
13
//continued...
Introduction to Programming 2
39
System.out.println("Fly...");
4
5
6
7
" creatures...");
10
11
12
13
}
Introduction to Programming 2
40
Syntax:
<interfaceDeclaration> ::=
<modifier> interface <name> {
<attributeDeclaration>*
[<modifier> <returnType> <name>
(<parameter>*);]*
}
Introduction to Programming 2
41
Interface attributes:
Must be initialized
Modifiers:
Must be initialized
Implementing an interface:
42
interface MyInterface {
void iMethod();
2
3
4
5
System.out.println("Interface method.");
7
8
void myMethod() {
System.out.println("Another method.");
10
11
12
13
//continued...
Introduction to Programming 2
43
15
System.out.println("Another implementation.");
16
17
18
19
class InterfaceDemo {
public static void main(String args[]) {
20
21
22
23
mc1.iMethod();
24
mc1.myMethod();
25
mc2.iMethod();
}
26
27
Introduction to Programming 2
44
Introduction to Programming 2
45
Why this?
1. Disambiguate local attribute from a local variable
2. Refer to the object that invoked the non-static method
3. Refer to other constructors
Introduction to Programming 2
46
class ThisDemo1 {
int data;
this.data = data;
/*
9
10
}
Introduction to Programming 2
47
class ThisDemo2 {
int data;
void method() {
System.out.println(data);
4
5
void method2() {
method();
//this.method();
8
9
//this.data
Introduction to Programming 2
48
Method Overloading
Number of parameters
Type of parameters
An example:
class MyClass {
void myMeth() {}
void myMeth(int i) {}
void myMeth(int i, int j) {}
}
Introduction to Programming 2
49
class ThisDemo3 {
int data;
ThisDemo3() {
this(100);
ThisDemo3(int data) {
this.data = data;
8
9
50
Related to inheritance
class Person {
String firstName;
String lastName;
firstName = fname;
lastName = lname;
}
7
8
Introduction to Programming 2
51
//continuation...
class Student extends Person {
11
String studNum;
12
13
super(fname, lname);
14
studNum = sNum;
}
15
16
super()
52
class Superclass{
int a;
void display_a(){
System.out.println("a = " + a);
5
6
7
8
//continued...
Introduction to Programming 2
53
10
int a;
11
void display_a(){
System.out.println("a = " + a);
12
13
14
15
16
17
void display_super_a(){
super.display_a();
18
19
20
}
Introduction to Programming 2
54
class SuperDemo {
public static void main(String args[]){
22
23
24
25
SuperObj.a = 1;
26
SubObj.a = 2;
27
SubObj.set_super_a(3);
28
SuperObj.display_a();
29
SubObj.display_a();
30
SubObj.display_super_a();
31
System.out.println(SubObj.a);
}
32
33
Introduction to Programming 2
55
Introduction to Programming 2
56
Attributes
Methods
Inner classes
57
Class methods
static blocks
Introduction to Programming 2
58
class Demo {
static int a = 0;
4
5
static {
//static block
System.out.println("static block");
a += 1;
}
9
10
11
12
//continued...
Introduction to Programming 2
59
class StaticDemo {
public static void main(String args[]) {
14
15
System.out.println(Demo.a);
16
Demo.staticMethod(5);
17
18
System.out.println(d.a);
19
d.staticMethod(0);
20
21
System.out.println(e.a);
22
d.a += 3;
23
24
25
Introduction to Programming 2
60
Introduction to Programming 2
61
Example:
final int data = 10;
data++;
Introduction to Programming 2
62
final method
Cannot be overridden
Example:
final void myMethod() { //in a parent class
}
void myMethod() { //in a child class
}
final class
Cannot be inherited
Example:
final public class MyClass {}
class WrongClass extends MyClass {}
Introduction to Programming 2
63
Introduction to Programming 2
64
Example:
innerObj.innerMember = 5;
//innerObj is an instance of the inner class
//innerMember is a member of the inner class
Introduction to Programming 2
65
Example:
1
2
3
4
5
6
7
8
class Out {
int OutData;
class In {
void inMeth() {
OutData = 10;
}
}
}
Introduction to Programming 2
66
class OuterClass {
int data = 5;
class InnerClass {
void method() {
System.out.println(data);
System.out.println(data2);
}
8
9
10
11
//continued...
Introduction to Programming 2
67
9
10
11
12
System.out.println(oc.data);
13
System.out.println(ic.data2);
14
ic.method();
}
15
16
Introduction to Programming 2
68
Introduction to Programming 2
69
Summary
Object-Oriented Concepts
Object-Oriented Design
Package
Class
Encapsulation
Object
Abstraction
Attribute
Inheritance
Method
Polymorphism
Constructor
Interface
Introduction to Programming 2
70
Summary
Inheritance
Declaring Attributes
Overriding Methods
Declaring Methods
Declaring a Constructor
Interface
Instantiating a Class
Packages
Encapsulation
Inner Classes
Introduction to Programming 2
71