Day 1 - OOPS Concepts - Java SE
-
Upload
karan-sharma -
Category
Documents
-
view
13 -
download
1
description
Transcript of Day 1 - OOPS Concepts - Java SE
MAVEN SCIENTISTS
2013
H T T P : / / B L O G . M A V E N S C I E N T I S T S . C O M
2 http://blog.mavenscientists.com OOPS Concepts - Java SE
Contents
1. Object 2. Class 3. Inheritance 4. Interface 5. Package
3 http://blog.mavenscientists.com OOPS Concepts - Java SE
Object
The real world objects have two characteristics:
#Real World Objects
State Behaviour
Example:
Dog – State Behaviour Name Barking
Colour Running
Breed Wagging Tail
Hungry
Similarly, the software Objects have both the state and behaviour.
State – State of an Object is stored in the variables, notably called fields
in Java Language
Behaviour – Behaviour of an Object is expressed through the functions,
notably called methods in Java Language.
#Benefits of Objects
1. Modularity
2. Information – Hiding
3. Code Reuse
4. Pluggability and Debugging Ease
4 http://blog.mavenscientists.com OOPS Concepts - Java SE
Class
In real world, there might be many objects which may belong to a single family.
Example: Bicycle is a class. (General Term) but
Jack’s Bicycle (which is a particular real world object ) is an instance (object )of
the class known as Bicycle.
An object refers to an existing particular thing. Now, the classification of the
type of object is called a class. We can also say that an object belongs to family
of objects, which is known as class.
Example of different Classes:
Car, Human Being, Cat, Dog, Bicycle etc.
Syntax:
class class_name {
}
Syntax Explanation:
Class – Class is a keyword
Class_name – Name of the class (An identifier)
{ } – Called a block. The block is defined by curly braces, which contains various statements. Example: methods, fields etc.
5 http://blog.mavenscientists.com OOPS Concepts - Java SE
Code Example (Class):
class Bicycle {
int speed = 0;
int gear = 1;
void changeGear(int newValue) {
gear = newValue;
}
void speedup (int increment) {
speed += increment;
}
}
Fields (called variables in other
programming languages)
Methods (called functions in other
programming languages)
6 http://blog.mavenscientists.com OOPS Concepts - Java SE
Inheritance
Object Oriented Programming allows to inherit commonly used state
and behaviour from other classes.
Example:
Now, here:
Animal - is the Super Class of all the classes
Mammal and Reptile – are the subclasses of class Animal.
Also Mammal and Reptile are the super classes for the classes Dog, Cat
and Frog, Snake respectively.
Dog , Cat - are the subclasses of class Mammal.
Frog , Snake - are the sub classes of class Reptile.
We can observe that the subclasses have inherited state and behaviour from
their respective super classes.
For example:
Mammals have hair. Hence both Dog and Cat have hair. (Talking about state)
Animal
Mammal Reptile
Dog Cat Frog Snake
Note :- A Super Class in Java has the potential of creating unlimited
subclasses.
7 http://blog.mavenscientists.com OOPS Concepts - Java SE
Syntax:
class Sub_Class_Name extends Super_Class_Name {
}
Syntax Explanation:
class – keyword
Sub_Class_Name - name of sub class
extends – keyword
Super_Class_Name – name of the super class
Code Example (Inheritance):
class Dog extends Mammal {
}
Note :-
The Sub Class has all the same fields and methods as that of the
Super Class. In a Sub Class, you can define additional properties
(state and behaviour) too.
Multiple Inheritance is not allowed in Java. This means, you can not
have more than one Super Class of a single Class.
8 http://blog.mavenscientists.com OOPS Concepts - Java SE
Interface
Methods from the object interface with the outside world. Example:
Power button on your TV, is the interface between you and the electrical
components.
Code Example:
interface Bicycle {
void changeGear();
void speedUp(); note the semicolon
}
To use this interface, you use implements keyword.
Code Example:
class MountainBicycle implements Bicycle {
// the methods changeGear() and speedUp() are defined here
}
Interface methods are public, abstract and never final.
Only prototypes are declared in the interface and no
implementations.
9 http://blog.mavenscientists.com OOPS Concepts - Java SE
Package
Package is a namespace that organizes a set of related classes and
interfaces.
Package can be understood as similar to folders on a PC (Personal Computer).
The Java platform provides an enormous class library (a set of packages)
suitable for use in your own applications. This library is known as the
"Application Programming Interface", or "API" for short.
Java SE 7 API Specification:
http://docs.oracle.com/javase/7/docs/api/index.html