Java OOP1
(Class, Object, Constructor, Overloading, Java Access Modifiers, Recursion, Instanceof Operator)
Java Class and Objects
An object is any entity that has a state and behavior
A class is a blueprint for the object. Before we create an object, we first need to define the class.
Create a class in Java
fields are used to store data
methods are used to perform some operation
To create an object for bicycle we need to make class first (blueprint)
Fields and methods of a class are also called members of the class.
Access Members of a Class
Create objects inside the same class
Declaring a Java Method
Java Method for Code Re-usability
Method overloading?
Suppose, you have to perform the addition of given numbers but there can be any number of arguments (letβs say either 2 or 3 arguments for simplicity).
In order to accomplish the task, you can create two methods
sum2num(int, int)
andsum3num(int, int, int)
for two and three parameters respectively. However, other programmers, as well as you in the future may get confused as the behavior of both methods are the same but they differ by name.The better way to accomplish this task is by overloading methods. And, depending upon the argument passed, one of the overloaded methods is called. This helps to increase the readability of the program
Overloading by changing the number of parameters
Method Overloading by changing the data type of parameters
Java Constructors
A constructor in Java is similar to a method that is invoked when an object of the class is created.
In Java, a constructor is a special method with the same name as the class, invoked when an object is created. It lacks a return type and is used for initializing object attributes. Constructors can be categorized into three types:
No-Arg Constructor: Takes no parameters. If declared private, it can only be accessed within the class.
Parameterized Constructor: Accepts one or more parameters, allowing for the initialization of object attributes based on provided values.
Default Constructor: Created automatically by the compiler if no constructors are defined. Initializes instance variables with default values.
Constructors can be overloaded, allowing for multiple constructors with different parameter sets. Overloaded constructors are called based on the arguments provided during object creation.
Java Access Modifiers
Access modifiers in Java control the visibility or accessibility of classes, interfaces, variables, methods, constructors, data members, and setter methods. There are four types of access modifiers in Java:
Default (Package-Private) Access Modifier:
Declarations are visible only within the same package.
No explicit keyword is used; it's the default if no modifier is specified.
Example:
Private Access Modifier:
Declarations are visible only within the class.
Example:
Protected Access Modifier:
Declarations are visible within the same package or subclasses.
Example:
Public Access Modifier:
Declarations are visible everywhere.
Example:
These access modifiers help in encapsulation, allowing control over which parts of a program can access the members of a class, preventing misuse of data.
Access Modifiers Summarized in one figure
Java Recursion
Definition:
A method calling itself is termed a recursive method, and the overall process is known as recursion.
How Recursion works?
Example:
How it Works:
A function calls itself (recursive call).
Termination condition is crucial to avoid infinite recursion.
Advantages:
Simplicity: Recursive solutions are often simpler and easier to understand.
Quick Implementation: Less time-consuming to write, debug, and maintain.
Disadvantages:
Memory Usage: Recursion uses more memory as each call allocates new storage on the stack.
Speed: Generally slower compared to iterative solutions.
Recommendation:
Choose recursion for simplicity and ease of understanding.
Consider alternatives for performance-critical applications
Java instanceof
Operator:
instanceof
Operator:Definition:
Checks whether an object is an instance of a particular class or interface.
Syntax:
Example:
Output:
Inheritance Example:
Interface Example:
Note:
Used to check if an object is an instance of a specific class or interface.
Can be used in inheritance scenarios to check superclass or interface instances.
Returns
true
if the object is an instance; otherwise, returnsfalse
.All classes in Java are inherited from the
Object
class.
Last updated
Was this helpful?