0% found this document useful (0 votes)
108 views25 pages

Oops

1. Object-Oriented Programming (OOP) is a programming model organized around objects rather than actions and data. OOP concepts include abstraction, encapsulation, inheritance, and polymorphism. 2. Static members in Java can be blocks, variables, methods, or nested classes. They are associated with the class rather than objects. 3. The key features of the Java language include being simple, object-oriented, portable, platform independent, secure, robust, architecture neutral, dynamic, interpreted, high performance, multithreaded, and distributed.

Uploaded by

5016 V.Gayathri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views25 pages

Oops

1. Object-Oriented Programming (OOP) is a programming model organized around objects rather than actions and data. OOP concepts include abstraction, encapsulation, inheritance, and polymorphism. 2. Static members in Java can be blocks, variables, methods, or nested classes. They are associated with the class rather than objects. 3. The key features of the Java language include being simple, object-oriented, portable, platform independent, secure, robust, architecture neutral, dynamic, interpreted, high performance, multithreaded, and distributed.

Uploaded by

5016 V.Gayathri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

1 Define oops

Object-Oriented Programming (OOP) is a programming language model organized


around objects rather than actions and data
2 Define static member
Static is a non-access modifier in Java which is applicable for the following:
1. Blocks
2. variables
3. methods
4. nested classes
3 List the features of java language.
The features of Java Language are Simple, Object-Oriented, Portable, Platform
independent, Secured, Robust, Architecture neutral, Dynamic, Interpreted, High
Performance, Multithreaded and Distributed.
4 List any four applications of oops
⚫ client-Server Systems.
⚫ Object-Oriented Databases.
⚫ Object-Oriented Databases.
⚫ Real-Time System Design.
⚫ Simulation and Modeling System.
⚫ Hypertext and Hypermedia.
⚫ Neural Networking and Parallel Programming
⚫ Office Automation Systems.

5 Define parameterised Constructor with example


A constructor which has a specific number of parameters is called parameterized
constructor. Parameterized constructor is used to provide different values to the distinct
objects.

6.Define Abstract classes.


Abstract class is a class that has no instances. An abstract class is written with the
expectation that its concrete subclasses will add to its structure and behavior, typically
by implementing its abstract operations.
7 Define the methods provided by object class.

8 Define hybrid inheritance


Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again
Hybrid inheritance is also not directly supported in Java only through interface we can
achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can
ClassA will be acting as the Parent class for ClassB & ClassC and ClassB & ClassC will
be acting as Parent for ClassD.

9 Write short note on final fields


Final keyword can be used along with variables, methods and classes.

1) final variable

2) final method

3) final class

10 How will you compare two strings


String concat (String str) - concatnates specified string
Static String equalsIgnoreCase(String another)- Compare another string.It doesnot check
case.

1. What are the Four Cornerstone of OOP?


