Ch1-Oop Concepts
Ch1-Oop Concepts
Class
Allows you to define new data types
Blueprint
3
Object-Oriented Concepts
Object
An entity that has a state, behavior and identity with a well-defined
role in problem space
An actual instance of a class
Attribute
Data element of an object
Stores information about the object
A.K.A. Data member, instance variable, property, data field
Student registration system example
4
Object-Oriented Concepts
Method
Describes the behavior of an object
Also called a function or a procedure
Student registration system example
5
Object-Oriented Concepts
Package
Grouping of classes and/or subpackages
Analogous to a directory
Abstraction
Ignoring aspects of a subject not relevant to the current purpose to
focus on those that are
6
Object-Oriented Concepts
Inheritance
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
SuperHero
FlyingSuperHero UnderwaterSuperHero
7
Object-Oriented Concepts
Polymorphism
"poly" means many while "morph" means form
Ability of an object to assume may different forms
8
Java Program Structure:
Declaring Classes
Syntax
<classDeclaration> ::=
<modifier> class <name> {
<attributeDeclaration>*
11
Java Program Structure:
Declaring Attributes
1 public class AttributeDemo {
2 private String studNum;
3 public boolean graduating = false;
4 protected float unitsTaken = 0.0f;
12
Java Program Structure:
Declaring Methods
Syntax:
<methodDeclaration> ::=
<modifier> <returnType> <name>(<parameter>*) {
<statement>*
13
Java Program Structure:
Declaring Methods
1 class MethodDemo {
2 int data;
3 int getData() {
4 return data;
Default constructor
No arguments
Empty body 15
Java Program Structure:
Declaring a Constructor
1 class ConstructorDemo {
2 private int data;
3 public ConstructorDemo() {
4 data = 100;
16
Java Program Structure:
Instantiating a Class
Syntax:
new <constructorName>(<parameters>)
Example:
Dot notation:
<object>.<member>
21
Java Program Structure:
The Access Modifiers
24
Java Program Structure:
Inheritance
1 import java.awt.*;
2
3 class Point {
4 int x;
25
Java Program Structure:
Overriding Methods
Subclass defines a method whose signature is identical to a
method
in the superclass
Signature of a method
26
Java Program Structure:
Overriding Methods
1 class Superclass {
2 void display(int n) {
3 System.out.println("super: " + n);
4 }
28
Java Program Structure:
Overriding Methods
Version of method called
Based on actual data type of the object that invoked the method
29
Java Program Structure:
Overriding Methods
1 class Superclass {
2 void overriddenMethod() {
3 }
4 }
32
Java Program Structure:
Abstract Classes and Methods
abstract keyword is not for:
Constructor
static method
33
Java Program Structure:
Abstract Classes and Methods
1 abstract class SuperHero {
2 String superPowers[];
3 void setSuperPowers(String superPowers[]) {
4 this.superPowers = superPowers;
}
6 void printSuperPowers() {
7 for (int i = 0; i < superPowers.length; i++) {
8 System.out.println(superPowers[i]);
9 }
10 }
11 abstract void displayPower();
12 }
34
13 //continued...
Java Program Structure:
Abstract Classes and Methods
1 class FlyingSuperHero extends SuperHero {
2 void displayPower() {
3 System.out.println("Fly...");
4 }
}
36
Java Program Structure:
Interface
Interface attributes:
Implicitly static and final
Must be initialized
Implementing an interface:
Use implements keyword
Should implement all the interfaces methods
A class can implement several interfaces 37
Java Program Structure:
Interface
1 interface MyInterface {
2 void iMethod();
3 }
4
42
Java Program Structure:
The this Keyword
Method Overloading
Different methods within a class sharing the same name
Parameter lists should differ
Number of parameters
44
Call to this() should be the first statement in constructor
Java Program Structure:
The super Keyword
Related to inheritance
Invoke superclass constructors
Can be used like the this keyword to refer to members of the
superclass
super()
Refers to the immediate superclass
Should be first statement in the subclasss constructor 46
Java Program Structure:
The super Keyword
Referring to superclass members
1 class Superclass{
2 int a;
3 void display_a(){
8 //continued...
47
Java Program Structure:
The super Keyword
9 class Subclass extends Superclass {
10 int a;
11 void display_a(){
12 System.out.println("a = " + a);
Class variables
Behave like a global variable
Can be accessed by all instances of the class
50
Java Program Structure:
The static Keyword
Class methods
May be invoked without creating an object of its class
Can only access static members of the class
Cannot refer to this or super
51
Java Program Structure:
The static Keyword
1 class Demo {
2 static int a = 0;
3 static void staticMethod(int i) {
4 System.out.println(i);
12 //continued...
52
Java Program Structure:
The static Keyword
13 class StaticDemo {
14 public static void main(String args[]) {
15 System.out.println(Demo.a);
16 Demo.staticMethod(5);
54
Java Program Structure:
The final Keyword
final method
Cannot be overridden
Example:
final void myMethod() { //in a parent class
}
final class
Cannot be inherited
Example:
final public class MyClass {}
class WrongClass extends MyClass {} 55
Java Program Structure:
The final Keyword
Keyword may be placed before after other modifiers
public final static void meth() {} or
final public static void meth() {} or ...
//order of modifiers is not important
57
Java Program Structure:
Inner Classes
58
Java Program Structure:
Inner Classes
1 class OuterClass {
int data = 5; class InnerClass {
2
int data2 = 10; void method() {
3
4 System.out.println(data); System.out.println(data2);
10
11 //continued...
59
Java Program Structure:
9 public static void main(String args[]) {
10 OuterClass oc = new OuterClass();
11 InnerClass ic = oc.new InnerClass();
12 System.out.println(oc.data);
System.out.println(ic.data2);
14 ic.method();
15 }
16 }
60