KINGDOM OF SAUDI ARABIA | JAZAN UNIVERSITY
COLLEGE OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
ASSIGNMENT
Academic Year 2021-2022 Semester fall
Course with code Elective 1 3**-INFS Section -
Type of Assignment Assignment 1 Marks 10
Date of Announcement - Deadline 3/4/2022
Student Name Ranim Student ID
ASSIGNMENT 1
Answer All Questions
1. Explain the types of classes and give example for each one.
Answer:
Static Class
Final Class
Abstract Class
Concrete Class
Singleton Class
POJO Class
Inner Class
Static Class
In Java, static is a keyword that manage objects in the memory. The static object belongs to the
class instead of the instance of the class.
We can make a class static if and only if it is a nested class. We can also say that static classes are
known as nested classes. It means that a class that is declared as static within another class is
known as a static class. Nested static class does not require reference to the outer class. The
purpose of a static class is to provide the outline of its inherited class.
The properties of the static class are:
1. The class has only static members.
2. It cannot access the member (non-static) of the outer class.
3. We cannot create an object of the static class
public class StaticClassExample
private static String str = "Javatpoint";
//nested class which is a Static class
public static class StaticDemo
//non-static method of Static class
public void show()
System.out.println(str);
public static void main(String args[])
StaticClassExample.StaticDemo obj = new StaticClassExample.StaticDemo();
obj.show();
output
Java point
Final Class
The word final means that cannot be changed. The final class in Java can be declared
using the final keyword. Once we declare a class as final, the values remain the same
throughout the program. The purpose of the final class is to make the class
immutable like the String class. It is only a way to make the class immutable.
Remember that the final class cannot be extended. It also prevents the class from
being sub-classed.
/base class declared as final
final class A
void printmsg()
System.out.print("Base class method is executed.");
//derived class
//extending a final class which is not possible
//it shows the error cannot inherit final class at compile time
class B extends A
void printmsg()
{
System.out.print("Derived class method is executed.");
//main class
public class FinalClassExample
public static void main(String[] arg)
B obj = new B();
obj.printmsg();
Abstract Class
An abstract class is a that is declared with the keyword abstract. The class may or
may not contain abstract methods. We cannot create an instance of an abstract class
but it can be a subclass. These classes are incomplete, so to complete the abstract
class we should extend the abstract classes to a concrete class. When we declare a
subclass as abstract then it is necessary to provide the implementation of abstract
methods. Therefore, the subclass must also be declared abstract. We can achieve data
hiding by using the abstract class. An example of an abstract class is AbstarctMap
class that is a part of the Collections framework.
//abstract class
abstract class MathematicalOperations
int a=30, b=40;
//abstract method
public abstract void add();
public class Operation extends MathematicalOperations
//definition of abstract method
public void add()
System.out.println("Sum of a and b is: "a+b);
public static void main(String args[])
MathematicalOperations obj = new Operation();
obj.add();
output
Sum of a and b is: 70
Concrete Class
These are the regular Java classes. A derived class that provides the basic
implementations for all of the methods that are not already implemented in the base
class is known as a concrete class. In other words, it is regular Java classes in which
all the methods of an abstract class are implemented. We can create an object of the
concrete class directly. Remember that concrete class and abstract class are not the
same. A concrete class may extend its parent class. It is used for specific
requirements.
Let's understand the concept of the concrete class through a program.
public class ConcreteClassExample
//method of the concreted class
static int product(int a, int b)
return a * b;
public static void main(String args[])
//method calling
int p = product(6, 8);
System.out.println("Product of a and b is: " + p);
Output:
Product of a and b is: 48
Singleton Class
A class that has only an object at a time is known as a singleton class. Still, if we are
trying to create an instance a second time, that newly created instance points to the
first instance. If we made any alteration inside the class through any instance, the
modification affects the variable of the single instance, also. It is usually used to
control access while dealing with the database connection and socket programming.
If we want to create a singleton class, do the following:
Create a private constructor.
Create a static method (by using the lazy initialization) that returns the
object of the singleton class.
public class Singleton
private String objectState;
private static Singleton instance = null;
private Singleton() throws Exception
this.objectState = "Javatpoint";
public static Singleton getInstance()
if(instance==null)
try
instance=new Singleton();
catch(Exception e)
e.printStackTrace();
return instance;
public String getObjectState()
return objectState;
}
public void setObjectState(String objectState)
this.objectState = objectState;
Output:
Javatpoint
POJO Class
In Java, POJO stands for Plain Old Java Object. A Java class that contains only
private variables, setter and getter is known as POJO class. It is used to define Java
objects that increase the reusability and readability of a Java program. The class
provides encapsulation. It is widely used in Java because it is easy to understand
these classes. POJO class has the following properties:
It does not extend the predefined classes such as Arrays, HttpServlet, etc.
It cannot contain pre-specified annotations.
It cannot implement pre-defined interfaces.
It is not required to add any constructor.
All instance variables must be private.
The getter/ setter methods must be public.
class PojoDemo
//private variable
private double price=89764.34;
//getter method
public double getPrice()
return price;
//setter method
public void setPrice(int price)
this.price = price;
//main class
public class PojoClassExample
public static void main(String args[])
PojoDemo obj = new PojoDemo();
System.out.println("The price of an article is "+ obj.getPrice()+" Rs.");
}
Output:
The price of an article is 89764.34 Rs.
Inner class
Java allows us to define a class within a class and such classes are known as nested
classes. It is used to group the classes logically and to achieve encapsulation. The
outer class members (including private) can be accessed by the inner class.
The nested classes are of two types:
1. Static Nested class: A class that is static and nested is called a static
nested class. It interacts with the instance member of its outer class. We can
create an object of the static nested class by using the following syntax:
OuterClass.StaticNestedClass nestedObject = new
OuterClass.StaticNestedClass();
2. Non-static Nested Class: Non-static nested classes are called inner
classes.
The general syntax for declaring the static nested class and inner class is as
follows:
public class InnerClassExample
public static void main(String[] args)
System.out.println("This is outer class.");
class InnerClass
public void printinner()
System.out.println("This is inner class.");
OuterClass.java
public class OuterClass
private void getValue()
{
//if you are using Java 7 make the variable final
//if you are using Java 8 the code runs successfully
int sum = 20;
//declaring method local inner class
class InnerClass
public int divisor;
public int remainder;
public InnerClass()
divisor = 4;
remainder = sum%divisor;
private int getDivisor()
return divisor;
private int getRemainder()
return sum%divisor;
private int getQuotient()
{
System.out.println("We are inside the inner class");
return sum / divisor;
//creating an instance of inner class
InnerClass ic = new InnerClass();
System.out.println("Divisor = "+ ic.getDivisor());
System.out.println("Remainder = " + ic.getRemainder());
System.out.println("Quotient = " + ic.getQuotient());
public static void main(String[] args)
//creating an instance of outer class
OuterClass oc = new OuterClass();
oc.getValue();
Output:
Divisor = 4
Remainder = 0
We are inside the inner class
Quotient = 5
Anonymous Inner Class
It is a type of inner class that is the same as local classes but the only difference is
that the class has not a class name and a single object is created of the class. It makes
the code more concise. It is used if we want to use the local class once. We can
create anonymous classes in the following two ways:
By using an interface
By declaring the class concrete and abstract
Syntax:
// the class may an interface, abstract or concrete class
DemoClass obj = new DemoClass()
//methods
//data members
public void demomethod()
//statements
};
Looking at the above syntax, we see that it is the same as the invocation of
constructor except that the class has a definition contained in the block.
AnonymousClassExample.java
interface Score
int run = 321;
void getScore();
public class AnonymousClassExample
public static void main(String[] args)
// Myclass is hidden inner class of Score interface
// whose name is not written but an object to it
// is created.
Score s = new Score()
@Override
public void getScore()
{
//prints score
System.out.print("Score is "+run);
};
s.getScore();
Output:
Score is 321
Wrapper Class
In Java, the term wrapper class represents a collection of Java classes that objectify
the primitive type of Java. It means that for each primitive type there is a
corresponding wrapper class. The wrapper classes are used to perform the conversion
from a primitive type to an object and vice-versa. The following figure demonstrates
the wrapper class hierarchy.
public class WrapperClassExample
public static void main(String args[])
byte x = 0;
//wrapping byte primitive type into Byte object
Byte byteobj = new Byte(x);
int y = 23;
//wrapping int primitive type into Integer object
Integer intobj = new Integer(y);
char c='m';
//wrapping char primitive type into Character object
Character charobj=c;
//printing values from objects
System.out.println("Byte object byteobj: " + byteobj);
System.out.println("Integer object intobj: " + intobj);
System.out.println("Character object charobj: " + charobj);
} }
Output:
Byte object byteobj: 0
Integer object intobj: 23
Character object charobj: m
2. By using java, give example of encapsulation and information hiding.
Answer:
This way data can only be accessed by public methods thus making the private fields and their
implementation hidden for outside classes. That’s why encapsulation is known as data hiding. Lets
see an example to understand this concept better.
Example of Encapsulation in Java
How to implement encapsulation in java:
1) Make the instance variables private so that they cannot be accessed directly from outside the
class. You can only set and get values of these variables through the methods of the class.
2) Have getter and setter methods in the class to set and get the values of the fields.
class EncapsulationDemo{
private int ssn;
private String empName;
private int empAge;
//Getter and Setter methods
public int getEmpSSN(){
return ssn;
public String getEmpName(){
return empName;
public int getEmpAge(){
return empAge;
public void setEmpAge(int newValue){
empAge = newValue;
public void setEmpName(String newValue){
empName = newValue;
public void setEmpSSN(int newValue){
ssn = newValue;
public class EncapsTest{
public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSSN(112233);
System.out.println("Employee Name: " + obj.getEmpName());
System.out.println("Employee SSN: " + obj.getEmpSSN());
System.out.println("Employee Age: " + obj.getEmpAge());
Output:
Employee Name: Mario
Employee SSN: 112233
Employee Age: 32
3. Define the polymorphism with suitable program in java.
Answer:
The word polymorphism means having many forms. In simple words, we can define polymorphism
as the ability of a message to be displayed in more than one form. Real-life Illustration:
Polymorphis
Types of polymorphism
In Java polymorphism is mainly divided into two types:
Compile-time Polymorphism
Runtime Polymorphism
Type 1: Compile-time polymorphism
It is also known as static polymorphism. This type of polymorphism is achieved by function
overloading or operator overloading.
// Java Program for Method overloading
// By using Different Types of Arguments
// Class 1
// Helper class
class Helper {
// Method with 2 integer parameters
static int Multiply(int a, int b)
// Returns product of integer numbers
return a * b;
}
// Method 2
// With same name but with 2 double parameters
static double Multiply(double a, double b)
// Returns product of double numbers
return a * b;
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
// Calling method by passing
// input as in arguments
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
Output:
34.65
4. Compare between hierarchical data model and simple network data model.
Answer:
Hierarchical Data Model Network Data Model
In this model, to store data hierarchy method is In this model, you could create a network that
used. shows how data is related to each other.
It implements 1:1 and 1:n relations It implements 1:1, 1:n and also many to many
relations.
To organize records, it uses tree structure To organize records, it uses graphs.
Insertion anomaly exits in this model i.e. child There is no insertion anomaly.
node cannot be inserted without the parent
node.
It is used to access the data which is complex It is used to access the data which is complex
and asymmetric. and symmetric.
This model lacks data independence. There is partial data independence in this
model.
5. Explain the Relational data model and why is it most popular?
Answer:
Relational databases go hand-in-hand with the development of SQL. The simplicity of SQL -
where even a novice can learn to perform basic queries in a short period of time - is a large part of
the reason for the popularity of the relational model.
The most popular database model is relational. This is employed in most websites and many offline
databases
6. Discuss the benefits of object oriented paradigm.
Answer:
You may be used to breaking down large problems into sub-problems and solving them in separate
units of code. Or you may have experience with functional programming, which treats elements of
code as precise mathematical functions, and prevents them from affecting other elements — that is,
no side effects. Come to grips with OOP, however, and you’ll see that it offers a whole new way of
solving problems.
With OOP, instead of writing a program, you create classes. A class contains both data and
functions. When you want to create something in memory, you create an object, which is an
instance of that class. So, for example, you can declare a Customer class, which holds data and
functions related to customers. If you then want your program to create a customer in memory, you
create a new object of the Customer class.
The advantages of object-oriented programming lie in this kind of encapsulation. Here’s a look at
some of OOP’s top benefits:
1. Modularity for easier troubleshooting
When working with object-oriented programming languages, you know exactly where to
look when something goes wrong. “Oh, the car object broke down? The problem must be in
the Car class!” You don’t have to go line-by-line through all your code.
That’s the beauty of encapsulation. Objects are self-contained, and each bit of functionality
does its own thing while leaving the other bits alone. Also, this modularity allows an IT
team to work on multiple objects simultaneously while minimizing the chance that one
person might duplicate someone else’s functionality.
2. Reuse of code through inheritance
Suppose that in addition to your Car object, one colleague needs a RaceCar object, and
another needs a Limousine object. Everyone builds their objects separately but discover
commonalities between them. In fact, each object is just a different kind of Car. This is
where the inheritance technique saves time: Create one generic class (Car), and then define
the subclasses (RaceCar and Limousine) that are to inherit the generic class’s traits.
Of course, Limousine and RaceCar still have their unique attributes and functions. If the
RaceCar object needs a method to “fireAfterBurners” and the Limousine object requires a
Chauffeur, each class could implement separate functions just for itself. However, because
both classes inherit key aspects from the Car class, for example the “drive” or “fillUpGas”
methods, your inheriting classes can simply reuse existing code instead of writing these
functions all over again.
What if you want to make a change to all Car objects, regardless of type? This is another
advantage of the OOP approach. Make a change to your Car class, and all car objects will
simply inherit the new code.
3. Flexibility through polymorphism
Riffing on this example, you now need just a few drivers, or functions, like “driveCar,”
driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some traits with
LimousineDrivers, but other things, like RaceHelmets and BeverageSponsorships, are
unique.
This is where object-oriented programming’s polymorphism comes into play. Because a
single function can shape-shift to adapt to whichever class it’s in, you could create one
function in the parent Car class called “drive” — not “driveCar” or “driveRaceCar,” but just
“drive.” This one function would work with the RaceCarDriver, LimousineDriver and so
on. In fact, you could even have “raceCar.drive(myRaceCarDriver)” or
“limo.drive(myChauffeur).”
4. Effective problem solving
Many people avoid learning OOP because the learning curve seems steeper than that for
top-down programming. But take the time to master OOP and you’ll find it’s the easier,
more intuitive approach for developing big projects.
Object-oriented programming is ultimately about taking a huge problem and breaking it
down to solvable chunks. For each mini-problem, you write a class that does what you
require. And then — best of all — you can reuse those classes, which makes it even quicker
to solve the next problem.
This isn’t to say that OOP is the only way to write software. But there’s a reason that
languages like C++, C# and Java are the go-to options for serious software development.
7. Define the overloading with suitable program in java.
Answer:
In Java, two or more methods may have the same name if they differ in parameters (different
number of parameters, different types of parameters, or both). These methods are called overloaded
methods and this feature is called method
class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
private static void display(int a, int b){
System.out.println("Arguments: " + a + " and " + b);
public static void main(String[] args) {
display(1);
display(1, 4);
Name of the Course Teacher Malek Al-Zoubi Signature