Professional Documents
Culture Documents
Arrays are used typically to group objects of the same type and they are referred to
the group of objects by a common name.
Arrays can be declared of any type either primitive or class.
The declaration of an array creates space for a reference.
Actual memory allocation is done dynamically either by a new statement or by an
array initializer.
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java array.
Programming in Java
Whenever you initialize an Array Variable it will acquire a property called length so
that you can come to know about how many elements it is containing inside.
package Mypackage;
The Java programming language provides an arraycopy() method in the System class,
which is used to copy arrays.
It is a method that only copy references and not objects, when dealing with arrays of
objects.
Let us understand the concept of arraycopy() method by using the following code.
Programming in Java
Copying Arrays : Use of arraycopy()
public class CopyExample {
import java.util.LinkedList;
public class LinkExample {
public static void main(String[] args)
{
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo"); cars.add("BMW"); cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}}
Programming in Java
1) ArrayList internally uses a dynamic array to LinkedList internally uses a doubly linked
store the elements. list to store the elements.
import java.util.HashMap;
Inheritance:
Inheritance can be defined as the process where one class acquires the properties (methods
and fields) of another. With the use of inheritance the information is made manageable in a
hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child
class) and the class whose properties are inherited is known as superclass (base class,
Child parent class). We use extends keyword to implement inheritance. Following is the syntax of
Class/Subclass extends keyword.
class A extends B
{
Parent Class/ Super Class
}
Programming in Java
Inheritance:
In the above example A will be parent or the super class and B is the extended or the sub
class. In such a situation class B can inherit the public or protected or private-protected
members of the class A – however the private members will not be accessible. Inheritance
gives us the more power of code reusability and it is a common practice that we try to keep
the common information within the super class or the parent class.
Majorly Inheritance can be applied in many forms – namely they are Single Inheritance,
Multiple Inheritance, and Multilevel Inheritance- however Java only supports only Single
Inheritance & Multilevel Inheritance using classes. Multiple Inheritance is indirectly
supported in Java with another technique called interface. Now let us discuss about the
various types of inheritance:-
Programming in Java
Inheritance:
Single Inheritance: Single Inheritance occurs when there is only a parent class or the super
class and based on that class another sub class is formed. For e.g. –
class X
{
}
Now using the above class X say we are creating another class called Y like below-
class Y extends X
{}
- here it is an example of Single Inheritance as there is a single super class (X) and there is a
single sub class (Y).
Programming in Java
Inheritance:
Multilevel Inheritance: Multilevel Inheritance occurs when there is only a parent class or
the super class and based on that class another sub class is formed but the super class itself
is a sub class. For e.g. –
class X
{
}
Now using the above class X say we are creating another class called Y like below-
class Y extends X
{
} - here it is an example of Single Inheritance as there is a single super class (X) and there is
a single sub class (Y).
class Z extends Y { }
- here class Z is also a sub class but it is formed based on another class Y which itself is a
sub class of X. So in this case it will be considered as a multilevel inheritance.
Programming in Java
Inheritance:
Multiple Inheritance: As already mentioned Java does not support Multiple Inheritance-
however Multiple Inheritance occurs when a sub class is created using more than 1 parent
classes. In some other languages like C++ it supports Multiple Inheritance. However Java
Supports Multiple Inheritance with the help of interface.
Trainer Will Give Demonstration of Single & Multi Level Inheritance with some
suitable Examples.
Programming in Java
Interface:
An interface is a something like a class that is used to group related methods with empty
bodies. Remember in an interface we can only define some methods but all the methods
will remain empty. Later on we can create a class by using an interface and in that class we
can define the executable codes within the functions or methods. An interface can be
created as follows :-
interface myinterface
{
public void Show();
public void ShowHeading();
}
So in the above case interface keyword is used to create an interface. Now if we want to
use the interface by using another class we have to use a keyword called implements. For
e.g.-
Programming in Java
Interface:
public class newclass implements myinterface
{ int x,y,z;
public newclass() { x=7; y=10; } // Implementing Constructor
public void Show()
{ z=x+y;
System.out.println(“Total = “+String.valueOf(z));
}
public void ShowHeading() // Implementing the Second Function of the interface
{ System.out.println(“Showing Calculation”); }
public static void main(String[] args)
{
newclass n1=new newclass(); n1.ShowHeading();
n1.Show();
} } // End of class
Also it is important to note that once you have implemented an interface then all the
methods declared within that interface must be defined within the class which is using the
same interface. Otherwise there will be compilation error.
Programming in Java
Interface:
Notes on Interfaces:
Interfaces cannot be used to create objects.
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?
To achieve security - hide certain details and only show the important details of an
object (interface).
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.
To implement multiple interfaces, separate them with a comma (see example below).
Programming in Java
Interface:
interface FirstInterface {
public void myMethod(); // interface method
}
interface SecondInterface {
public void myOtherMethod(); // interface method
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}}
class Demo {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod(); myObj.myOtherMethod();
}}
Programming in Java
Lab Assignment:
1. Implement an Interface with two member functions called –pyramid() and flag() and
use these two methods in a different class. The pyramid() will produce the output as –
1
22
333
4444
Where as the flag() will produce the output as-
******
*****
****
***
**
*
2. Implement an interface and with a member function arrange() and Fibonacci(int) -
describe It in a class so that you can input some numbers in an array and arrange them in
ascending order. The other Function Fibonacci() will be able to generate all the Fibonacci
series till the number sent as a parameter.
Programming With Java
End of Day 4