Elective
Elective
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
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
System.out.println(str);
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.
final class A
void printmsg()
//derived class
//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
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
methods. Therefore, the subclass must also be declared abstract. We can achieve data
//abstract method
obj.add();
output
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.
return a * b;
//method calling
int p = product(6, 8);
Output:
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.
Create a static method (by using the lazy initialization) that returns the
this.objectState = "Javatpoint";
if(instance==null)
try
instance=new Singleton();
catch(Exception e)
e.printStackTrace();
return instance;
return 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
It does not extend the predefined classes such as Arrays, HttpServlet, etc.
//private variable
//getter method
return price;
//setter method
this.price = price;
//main class
Output:
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.
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();
classes.
The general syntax for declaring the static nested class and inner class is as
follows:
public class InnerClassExample
class InnerClass
OuterClass.java
{
//if you are using Java 7 make the variable final
class InnerClass
public InnerClass()
divisor = 4;
remainder = sum%divisor;
return divisor;
return sum%divisor;
oc.getValue();
Output:
Divisor = 4
Remainder = 0
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
By using an interface
Syntax:
//methods
//data members
//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
void getScore();
// is created.
@Override
{
//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
byte x = 0;
//wrapping byte primitive type into Byte object
int y = 23;
char c='m';
Character charobj=c;
} }
Output:
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
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{
return ssn;
return empName;
return empAge;
empName = newValue;
ssn = newValue;
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSSN(112233);
Output:
Employee Age: 32
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
Compile-time Polymorphism
Runtime Polymorphism
// Class 1
// Helper class
class Helper {
return a * b;
}
// Method 2
return a * b;
// Class 2
// Main class
class GFG {
// 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:
In this model, to store data hierarchy method is In this model, you could create a network that
It implements 1:1 and 1:n relations It implements 1:1, 1:n and also many to many
relations.
Insertion anomaly exits in this model i.e. child There is no insertion anomaly.
node.
It is used to access the data which is complex It is used to access the data which is complex
This model lacks data independence. There is partial data independence in this
model.
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 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
The advantages of object-oriented programming lie in this kind of encapsulation. Here’s a look at
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.
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
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
Riffing on this example, you now need just a few drivers, or functions, like “driveCar,”
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
“limo.drive(myChauffeur).”
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,
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
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.
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
class MethodOverloading {
private static void display(int a){
display(1);
display(1, 4);