Abstraction: Can manage complexity through abstraction. Gives the complete overview
of a particular task and the details are handled by its derived classes. Example : Car.
Encapsulation: Nothing but data hiding, like the variables declared under private of a
particular class is accessed only in that class and cannot access in any other the class.
Inheritance: Is the process in which one object acquires the properties of another object,
ie.,
derived object.
Polymorphism: One method different forms, ie., method overriding and interfaces are the
examples of polymorphism.
2. What are the Data types supported in Java?
Operator in java is a symbol that is used to perform operations like +, -, *, / etc. There
are many types of operators in java which are Unary Operator, Arithmetic Operator,
Shift Operator, Relational Operator, Bitwise Operator, Logical Operator, Ternary
Operator and, Assignment Operator.
3. What are the features of java?
The features of Java Language are Simple, Object-Oriented, Portable, Platform
independent, Secured, Robust, Architecture neutral, Dynamic, Interpreted, High
Performance, Multithreaded and Distributed.
4. How Java supports Platform Independency?
• The meaning of platform independent is that, the java source code can run on all
operating systems. a compiler is a program that translates the source code for
another program from a programming language into executable code.
• This executable code may be a sequence of machine instructions that can be
executed by the CPU directly, or it may be an intermediate representation that is
interpreted by a virtual machine. This intermediate representation in Java is the
Java Byte Code.
• It is the Bytecode that makes it platform independent. This adds to an
important feature
in the JAVA language termed as portability. Every system has its own JVM
which gets installed automatically when the jdk software is installed. For every
operating system separate JVM is available which is capable to read the .class file
or byte code. An important point to be noted is that while JAVA is platform-
independent language, the JVM is platform-dependent
5.Define Objects and Class in Java?
• Class is a collection of data and the function that manipulate the data.The data
components of the class are called data fields and the function components of the
class are called member functions or methods. The class that contains main
function is called main class.
• Object is an instance of a class. The objects represent real world entity. The objects
are used to provide a practical basis for the real world. Objects are used to
understand the real world. The object can be declared by specifying the name of
the class.
6. What is Garbage collection?
Objects are dynamically allocated by using the new operator, dynamically allocated
objects must be manually released by use of a delete operator. Java takes a different
approach; it handles deallocation automatically this is called garbage collection. When
no references to an object exist, that object is assumed to be no longer needed, and the
memory occupied by the object can be reclaimed. Garbage collection only occurs
sporadically (if at all) during the execution of your program. It will not occur simply
because one or more objects exist that are no longer used.
7. What is meant by Inheritance ?
Inheritance can be defined as the procedure or mechanism of acquiring all the properties
and behavior of one class to another, i.e., acquiring the properties and behavior of child
class from the parent class
8. State the difference between Super class and Sub class?
A super class is a class that is inherited whereas sub class is a class that does the
inheriting.
9. What is protected class in Java?
A protected member is accessible within all classes in the same package and within
subclasses in other packages
10. What is an Interface?
Interface is an outside view of a class or object which emphasizes its abstraction while
hiding its structure and secrets of its behavior.
Part b
11. Describe in detail the basic concepts of oops.
object-Oriented Programming (OOP) is a programming language model organized
around objects rather than actions and data. An object-oriented program can be
characterized as data controlling access to code.
Concepts of OOPS:
⚫ Object
⚫ Class
⚫ Inheritance
⚫ Polymorphism
⚫ Abstraction
⚫ Encapsulation
OBJECT
Object means a real word entity such as pen, chair, table etc. Any entity that has state
and behavior is known as an object. Object can be defined as an instance of a class.
CLASS
Collection of objects is called class. It is a logical entity. A class can also be defined as a
blueprint from which you can create an individual object. A class consists of Data
members and methods.The primary purpose of a class is to hold data/information.
INHERITANCE
Inheritance can be defined as the procedure or mechanism of acquiring all the properties
and behavior of one class to another, i.e., acquiring the properties and behavior of child
class from the parent class. When one object acquires all the properties and behaviours of
another object, it is known as inheritance. It provides code reusability and establishes
relationships between different classes. A class which inherits the properties is known as
Child Class(sub-class or derived class) whereas a class whose properties are inherited is
known as Parent class(super-class or base class).
POLYMORPHISM
When one task is performed by different ways i.e. known as polymorphism. For example:
to convince the customer differently, to draw something e.g. shape or rectangle etc.
ABSTRACTION
Abstraction is a process of hiding the implementation details and showing only
functionality to the user. For example: phone call, we don't know the internal
processing.In java, we use abstract class and interface to achieve abstraction.
ENCAPSULATION
Encapsulation in java is a process of wrapping code and data together into a single unit,
for example capsule i.e. mixed of several medicines.A java class is the example of
encapsulation

12 Define Constructor and its types with example program


Constructors are special member functions whose task is to initialize the objects of its
class. It is a special member function, it has the same as the class name.
There are two types of constructors:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
Default constructor (no-arg constructor)
A constructor having no parameter is known as default constructor and no-arg
constructor.
Example:
/* Here, Box uses a constructor to initialize the dimensions of a box. */
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}}
Output:
Constructing Box
Constructing Box
Volume is 1000.0
Volume is 1000.0
Parameterized Constructors
A constructor which has a specific number of parameters is called parameterized
constructor. Parameterized constructor is used to provide different values to the distinct
objects.
Example:
/* Here, Box uses a parameterized constructor to initialize the dimensions of a box. */
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h
; depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}}
class BoxDemo7 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box vol = mybox2.volume();
System.out.println("Volume is " + vol);
}}
Output:
Volume is 3000.0
Volume is 162.0
14 Explain arrays in java.
⚫ Array is a collection of similar type of elements that have contiguous memory
location .In Java all arrays are dynamically allocated. Since arrays are objects in Java,
we can find their length using member length.
⚫ A Java array variable can also be declared like other variables with [] after the data
type.The variables in the array are ordered and each have an index beginning from 0.

