Chapter 1 - Introduction to java
Chapter 1 - Introduction to java
• Everything is objects!
• 5 OOP Principles
• polymorphic expressions
• type abstraction
• separation of concerns
• adaptability
• code reuse
Other oop languages
Static keyword
• The static keyword is used to create fields and methods
that can be accessed without having to make an instance
of the class.
Local Constants:
Constant fields
class MyClass
{
final double E = 2.72;
static final double C = 3e8;
}
OPERATORS AND EXPRESSIONS
Operators And Expressions
class MyRectangle
{
int x, y;
int getArea() { return x * y; }
}
Object creation
To access a class’s fields and methods from outside the defining class, an object of the
class must first be created.
This is done by using the new keyword, which will create a new
object in the system’s memory.
The members of this object can be reached by using the dot operator after the instance
name.
class MyRectangle
{
public int x, y;
public int getArea() { return x * y; }
}
class MyRectangle
{
int x, y;
public MyRectangle() { x = 10; y = 20; }
public static void main(String[] args)
{
MyRectangle r = new MyRectangle();
}
}
Constructor with parameter
• The constructor can have a parameter list, just as any
other method.
• This can be used to make the fields’ initial values
depend on the parameters passed when the object is
created.
class MyRectangle
{
int x, y;
public MyRectangle(int a, int b) { x = a; y = b; }
public static void main(String[] args)
{
MyRectangle r = new MyRectangle(20,15);
}
}
This keyword
• “this” keyword is a reference to the current instance of
the class.
class MyRectangle
{
int x, y;
public MyRectangle(int x, int y)
{
this.x = x; this.y = y;
}
}
Constructor overloading
• To support different parameter lists the
constructor can be overloaded.
class MyRectangle
{
int x, y;
public MyRectangle() { x = 10; y = 20; }
public MyRectangle(int a) { x = a; y = a; }
public MyRectangle(int a, int b) { x = a; y = b; }
}
Encapsulation
• Encapsulation is a mechanism of binding data & code
together
class EncapsulatedBicycle
{
private int speed = 0;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
Abstraction
• Abstraction is the way to skip implementation for some
methods; at the same time giving implementation for some
others
Creating objects:
Bicycle without
Bicycle with fuel
fuel
}
Inheritance
• Types of Inheritance
Overriding members
• A member in a subclass can redefine a member in its
superclass.
class Rectangle
{
public int w = 10, h = 10;
public int getArea() { return w * h; }
}
class Triangle extends Rectangle
{
public int getArea() { return w * h / 2; }
}
Override annotation
• to show that this override was intentional, the
@Override annotation should be placed before the method.
class Triangle extends Rectangle
{
@Override public int getArea()
{
return w * h / 2;
}
}
Hiding members
• This is only true for instance methods, and not for
class methods.
• If a class method called newArea is added to Rectangle,
and redefined in Triangle, then Triangle’s version of
the method will only hide Rectangle’s implementation.
Because of this the @Override annotation is not used.
Example - Hiding members
class Rectangle
{
public int w = 10, h = 10;
public static int newArea(int a, int b) {
return a * b;
}
}
class Triangle extends Rectangle
{
public static int newArea(int a, int b) {
return a * b / 2;
}
}
interface MyInterface {}
Interface
Interface IBicycle
{
void chanceCadence(int newValue);
• Two Parties:
– Handlers – where the exceptions are handled
– Throwers – where the exception can occur
• Use logging
THANK YOU