Module2 - Chapter3 - Core Java
Module2 - Chapter3 - Core Java
MANUAL V8.3
MODULE CODE:
DL.A.01.01
ANUDIP FOUNDATION
Trainer
Manual Core Java
1
Trainer
Manual Core Java
Chapter 3
2
Trainer
Manual Core Java
Chapter 3
3.1 Interface
An interface in Java is an abstract type for defining the behavior of classes. It states what action a class must perform,
and how. A Java interface can be considered as the blueprint of a class. It specifies the methods to be implemented
by a class. Interface methods are abstract by default as they only contain method signatures.
Interface is utilized for achieving complete abstraction, to selectively show and hide object properties from users.
Methods within an interface are public, abstract and contain empty bodies.
An interface is declared with the ‘interface’ keyword in Java. A class has to implement all methods of an interface
Syntax:
interface <interface_name> {
Example of Interface:
import java.io.*;
interface in1
{
final int a = 5;
void display();
3
Trainer
Manual Core Java
{
public void display()
{
System.out.println("Boy");
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
Output:
Boy
5
* Preferred over abstract classes as interfaces contain public, static and final variables. Abstract classes have non-final
variables.
3.2 Aggregation
In Java, association refers to the relationship between two classes. A class can be associated with another through its
4
Trainer
Manual Core Java
objects. Through association, one object can establish a connection to another and use the latter’s features.
‘Aggregation’ is a type of association that represents a Has-A relationship. It is a one-way association. An example
of this relationship is the one between ‘bag’ and ‘coins’. The relationship is one-way because the bag has coins
but the coins do not require the bag to exist. So, the coins class can exist even if the bag class ceases to exist.
An aggregate class possesses a reference to another class and gains ownership of the class. Aggregation is mainly
class Table{
public void show(){
}
}
obj.show();
}
}
Output:
5
Trainer
Manual Core Java
See the example programme of Java interface below. Write a similar programme for the object Boy and value of int
b = 7. Write it again for the object Girl and the value of int c=9. Show the resulting outputs.
import java.io.*;
interface in1
{
final int a = 5;
void display();
{
public void display()
{
System.out.println("Boy");
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
6
Trainer
Manual Core Java
Instructions: The progress of students will be assessed with the exercises mentioned below.
MCQ
a) absolute
b) abstract
c) aggregate
a) methods
b) codes
c) data types
a) abstract
b) absolute
c) assimilated
7
Trainer
Manual Core Java
a) empty
b) semi-full
c) full
a) int
b) interface
c) intersect
a) re-process
b) insinuate
c) instantiate
a) complete
b) semi-full
c) incomplete
8
Trainer
Manual Core Java
a) private
b) public
c) hybrid
a) Had-A
b) Has-A
c) Have-A
a) reusability
b) rejection
c) redevelopment