⚫ Java array can be also be used as a static field, a local variable or a method
parameter.
⚫ The size of an array must be specified by an int value and not long or short.
⚫ The direct superclass of an array type is Object.Every array type implements the
interfaces Cloneable and java.io.Serializable.
Types of Array in java
1. One- Dimensional Array
2. Multidimensional Array
One-Dimensional Arrays
An array is a group of like-typed variables that are referred to by a common name. An
array declaration has two components: the type and the name. type declares the element
type of the array. The element type determines the data type of each element that
comprises the array.
Syntax:
type var-name[ ];
Instantiation of an Array in java
array-var = new type [size];
Example:
class Testarray{
public static void main(String args[]){
int a[]=new int[5];
//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++)
//length is the property of array
System.out.println(a[i]);
}}
Output:
10
20
70
40
50
Multidimensional Arrays
Multidimensional arrays are arrays of arrays with each element of the array holding the
reference of other array. These are also known as Jagged Arrays. A multidimensional
array is created by appending one set of square brackets ([]) per dimension.
Syntax:
type var-name[ ][ ]=new type[row-size ][col-size ];
Example:
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++) for(j=0; j<5; j++)
{ twoD[i][j] = k; k++;
} for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}}}
Output:
01234
56789
10 11 12 13 14
15 16 17 18 19
16 Explain the concept of inheritance with example
INHERITANCE

Inheritance can be defined as the procedure or mechanism of acquiring all the properties
and behaviors of one class to another, i.e., acquiring the properties and behavior of child
class from the parent class. When one object acquires all the properties and behaviours
of another object, it is known as inheritance. Inheritance represents the IS-A relationship,
also known as parent-child relationship.

Uses of inheritance in java

⚫ For Method Overriding (so runtime polymorphism can be achieved).

⚫ For Code Reusability.

Types of inheritance in java:

⚫ single,

⚫ multilevel and hierarchical inheritance.

⚫ Multiple and hybrid

Syntax:

class subClass extends superClass

//methods and fields

}
SINGLE INHERITANCE

In Single Inheritance one class extends another class (one class only).

Example:

public class ClassA

public void dispA()

System.out.println("disp() method of ClassA");

}
}

public class ClassB extends ClassA

public void dispB()

System.out.println("disp() method of ClassB");

public static void main(String args[])

//Assigning ClassB object to ClassB reference

ClassB b = new ClassB();

//call dispA() method of ClassA

b.dispA();

//call dispB() method of ClassB

b.dispB();

Output :

disp() method of ClassA

disp() method of ClassB

MULTILEVEL INHERITANCE

In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived
class

becomes the base class for the new class.


Example:

public class ClassA

public void dispA()

System.out.println("disp() method of ClassA");

public class ClassB extends ClassA

public void dispB()

System.out.println("disp() method of ClassB");

public class ClassC extends ClassB

public void dispC()

System.out.println("disp() method of ClassC");

public static void main(String args[])

//Assigning ClassC object to ClassC reference


ClassC c = new ClassC();

//call dispA() method of ClassA

c.dispA();

//call dispB() method of ClassB

c.dispB();

//call dispC() method of ClassC

c.dispC();

Output :

disp() method of ClassA

disp() method of ClassB

disp() method of ClassC

HIERARCHICAL INHERITANCE

In Hierarchical Inheritance, one class is inherited by many sub classes.

Example:

public class ClassA

public void dispA()

System.out.println("disp() method of ClassA");

public class ClassB extends ClassA


