0% found this document useful (0 votes)
33 views

Java - OOP 2

This document provides an overview of several Java programming concepts including object oriented programming, inner classes, abstract classes, interfaces, ArrayLists, wrapper classes, and exceptions. It explains that inner classes allow grouping of related classes, abstract classes cannot be used to create objects and define common functionality via abstract methods, and interfaces provide a way to achieve abstraction and multiple inheritance. It also describes ArrayLists as resizable arrays, wrapper classes for using primitive types as objects, and how exceptions can be handled using try/catch blocks.

Uploaded by

Maryam Butt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Java - OOP 2

This document provides an overview of several Java programming concepts including object oriented programming, inner classes, abstract classes, interfaces, ArrayLists, wrapper classes, and exceptions. It explains that inner classes allow grouping of related classes, abstract classes cannot be used to create objects and define common functionality via abstract methods, and interfaces provide a way to achieve abstraction and multiple inheritance. It also describes ArrayLists as resizable arrays, wrapper classes for using primitive types as objects, and how exceptions can be handled using try/catch blocks.

Uploaded by

Maryam Butt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Java

Object Oriented Programming


Java Inner Classes
In Java, it is also possible to nest classes (a class
within a class). The purpose of nested classes is to
group classes that belong together, which makes your
code more readable and maintainable.
To access the inner class, create an object of the outer
class, and then create an object of the inner class:
Private Inner Class
Unlike a "regular" class, an inner class can be private or
protected. If you don't want outside objects to access the
inner class, declare the class as private:

NOTE(If you try to access a private inner class from an


outside class (MyMainClass), an error occurs:
MyMainClass.java:12: error: OuterClass.InnerClass has
private access in OuterClass
    OuterClass.InnerClass myInner = myOuter.new
InnerClass();
)
Static Inner Class
An inner class can also be static, which means that you
can access it without creating an object of the outer
class: Example code 1.

Access Outer Class From Inner Class: One


advantage of inner classes, is that they can access
attributes and methods of the outer class:
Java Abstract Classes and Methods
Data abstraction is the process of hiding certain details and
showing only essential information to the user.
Abstraction can be achieved with either abstract classes or
interfaces (which you will learn more about in the next chapter).
The abstract keyword is a non-access modifier, used for classes and
methods:
Abstract class: is a restricted class that cannot be used to create
objects (to access it, it must be inherited from another class).

Abstract method: can only be used in an abstract class, and it does
not have a body. The body is provided by the subclass (inherited
from).
An abstract class can have both abstract and regular methods:
Java Interface
Another way to achieve abstraction in Java, is with
interfaces.
An interface is a completely "abstract class" that is
used to group related methods with empty bodies:
// interface interface Animal { public void
animalSound(); // interface method (does not have a
body) public void run(); // interface method (does not
have a body) }
Notes on Interfaces:
Like abstract classes, interfaces cannot be used to
create objects (in the example above, it is not possible to
create an "Animal" object in the MyMainClass)
Interface methods do not have a body - the body is
provided by the "implement" class
On implementation of an interface, you must override
all of its methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot
be used to create objects)
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only show
the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can
only inherit from one superclass). However, it can be
achieved with interfaces, because the class can implement
multiple interfaces. Note: To implement multiple interfaces,
separate them with a comma (see example below).

Multiple Interfaces : To implement multiple interfaces,


separate them with a comma:
Java ArrayList
Java ArrayList
The ArrayList class is a resizable array, which can be
found in the java.util package.
The difference between a built-in array and an
ArrayList in Java, is that the size of an array cannot be
modified (if you want to add or remove elements
to/from an array, you have to create a new one). While
elements can be added and removed from an ArrayList
whenever you want. The syntax is also slightly
different:
Add Items in Array: The ArrayList class has many useful
methods. For example, to add elements to the ArrayList, use
the add() method: Example 1
Access an Item: To access an element in the ArrayList, use
the get() method and refer to the index number: cars.get(0);
Change an Item: To modify an element, use the set()
method and refer to the index number: cars.set(0, "Opel");
Remove an Item: To remove an element, use the remove()
method and refer to the index number: cars.remove(0);
Remove All: To remove all the elements in the ArrayList,
use the clear() method: cars.clear();
Java Wrapper Classes
Wrapper classes provide a way to use primitive data
types (int, boolean, etc..) as objects.
The table below shows the primitive type and the
equivalent wrapper class:
Primitive Data Types
Creating Wrapper Objects
To create a wrapper object, use the wrapper class instead of the
primitive type. To get the value, you can just print the object:
Since you're now working with objects, you can use certain
methods to get information about the specific object. Example1

For example, the following methods are used to get the value
associated with the corresponding wrapper object: intValue(),
byteValue(), shortValue(), longValue(), floatValue(), doubleValue(),
charValue(), booleanValue(). Exampl2
This example will output the same result as the example above:
Another useful method is the toString() method, which is used to
convert wrapper objects to strings. In the following example, we
convert an Integer to a String, and use the length() method of the
String class to output the length of the "string": Example3
Java Exceptions
When executing Java code, different errors can occur: coding
errors made by the programmer, errors due to wrong input, or
other unforeseeable things.
When an error occurs, Java will normally stop and generate an
error message. The technical term for this is: Java will throw
an exception (throw an error).
Java try and catch
The try statement allows you to define a block of code to be
tested for errors while it is being executed.
The catch statement allows you to define a block of code to be
executed, if an error occurs in the try block.
The try and catch keywords come in pairs:
Consider the following example:
This will generate an error, because myNumbers[10]
does not exist.
public class MyClass { public static void main(String[ ]
args) { int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error! } }

If an error occurs, we can use try...catch to catch the error


and execute some code to handle it
Finally
The finally statement lets you execute code, after
try...catch, regardless of the result:
The throw keyword
The throw statement allows you to create a custom
error.
The throw statement is used together with an
exception type. There are many exception types
available in Java: ArithmeticException,
FileNotFoundException,
ArrayIndexOutOfBoundsException,
SecurityException, etc:

You might also like