Java OOPS 1
Java OOPS 1
Correct code :
When you have an inner class, it's like a class living inside another class (the outer class). The
inner class is tied to the outer class because it can directly access the outer class's data.
When you create an instance of the outer class (the "house"), you're setting up the whole
structure, including the inner class (the "room"). Without creating the outer class first, the inner
class has no "house" to be inside.
—-----------------------------------------------------------------------------------------------------------------------------
1. Dynamic Memory Allocation: When you create an object, Java needs to reserve
space in the memory for it. The new keyword tells Java to create that space.
○ For example, if you create an object of Car, Java needs to make space for
all the properties (like brand, color, speed) and the methods that belong
to that specific car object.
Initialization: The new keyword also runs the constructor of the class. A
constructor is a special method that initializes the object, setting up its initial
state.
● Without the new keyword, the constructor wouldn't run, and the object wouldn't be
properly set up.
The code works even though you haven't explicitly defined constructors for Main1 and
Car, because Java provides default constructors for classes when you don't define any.
A default constructor is an implicit constructor that Java automatically creates for a class
if you don't explicitly define one. The default constructor:
● Has no parameters.
● Does nothing other than creating the object and allocating memory for it.
—-----------------------------------------------------------------------------------------------------------------------------
A constructor : Unlike regular methods, constructors do not have a return type, not even
void.
Types of Constructors:
● Default Constructor: If you don't define any constructor in a class, Java provides a
default constructor. This constructor takes no arguments and assigns default
values to the object's fields.
● Parameterized Constructor: You can create a constructor with parameters to
initialize the object with specific values at the time of creation.
Car() {
model = "Unknown";
year = 2020;
// Parameterized constructor
this.model = model;
this.year = year;
—-----------------------------------------------------------------------------------------------------------------------------
In Java, the this keyword is a reference to the current object of the class. It is used
within an instance method or constructor to refer to the object on which the method or
constructor is being invoked.
—-----------------------------------------------------------------------------------------------------------------------------
Constructor overloading in Java refers to having more than one constructor in a class
with different parameter lists. Each constructor can perform different initialization tasks
based on the arguments provided when the object is created. Java differentiates between
constructors based on the number and types of parameters.
By providing multiple constructors, you give flexibility in how objects of the class can be
created.
Same Name: All constructors have the same name as the class.
Different Parameters: Each constructor must have a different set of parameters (either in
number, type, or both).
In Java, the constructor and the class name are the same because the constructor is
designed to initialize an object of that specific class. This naming convention serves a
clear purpose and aligns with the concept of object-oriented programming, making it
intuitive for developers to understand that the constructor is related to the class it's a
part of.
No, in Java, you cannot use a different name for a constructor. The constructor must
have the same name as the class. This is a fundamental rule of the language.
—-----------------------------------------------------------------------------------------------------------------------------
Primitive Types: Primitive data types are stored directly in the stack memory, which is
faster and more efficient for basic data types. When you declare a primitive, the memory
is immediately allocated, and the value is stored in that location.
Objects: Objects are more complex and are stored in the heap memory. The new keyword
is used to allocate memory for objects in the heap. This allocation requires more
resources and is slightly slower than stack allocation.
The stack and heap are two different types of memory used in Java (and many other
programming languages) for storing data, but they are used in different ways and have
distinct characteristics.
1. Stack Memory:
Static: Memory for variables is allocated at compile time and released when the method
execution ends.
2. Heap Memory:
Usage Stores local variables, method calls, Stores objects and arrays
and references to objects. created with new.
Access Speed Faster due to smaller size and LIFO Slower because of larger size
structure. and complexity.
Memory Limit Small and fixed for each thread. Larger, but can lead to
OutOfMemoryError if
exceeded.
Sharing Each thread has its own stack. Heap memory is shared across
all threads.
—-----------------------------------------------------------------------------------------------------------------------------
In Java, wrapper classes are object representations of the primitive data types. They
provide a way to treat primitive types as objects, which is useful in situations where you
need objects instead of primitives (e.g., in Java Collections like ArrayList, which only
work with objects, not primitives).
byte → Byte
short → Short
int → Integer
long → Long
float → Float
double → Double
char → Character
boolean → Boolean
—-----------------------------------------------------------------------------------------------------------------------------
A final variable can be assigned only once. Once it is assigned a value, that value
cannot be changed.
When we say that the object's internal state can still be modified for a final reference,
we mean that while the reference to the object itself cannot be changed, the properties or
fields of that object can be altered.
final Car car: The variable car is declared as final, meaning you cannot reassign
car to reference another Car object (e.g., car = new Car("BMW"); would cause an
error).
Modifying Internal State: However, you can change the internal state of the Car object
itself, such as its model field. In the example, we change the model from "Tesla" to
"Ford".
—-----------------------------------------------------------------------------------------------------------------------------