Overview of OOP in Java
1
Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131
Overview of OOP in Java Tessema M. Mengistu Department of Computer - - PowerPoint PPT Presentation
Overview of OOP in Java Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Outline Java Basics Classes and Objects Encapsulation Inheritance
1
Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131
2
3
// this is a java program import java.lang.*; class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
4
– Java supports three types of comment delimiters – The /* and */ - enclose text that is to be treated as a comment by the compiler – // - indicate that the rest of the line is to be treated as a comment by the Java compiler – the /** and */ - the text is also part of the automatic class documentation that can be generated using JavaDoc.
5
– Reserved keywords – Identifiers – Literals – Operators – Separators
6
never be used as identifiers:
abstract assert boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void violate while
7
– Must begin with
– As long as you like!! (until you are tired of typing) – The first letter can not be a digit – Java is case sensitive language
8
– Class names: starts with cap letter and should be inter-cap e.g. StudentGrade – Variable names: start with lower case and should be inter- cap e.g. varTable – Method names: start with lower case and should be inter- cap e.g. calTotal – Constants: often written in all capital and use underscore if you are using more than one word.
9
constant value to be stored in variables – Assigned to variables – Used in expressions – Passed to methods
– Pi = 3.14; // Assigned to variables – C= a * 60; // Used in expressions – Math.sqrt(9.0); // Passed to methods
10
11
Name Symbol
Parenthesis () braces {} brackets [] semicolon ; comma , , period
. 12
13
in a class – defines the ―state‖ and ―behavior‖ of the basic program components known as objects.
behaves like a basic data type ―int‖.
14
15
is a collection
fields (data) and methods (procedure or function) that operate on that data.
class ClassName { [fields declaration] [methods declaration] }
16
Rectangle Length Width Perimeter() area()
17
18
such a class cannot respond to any messages.
immediately after the declaration of data fields.
type MethodName (parameter-list) { Method-body; }
19
Method Body
20
– its name – defined state,
time
– Creating a reference variable
–
Syntax: <class idn><ref. idn>;
– Setting or assigning the reference with the newly created object. – Syntax: <ref. idn> = new <class idn>(…); r1 = new Rectangle(); – The two steps can be done in a single statement Rectangle r1 = new Rectangle();
21
22
– Rectangle aRec,bRec;
itself.
null
null
23
24
25
ObjectName.VariableName ObjectName.MethodName(parameter-list)
26
27
28
Methods Data
29
30
be accessed by anywhere
the enclosing class
31
32
– reuse – enhancement, – adaptation, etc
– Result: lots of duplicate code!
33
– Generalization/Specialization
34
35
{ // body of class } e.g.
class 2D_Shape extends Shape { // class contents }
36
37
38
39
methods and variables, but they have to be only abstract methods and final fields/variables.
it is the responsibility
the class that implements an interface to supply the code for methods.
cannot extend more than one class at a time.
realizing multiple inheritance in Java.
40
interface InterfaceName { // Constant/Final Variable Declaration // Methods Declaration – only method body } interface Shape { public double area( ); }
41
class ClassName implements InterfaceName [, InterfaceName2, …] { // Body of Class }
42
43
method that it invokes
would call the same method every time
called dynamic binding or late binding
44
45
46