Вы находитесь на странице: 1из 20

Classes and Objects

A class is a blueprint that defines


the variables and the methods
common to all objects of a
certain kind.

Variable is an item of data named
by an identifier. Each variable has
a type, such as int or Object, and
a scope.

Instance variable is any item of
data that is associated with a
particular object.

An instance method is a function
defined in a class.
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 all
have state and behavior.


Declaring Java Class
Basic syntax of a Java class:

<modifier> class <class_name>
{
[<attribute_declarations>]

[<constructor_declarations>]
[<method_declarations>]
}

Example

public class Vehicle
{
private double maxLoad;
public void setMaxLoad (double
value)
{
maxLoad = value;
}
}
Declaring Attributes
Basic syntax of an attribute:


[<modifier>] <type>
<name> [ = <initial_value];


Examples:

public class Vehicle
{
private int x;
public double maxLoad;
private String name =
Brce Wayne;
}

Declaring Methods
Basic syntax of a method:

[<modifier>] <return_type>
<name>
{
[<argument_list>]
{
[<statements>]
}
}

Examples:

public class Dog {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int
newWeight) {
weight = newWeight;
}
}
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()
Declaring Constructors
Basic syntax of a constructor:

[<modifier>]
<class_name>
([<argument_list>])
{
[<statements>]
}


Example:

public class Dog
{
private int weight;

public Dog()
{
weight = 42;
}

public int
getWeight()
{
return weight;
}
}

Inheritance
Inheritance 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
extends keyword.

When a class inherits from only one
class, it is called single-
inheritance.

Inheritance
Employee
+name : String =
+salary : double
+birthdate : Date
+getDetails() : String
Engineer
Manager
+department : String =
Secretary
Director
+carAllowance : double
+increaseAllowance()
Inheritance
The following is the general syntax
for declaring a class that inherits
from a superclass:

class subClassName
extends superClassName
{
//body of the
subclass
}


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);
}

public void sum() {

System.out.println(i+j+k:
+ (i+j+k));
}
}

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 of
subOb: );
subOb.showij();
subOb.showk();
System.out.println();
System.out.println(Sum of
i, j, and k in subOb: );
subOb.sum();
}
}

Polymorphism
In object-oriented programming,
polymorphism (from the Greek
meaning "having multiple forms") is
the characteristic of being able to
assign a different meaning or usage
to something in different contexts -
specifically, to allow an entity such
as a variable, a function, or an
object to have more than one form.

Polymorphism is the ability to have
many different forms

An object has only one form (the
one that is given to it when
constructed).

A variable is polymorphic because it
can refer to objects of different
forms.

Overloading Method Names
Use as follows:

public void println(int i)
public void println(float f)
public void println(String s)

Argument list must differ

Return types can be different

Exception Handling
All exception types are subclasses
of the built-in class Throwable

Immedaitely below Throwable are
two subclasses which are
Exception and Error

Exception class 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


Try-Catch Block

try {
//code that might throw
a particular exception
} catch
(MyExceptionType eObj) {
//code to execute if a
MyExceptionType exception is
thrown
} catch
(MyExceptionType eObj) {
//code to execute if a
MyExceptionType exception is
thrown
}

Try-Catch Block
public class TestException
{
public static void main(String[]
args){

int d = 0, a = 0;

try
{
d = 0;
a = 42/d;

System.out.println(This will not
be printed);
}
catch (ArithmeticException e)
{

System.out.println(Division by
zero);
}

System.out.println(After the
catch statement);
}
}

Using Throws Clause
The 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 Error or
RunTimeException, or any of their
subclasses

Using Throws Clause
This is the general form of a
method declaration that includes a
throws clause:

type method-name (param
list) throws exception-list
{
// body of method
}

Using the finally Block
finally block can be added at the
end of all catch blocks

It is basically used or cleaning up
purposes, such as closing files and
releasing resources

The finally block is executed after
the execution of all the catch
blocks.