Java For Placement Training Classes
Java For Placement Training Classes
Java For Placement Training Classes
Java is the name of an island in Indonesia where the first coffee(named java
coffee) was produced. And this name was chosen by James Gosling while
having coffee near his office. Note that Java is just a name, not an acronym.
Java Terminology
3. Java Development Kit(JDK): While we were using the term JDK, when we learn
about bytecode and JVM . So, as the name suggests, it is a complete Java
development kit that includes everything including compiler, Java Runtime
Environment (JRE), java debuggers, java docs, etc. For the program to execute in
java, we need to install JDK on our computer in order to create, compile and run the
java program.
4. Java Runtime Environment (JRE): JDK includes JRE. JRE installation on our
computers allows the java program to run, however, we cannot compile it. JRE
includes a browser, JVM, applet supports, and plugins. For running the java
program, a computer needs JRE.
6. ClassPath: The classpath is the file path where the java runtime and Java
compiler look for .class files to load. By default, JDK provides many libraries. If you
want to include external libraries they should be added to the classpath.
structures.
class. A method is a procedure associated with a call and does something with an
object (message passing). Methods have this awesome capability that can
the runtime of your coding program. When a new object is created, it is usually
object.
1. Platform Independent:
Abstraction
also has a private method meow(). It can call it whenever it wants, the other classes
can’t tell the cat when to meow.
What they can do is defined in the public methods sleep(), play() and feed().
Each of them modifies the internal state somehow and may invoke meow(). Thus,
the binding between the private state and public methods is made.
Source:
https://www.freecodecamp.org/news/object-oriented-programming-concepts-2
1bb035f7260/
● Inheritance
So how do we reuse the common logic and extract the unique logic into a separate
class? One way to achieve this is inheritance.
It means that you create a (child) class by deriving from another (parent) class. This
way, we form a hierarchy.
The child class reuses all fields and methods of the parent class (common part) and
can implement its own (unique part).
Source:
https://www.freecodecamp.org/news/object-oriented-programming-concepts-2
1bb035f7260/
So we already know the power of inheritance and happily use it. But there comes
this problem.
Say we have a parent class and a few child classes which inherit from it. Sometimes
we want to use a collection — for example a list — which contains a mix of all these
classes. Or we have a method implemented for the parent class — but we’d like to
use it for the children, too.This can be solved by using polymorphism.
Triangle, Circle, and Rectangle now can be used in the same collection
Source:
https://www.freecodecamp.org/news/object-oriented-programming-concepts-2
1bb035f7260/
3. Simple: Java is one of the simple languages as it does not have complex
features like pointers, operator overloading, multiple inheritances, Explicit memory
allocation.
8. Portable: As we know, java code written on one machine can be run on another
machine. The platform-independent feature of java in which its platform-independent
bytecode can be taken to any platform for execution makes java portable.
3. class: The class contains the data and methods to be used in the program.
Methods define the behavior of the class. Class GFG has only one method Main in
JAVA.
4. static void Main(): static keyword tells us that this method is accessible without
instantiating the class.
5. void: keywords tell that this method will not return anything. The main() method is
the entry point of our application.
6. System.in: This is the standard input stream that is used to read characters
from the keyboard or any other standard input device.
7. System.out: This is the standard output stream that is used to produce the
result of a program on an output device like the computer screen.
8. println(): This method in Java is also used to display text on the console. It prints
the text on the console and the cursor moves to the start of the next line at the
console. The next printing takes place from the next line.
References:
1. https://www.geeksforgeeks.org/introduction-to-java/?ref=lbp
2. https://www.freecodecamp.org/news/object-oriented-programming-concepts-2
1bb035f7260/
3. www.topperskills.com
4. https://medium.com/@rogercodes1/what-is-object-oriented-programming
● OOPs helps users to understand the software easily, although they don’t know
the actual implementation.
● With OOPs, the readability, understandability, and maintainability of the code
increase multifold.
● Even very big software can be easily written and managed easily using OOPs.
3. What are some major Object Oriented Programming languages?
The programming languages that use and follow the Object-Oriented Programming
paradigm or OOPs, are known as Object-Oriented Programming languages. Some of
the major Object-Oriented Programming languages include:
● Java
● C++
● Javascript
● Python
● PHP
And many more.
● Inheritance
● Encapsulation
● Polymorphism
● Data Abstraction
7. What are some advantages of using OOPs?
● OOPs is very helpful in solving very complex level of problems.
● Highly complex programs can be created, handled, and maintained easily
using object-oriented programming.
● OOPs, promote code reuse, thereby reducing redundancy.
● OOPs also helps to hide the unnecessary details with the help of Data
Abstraction.
● OOPs, are based on a bottom-up approach, unlike the Structural programming
paradigm, which uses a top-down approach.
● Polymorphism offers a lot of flexibility in OOPs.
8. Why is OOPs so popular?
OOPs programming paradigm is considered as a better style of programming. Not
only it helps in writing a complex piece of code easily, but it also allows users to
handle and maintain them easily as well. Not only that, the main pillar of OOPs - Data
Abstraction, Encapsulation, Inheritance, and Polymorphism, makes it easy for
programmers to solve complex scenarios. As a result of these, OOPs is so popular.
Therefore the class is basically a template or blueprint for objects. Also one can
create as many objects as they want based on a class.
For example, first, a car’s template is created. Then multiple units of car are created
based on that template.
In OOPs, Polymorphism refers to the process by which some code, data, method, or
object behaves differently under different circumstances or contexts. Compile-time
polymorphism and Run time polymorphism are the two types of polymorphisms in
OOPs languages.
13. What is Compile time Polymorphism and how is it different from Runtime
Polymorphism?
Compile Time Polymorphism: Compile time polymorphism, also known as Static
Polymorphism, refers to the type of Polymorphism that happens at compile time.
What it means is that the compiler decides what shape or value has to be taken by
the entity in the picture.
Example:
In the above example, there are four versions of add methods. The first method takes
two parameters while the second one takes three. For the third and fourth methods,
there is a change of order of parameters. The compiler looks at the method
signature and decides which method to invoke for a particular method call at
compile time.
Example:
class AnyVehicle{
public void move(){
System.out.println(“Any vehicle should move!!”);
}
}
class Bike extends AnyVehicle{
public void move(){
System.out.println(“Bike can move too!!”);
}
}
class Test{
public static void main(String[] args){
AnyVehicle vehicle = new Bike();
// In the above statement, as you can see, the object
vehicle is of type AnyVehicle
// But the output of the below statement will be “Bike can
move too!!”,
// because the actual implementation of object ‘vehicle’ is
decided during runtime vehicle.move();
vehicle = new AnyVehicle();
// Now, the output of the below statement will be “Any
vehicle should move!!”,
vehicle.move();
}
}
As the method to call is determined at runtime, as shown in the above code, this is
called runtime polymorphism.
For example, consider a car. You only need to know how to run a car, and not how the
wires are connected inside it. This is obtained using Abstraction.
Now here, the method called after “new” keyword - MyClass(), is the constructor of
this class. This will help to instantiate the member data and methods and assign
them to the object myClassObject.
Default constructor: The default constructor is the constructor which doesn’t take
any argument. It has no parameters.
class ABC
{
int x;
ABC()
{
x = 0;
}
}
Parameterized constructor: The constructors that take some arguments are known
as parameterized constructors.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
}
class ABC
{
int x;
ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}
23. Are class and structure the same? If not, what's the difference between a class
and a structure?
No, class and structure are not the same. Though they appear to be similar, they have
differences that make them apart. For example, the structure is saved in the stack
memory, whereas the class is saved in the heap memory. Also, Data Abstraction
cannot be achieved with the help of structure, but with class, Abstraction is majorly
used.
Let me explain to you with a common example. Let's take three different vehicles - a
car, truck, or bus. These three are entirely different from one another with their own
specific characteristics and behavior. But. in all three, you will find some common
elements, like steering wheel, accelerator, clutch, brakes, etc. Though these elements
are used in different vehicles, still they have their own features which are common
among all vehicles. This is achieved with inheritance. The car, the truck, and the bus
have all inherited the features like steering wheel, accelerator, clutch, brakes, etc, and
used them as their own. Due to this, they did not have to create these components
from scratch, thereby facilitating code reuse.
25. Are there any limitations of Inheritance?
Yes, with more powers comes more complications. Inheritance is a very powerful
feature in OOPs, but it has some limitations too. Inheritance needs more time to
process, as it needs to navigate through multiple classes for its implementation.
Also, the classes involved in Inheritance - the base class and the child class, are very
tightly coupled together. So if one needs to make some changes, they might need to
do nested changes in both classes. Inheritance might be complex for
implementation, as well. So if not correctly implemented, this might lead to
unexpected errors or incorrect outputs.
● Single inheritance
● Multiple inheritances
● Multi-level inheritance
● Hierarchical inheritance
● Hybrid inheritance
27. What is a subclass?
The subclass is a part of Inheritance. The subclass is an entity, which inherits from
another class. It is also known as the child class.
Image
So exception handling is the mechanism for identifying the undesirable states that
the program can reach and specifying the desirable outcomes of such states.
Try-catch is the most common method used for handling exceptions in the program.
39. What is meant by Garbage Collection in OOPs world?
Object-oriented programming revolves around entities like objects. Each object
consumes memory and there can be multiple objects of a class. So if these objects
and their memories are not handled properly, then it might lead to certain
memory-related errors and the system might fail.
Garbage collection refers to this mechanism of handling the memory in the program.
Through garbage collection, the unwanted memory is freed up by removing the
objects that are no longer needed.
40. Can we run a Java application without implementing the OOPs concept?
No. Java applications are based on Object-oriented programming models or OOPs
concept, and hence they cannot be implemented without it.
However, on the other hand, C++ can be implemented without OOPs, as it also
supports the C-like structural programming model.
class BaseClass2 {
public:
BaseClass2()
{ cout << "BaseClass2 constructor called" << endl; }
};
int main()
{
DerivedClass derived_class;
return 0;
}
Output:
Reason:
The above program demonstrates Multiple inheritances. So when the Derived class’s
constructor is called, it automatically calls the Base class's constructors from left to
right order of inheritance.
static
{
System.out.println(“a”);
i = 100;
}
}
System.out.println(Scaler.i);
}
}
Output:
b
c
a
100
Reason:
Firstly the static block inside the main-method calling class will be implemented.
Hence ‘b’ will be printed first. Then the main method is called, and now the sequence
is kept as expected.
class ClassA {
public:
ClassA(int ii = 0) : i(ii) {}
void show() { cout << "i = " << i << endl;}
private:
int i;
};
class ClassB {
public:
ClassB(int xx) : x(xx) {}
operator ClassA() const { return ClassA(x); }
private:
int x;
};
void g(ClassA a)
{ a.show(); }
int main() {
ClassB b(10);
g(b);
g(20);
getchar();
return 0;
}
Output:
i = 10
i = 20
Reason:
ClassA contains a conversion constructor. Due to this, the objects of ClassA can
have integer values. So the statement g(20) works. Also, ClassB has a conversion
operator overloaded. So the statement g(b) also works.
Output:
Main1
Reason:
Here the main() method is overloaded. But JVM only understands the main method
which has a String[] argument in its definition. Hence Main1 is printed and the
overloaded main method is ignored.
class BaseClass{
int arr[10];
};
int main(void)
{
cout<<sizeof(DerivedClass);
return 0;
}
Output:
Reason:
class B : public A {
public:
void print()
{ cout <<" Inside B"; }
};
class C: public B {
};
int main(void)
{
C c;
c.print();
return 0;
}
Output:
Inside B
Reason: