[Go to site: main page, start]

0% found this document useful (0 votes)
13 views14 pages

Java

The document provides an overview of key concepts in Java, including the functionality of switch statements, the significance of the static main method, and the characteristics of classes and objects. It explains access specifiers, constructors, error vs. exception handling, and the principles of abstraction, inheritance, and polymorphism. Additionally, it covers the differences between class and object, the rules for creating constructors, and the concept of overloading and overriding methods.

Uploaded by

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

Java

The document provides an overview of key concepts in Java, including the functionality of switch statements, the significance of the static main method, and the characteristics of classes and objects. It explains access specifiers, constructors, error vs. exception handling, and the principles of abstraction, inheritance, and polymorphism. Additionally, it covers the differences between class and object, the rules for creating constructors, and the concept of overloading and overriding methods.

Uploaded by

amulya.gajarla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA

=> A switch works with the byte , short , char , and int primitive
data types. It also works with enumerated types (discussed in Enum
Types), the String class, and a few special classes that wrap certain
primitive types: Character , Byte , Short , and Integer (discussed in
Numbers and Strings).

[Link]
%20works%20with%20the,discussed%20in%20Numbers%20and%20Strings).

=>why main method is static and public in java

The main() method is declared static so that JVM can call it


without creating an instance of the class containing the main()
method. We must declare the main() function static as no class
object is present when the java runtime starts. JVM can then
load the class into the main memory and invoke the main()
method.

Can we declare constructor as private?

*** a class cannot inherit the constructor of its base class.


Yes, we can declare a constructor as private. If we declare a constructor as
private we are not able to create an object of a class. We can use this private
constructor in the Singleton Design Pattern.

Class: A class is a blueprint or template of an object. It is a user-defined data type.


Inside a class, we define variables, constants, member functions, and other
functionality. It does not consume memory at run time. Note that classes are not
considered as a data structure. It is a logical entity. It is the best example of data
binding.

Object: An object is a real-world entity that has attributes, behavior, and properties. It
is referred to as an instance of the class. It contains member functions, variables
that we have defined in the class. It occupies space in the memory. Different objects
have different states or attributes, and behaviors.

The following figure best illustrates the class and object.


Any entity with states and behaviors is an object. While methods define an item’s
behaviors, states reflect the characteristics or data of an entity. Objects include
students, workers, books, etc. By exchanging messages, these things communicate
with one another. A class is also a template for building an object. A class is required
in order to generate objects. For instance, there needs to be an Employee class in
order to generate an Employee object.

11) What are the differences between class and object?

Class Object

It is a logical entity. It is a real-world entity.

It is conceptual. It is real.

It binds data and methods together into It is just like a variable of a class.
a single unit.

It does not occupy space in the memory. It occupies space in the memory.

It is a data type that represents the It is an instance of the class.


blueprint of an object.

It is declared once. Multiple objects can be declared as


and when required.

It uses the keyword class when declared. It uses the new keyword to create an
object.

A class can exist without any object. Objects cannot exist without a class.
What is the concept of access specifiers when should we use
these?

In OOPs language, access specifiers are reserved keyword that is used to set the
accessibility of the classes, methods and other members of the class. It is also
known as access modifiers. It includes public, private, and protected. There is some
other access specifier that is language-specific. Such as Java has another access
specifier default. These access specifiers play a vital role in achieving one of the
major functions of OOP, i.e. encapsulation. The following table depicts the
accessibility.

15) What are the rules for creating a constructor?

○ It cannot have a return type.

○ It must have the same name as the Class name.

○ It cannot be marked as static.

○ It cannot be marked as abstract.

○ It cannot be overridden.

○ It cannot be final.
What are the differences between error and exception?

Basis of Exception Error


Comparison

Recoverable/ Exception can be An error cannot be


Irrecoverable recovered by using the recovered.
try-catch block.

Type It can be classified into All errors in Java are


two categories i.e. unchecked.
checked and unchecked.

Occurrence It occurs at compile time It occurs at run time.


or run time.

Package It belongs to It belongs to [Link]


[Link] package.
package.

Known or unknown Only checked exceptions Errors will not be known to


are known to the the compiler.
compiler.
Causes It is mainly caused by the It is mostly caused by the
application itself. environment in which the
application is running.

Example Checked Exceptions: [Link],


SQLException, [Link]
IOException

Unchecked Exceptions:
ArrayIndexOutOfBoundEx
ception,
NullPointerException,
ArithmaticException

What are the characteristics of an abstract class?

An abstract class is a class that is declared as abstract. It cannot be instantiated


and is always used as a base class. The characteristics of an abstract class are as
follows:

○ Instantiation (Creation of object) of an abstract class is not allowed. It must


be inherited.(Object is created to subclass and abstract class methods are
accessed).

○ An abstract class can have both abstract and non-abstract methods.

○ An abstract class must have at least one abstract method.

○ You must declare at least one abstract method in the abstract class.
○ It is always public.

