MELJUN CORTES Review JAVA OOP
-
Upload
meljun-cortes-mbampa -
Category
Documents
-
view
225 -
download
0
Transcript of MELJUN CORTES Review JAVA OOP
-
8/11/2019 MELJUN CORTES Review JAVA OOP
1/20
Classes and Objects
A class is a blueprint that defines
the variables and the methodscommon to all objects of acertain kind.
Variable is an item of data named
by an identifier. Each variable hasa type, such as int or Object, anda scope.
Instance variable is any item ofdata that is associated with aparticular object.
An instance method is a functiondefined in a class.
-
8/11/2019 MELJUN CORTES Review JAVA OOP
2/20
Classes and Objects
Objects are principal building blocks
of object-oriented programs.
Each object is a programming unit
consisting of data (instance
variables) and functionality (instance
methods).
Objects are key to understanding
object-oriented technology. They allhave stateand behavior.
-
8/11/2019 MELJUN CORTES Review JAVA OOP
3/20
Declaring Java Class Basic syntax of a Java class:
class
{
[]
[]
[]
}
Example
public class Vehicle
{
private double maxLoad;
public void setMaxLoad (double
value) {
maxLoad = value;
}
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
4/20
Declaring AttributesBasic syntax of an attribute:
[] [ =
-
8/11/2019 MELJUN CORTES Review JAVA OOP
5/20
Declaring Methods Basic syntax of a method:
[]
{ []
{
[]
} }
Examples:
public class Dog {
private int weight;
public int getWeight() {
return weight; }
public void setWeight(intnewWeight) {
weight = newWeight;
}
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
6/20
Encapsulation
Hides the implementation details
of a class
Forces the user to use an
interface to access data
Makes the code more
maintainable
MyDate
-date
+getDay()
+getMonth()+getYear()
+setDay(int)
+setMonth(int)
+setYear()
-validDay()
-
8/11/2019 MELJUN CORTES Review JAVA OOP
7/20
Declaring ConstructorsBasic syntax of a constructor:
[]
([])
{
[]
}
Example:
public class Dog {
private int weight;
public Dog()
{ weight = 42;
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
8/20
InheritanceInheritance is the backbone of
object-oriented programming.
It enables programmers to create a
hierarchy among a group of classes
that have similar characteristics.
To inherent a class, you simply
incorporate the definition of one
class into another by using the
extendskeyword.
When a class inherits from only one
class, it is called single-
inheritance.
-
8/11/2019 MELJUN CORTES Review JAVA OOP
9/20
Inheritance
Employee
+name : String =
+salary : double
+birthdate : Date
+getDetails() : String
Engineer Manager
+department : String = Secretary
Director
+carAllowance : double
+increaseAllowance()
-
8/11/2019 MELJUN CORTES Review JAVA OOP
10/20
InheritanceThe following is the general syntax
for declaring a class that inherits
from a superclass:
class subClassName
extends superClassName
{
//body of the
subclass
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
11/20
Inheritance public class A
{
int i, j;
void showij(){
System.out.println(i
and j: + i + + j);
} }
public class B extends A {
int k;
public void showk() {
System.out.println(k: +k);
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
12/20
Inheritance public class SimpleInheritance {
public static void main(String[]args) {
A superOb = new A(); B subOb = new B();
superOb.i = 10;
superOb.j = 20;
System.out.println(Contents of
superOb: );
superOb.showij();
System.out.println();
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println(Contents ofsubOb: );
subOb.showij();
subOb.showk();
System.out.println();
System.out.println(Sum of
i, j, and k in subOb: ); subOb.sum();
}
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
13/20
PolymorphismIn object-oriented programming,
polymorphism(from the Greek
meaning "having multiple forms") is
the characteristic of being able toassign a different meaning or usage
to something in different contexts -
specifically, to allow an entity such
as a variable, a function, or anobject to have more than one form.
Polymorphismis the ability to have
many different forms
An objecthas only one form (the
one that is given to it when
constructed).
A variableis polymorphic because it
-
8/11/2019 MELJUN CORTES Review JAVA OOP
14/20
Overloading Method NamesUse as follows:
public void println(int i)
public void println(float f)
public void println(String s)
Argument list mustdiffer
Return types can be different
-
8/11/2019 MELJUN CORTES Review JAVA OOP
15/20
Exception HandlingAll exception types are subclasses
of the built-in class Throwable
Immedaitely below Throwableare
two subclasses which are
Except ionand Error
Except ionclass is used for
exceptional conditions that user
programs should catch.
Error defines exceptions that are
not expected to be caught under
normal circumstances by your
program
-
8/11/2019 MELJUN CORTES Review JAVA OOP
16/20
Try-Catch Block
try {
//code that might throwa particular exception
} catch
(MyExceptionType eObj) {
//code to execute if aMyExceptionType exception is
thrown
} catch
(MyExceptionType eObj) {
//code to execute if a
MyExceptionType exception is
thrown
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
17/20
Try-Catch Blockpublic class TestException
{
public static void main(String[]args){
int d = 0, a = 0;
try
{
d = 0;
a = 42/d;
System.out.println(This will notbe printed);
}
catch (ArithmeticException e)
{
System.out.println(Division by
zero);
}
System.out.println(After thecatch statement);
}
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
18/20
Using Throws ClauseThe throws clause is used when a
method that raises an exception
does not handle the exception.
A throws clause lists the types of
exceptions that a method might
throw
This is necessary for all exceptions,
except of those of type Erroror
RunTimeException, or any of their
subclasses
-
8/11/2019 MELJUN CORTES Review JAVA OOP
19/20
Using Throws ClauseThis is the general form of a
method declaration that includes a
throwsclause:
type method-name (param
list) throws exception-list
{
// body of method
}
-
8/11/2019 MELJUN CORTES Review JAVA OOP
20/20
Using the finally Blockfinally block can be added at the
end of all catchblocks
It is basically used or cleaning up
purposes, such as closing files and
releasing resources
Thefinallyblock is executed after
the execution of all the catch
blocks.