{

public void dispB()

System.out.println("disp() method of ClassB");

public class ClassC extends ClassA

public void dispC()

System.out.println("disp() method of ClassC");

public class ClassD extends ClassA

public void dispD()

System.out.println("disp() method of ClassD");

public class HierarchicalInheritanceTest

public static void main(String args[])

{
//Assigning ClassB object to ClassB reference

ClassB b = new ClassB();

//call dispB() method of ClassB

b.dispB();

//call dispA() method of ClassA

b.dispA();

//Assigning ClassC object to ClassC reference

ClassC c = new ClassC();

//call dispC() method of ClassC

c.dispC();

//call dispA() method of ClassA

c.dispA();

//Assigning ClassD object to ClassD reference

ClassD d = new ClassD();

//call dispD() method of ClassD

d.dispD();

//call dispA() method of ClassA

d.dispA();

Output :

disp() method of ClassB

disp() method of ClassA

disp() method of ClassC


disp() method of ClassA

disp() method of ClassD

disp() method of ClassA

17 Describe object cloning with an example program


The object cloning is a way to create exact copy of an object. The clone() method of
Object class is used to clone an object. The java.lang.Cloneable interface must be
implemented by the class whose object clone we want to create. If we don't implement
Cloneable interface, clone() method generates CloneNotSupportedException. The
clone() method is defined in the Object class.

Syntax of the clone() method:

protected Object clone() throws CloneNotSupportedException

The clone() method saves the extra processing task for creating the exact copy of an
object. If we perform it by using the new keyword, it will take a lot of processing time to
be performed that is why we use object cloning.

Advantage of Object cloning

⚫ You don't need to write lengthy and repetitive codes. Just use an abstract class with
a 4- or 5-line long clone() method.

⚫ It is the easiest and most efficient way for copying objects, especially if we are
applying it to an already developed or an old project. Just define a parent class,
implement Cloneable in it, provide the definition of the clone() method and the task
will be done.

⚫ Clone() is the fastest way to copy array.

Disadvantage of Object cloning

• To use the Object.clone() method, we have to change a lot of syntaxes to our code, like
implementing a Cloneable interface, defining the clone() method and handling
CloneNotSupportedException, and finally, calling Object.clone() etc.

⚫ We have to implement cloneable interface while it doesn?t have any methods in it.
We just have to use it to tell the JVM that we can perform clone() on our object.
⚫ Object.clone() is protected, so we have to provide our own clone() and indirectly
call Object.clone() from it. Object.clone() doesn?t invoke any constructor so we
don?t have any control over object construction.

EXAMPLE

class Student implements Cloneable{

int rollno;

String name;

Student(int rollno,String

name){ this.rollno=rollno;

this.name=name;

public Object clone()throws CloneNotSupportedException{

return super.clone();

public static void main(String args[]){

try{

Student s1=new Student(101,"amit");

Student s2=(Student)s1.clone();

System.out.println(s1.rollno+" "+s1.name);

System.out.println(s2.rollno+" "+s2.name);

catch(CloneNotSupportedException c){}

Output:
101 amit

101 amit

13. Explain in detail about Access specifier in Java?


The access modifiers in java specifies accessibility (scope) of a data member, method,
constructor or class.

There are 4 types of java access modifiers:

1. private

2. default

3. protected

4. public

1) Private Access Modifier

The private access modifier is accessible only within class.

Simple example of private access modifier

In this example, we have created two classes A and Simple. A class contains private data
member and

private method. We are accessing these private members from outside the class, so there is
compile time

error.

class A{

private int data=40;

private void msg(){System.out.println("Hello java");}

public class Simple{

public static void main(String args[]){


A obj=new A();

System.out.println(obj.data);//Compile Time Error

obj.msg();//Compile Time Error

2) Default Access Modifier

If you don't use any modifier, it is treated as default bydefault. The default modifier is
accessible only within package.

Example:

In this example, we have created two packages pack and mypack. We are accessing the A class
from outside its package, since A class is not public, so it cannot be accessed from outside the
package.

//save by A.java

package pack;

class A{

void msg(){System.out.println("Hello");}

//save by B.java

package mypack;

import pack.*;

class B{

public static void main(String args[]){

A obj = new A();//Compile Time Error

obj.msg();//Compile Time Error

}
}

In the above example, the scope of class A and its method msg() is default so it cannot be
accessed from

outside the package.

3) Protected Access Modifier

The protected access modifier is accessible within package and outside the package but
through inheritance

only

The protected access modifier can be applied on the data member, method and constructor. It
can't be applied on the class.

Example:

In this example, we have created the two packages pack and mypack. The A class of pack
package is public,so can be accessed from outside the package. But msg method of this package
is declared as protected, so it can be accessed from outside the class only through inheritance.

EXAMPLE

//save by A.java

package pack;

public class A{

protected void msg(){System.out.println("Hello");}

//save by B.java

package mypack;

import pack.*;

class B extends A{

public static void main(String args[]){

B obj = new B();


obj.msg();

Output:

Hello

4) Public Access Modifier

The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.

Example:

//save by A.java

package pack;

public class A{

public void msg(){System.out.println("Hello");}

//save by B.java

package mypack;

import pack.*;

class B{

public static void main(String args[]){

A obj = new A();

obj.msg();

Output:
Hello

You might also like