○ It is declared using the abstract

The purpose of an abstract class is to provide a common definition of the base class
that multiple derived classes can share.

What is data abstraction and how can we achieve data


abstraction?

It is one of the most important features of OOP. It allows us to show only essential
data or information to the user and hides the implementation details from the user. A
real-world example of abstraction is driving a car. When we drive a car, we do not
need to know how the engine works (implementation) we only know how ECG works.

There are two ways to achieve data abstraction

○ Abstract class

○ Abstract method

Identify which OOPs concept should be used in the following


scenario?

A group of 5 friends, one boy never gives any contribution when the group goes for
the outing. Suddenly a beautiful girl joins the same group. The boy who never
contributes is now spending a lot of money for the group.

Runtime Polymorphism

If a class Demo has a static block and a main() method. A print statement is
presented in both. The question is which one will first execute, static block or the
main() method, and why?
JVM first executes the static block on a priority basis. It means JVM first goes to
static block even before it looks for the main() method in the program. After that
main() method will be executed.

1. class Demo
2. {
3. static //static block
4. {
5. [Link]("Static block");
6. }
7. public static void main(String args[]) //static method
8. {
9. [Link]("Static method");
10. }
11. }

main method invoked 6

Is it possible to overload a constructor?

Yes, the constructors can be overloaded by changing the number of arguments


accepted by the constructor or by changing the data type of the parameters.

There are three types of variables:


Instance Variable: It is an object-level variable. It should be declared inside a class
but must be outside a method, block, and constructor. It is created when an object is
created by using the new keyword. It can be accessed directly by calling the variable
name inside the class.

Static Variable: It is a class-level variable. It is declared with keyword static inside a


class but must be outside of the method, block, and constructor. It stores in static
memory. Its visibility is the same as the instance variable. The default value of a
static variable is the same as the instance variable. It can be accessed by calling the
class_name.variable_name.

Local Variable: It is a method-level variable. It can be declared in method,


constructor, or block. Note that the use of an access modifier is not allowed with
local variables. It is visible only to the method, block, and constructor in which it is
declared. Internally, it is implemented at the stack level. It must be declared and
initialized before use.

Another type of variable is used in object-oriented programming is the reference


variable.

Reference Variable: It is a variable that points to an object of the class. It points to


the location of the object that is stored in the memory.

Give a real-world example of polymorphism?


The general meaning of Polymorphism is one that has different forms. The best real-
world example of polymorphism is a person that plays different roles at different
palaces or situations.

○ At home a person can play the role of father, husband, and son.

○ At the office the same person plays the role of boss or employee.

○ In public transport, he plays the role of passenger.

○ In the hospital, he can play the role of doctor or patient.

○ At the shop, he plays the role of customer.

Hence, the same person possesses different behavior in different situations. It is


called polymorphism.
Overloading

Overloading is a concept in OOP when two or more methods in a class with the
same name but the method signature is different. It is also known as compile-time
polymorphism. For example, in the following code snippet, the method add() is an
overloaded method.

1. public class Sum


2. {
3. int a, b, c;
4. public int add();
5. {
6. c=a+b;
7. return c;
8. }
9. add(int a, int b);
10. {
11. //logic
12. }
13. add(int a, int b, int c);
14. {
15. //logic
16. }
17. add(double a, double b, double c);
18. {
19. //logic
20. }
21. //statements
22. }

Overriding

If a method with the same method signature is presented in both child and parent
class is known as method overriding. The methods must have the same number of
parameters and the same type of parameter. It overrides the value of the parent
class method. It is also known as runtime polymorphism. For example, consider the
following program.

1. class Dog
2. {
3. public void bark()
4. {
5. [Link]("woof ");
6. }
7. }
8. class Hound extends Dog
9. {
10. public void sniff()
11. {
12. [Link]("sniff ");
13. }
14. //overrides the method bark() of the Dog class
15. public void bark()
16. {
17. [Link]("bowl");
18. }
19. }
20. public class OverridingExample
21. {
22. public static void main(String args[])
23. {
24. Dog dog = new Hound();
25. //invokes the bark() method of the Hound class
26. [Link]();
27. }
28. }
29. Static int a=10,final static int a=10,int a=10 final int a=10;

What are the differences between Inheritance and


Polymorphism?

Inheritance Polymorphism

Inheritance is one in which a Polymorphism is one that you can define in


derived class inherits the already different forms.
existing class's features.

It refers to using the structure It refers to changing the behavior of a


and behavior of a superclass in a superclass in the subclass.
subclass.
It is required in order to achieve In order to achieve polymorphism, inherence
polymorphism. is not required.

It is applied to classes. It is applied to functions and methods.

It can be single, hybrid, multiple, There are two types of polymorphism:


hierarchical, multipath, and compile time and run time.
multilevel inheritance.

It supports code reusability and It allows the object to decide which form of
reduces lines of code. the function to be invoked at run-time
(overriding) and compile-time (overloading).

You might also like