FinalOne
https://www.geeksforgeeks.org/java/classes-objects-java/
1)Classes and Objects:
- In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs)
- A class itself does not occupy memory for its attributes and methods until an object is instantiated.
- used to represent real-world concepts and entities.
- A class is a template to create objects having similar properties a
- class is a blueprint for objects.
- An object is an instance of a class.
Example:
For example, the animal type Dog is a class, while a particular dog named Tommy is an object of the Dog class.
2)Methods:
- A method is a block of code designed to perform a specific task.
- They will execute only when we called.
- All methods in Java must belong to a class.
- Methods are similar to functions and expose the behavior of objects.
Types:
1.Predefined Methods (Built-in Methods)
2.User-defined Methods.
Syntax for the Methods:
1.Predefined Methods:
- Predefined methods are the method that is already defined in the Java class libraries.
- It is also known as the standard library method or built-in method.
For example, random() method which is present in the Math class and we can call it using the ClassName.methodName() as shown in the below example.
Example:
Math.random() // returns random value
Math.PI // return pi value
2.User defined Method:
- The method written by the user or programmer is known as a user-defined method.
- These methods are modified according to the requirement.
Example:
sayHello // user define method created above in the article
Greet()
setName()
Ref: https://www.geeksforgeeks.org/java/methods-in-java/
3)Different Ways to Create Java Method:
1.Instance Method.
2.Static Method.
1.Instance Method:
- Access the instance data using the object name.
- Declared inside a class. Example:
2.Static Method:
- Access the static data using class name.
- Declared inside class with static keyword. Example:
4)Typecasting:
- Typecasting is the process of converting one data type to another data type.
Types of Type Casting:
1.Widening Type Casting
2.Narrow Type Casting
1.Widening Type Casting:(Implicit)
- A lower data type is transformed into a higher one by a process known as widening type casting.
- Since there is no chance of data loss.
- Widening type casting is also sometimes called upcasting.
-
It occurs.....
1.The target type must be larger than the source type.
2.Both data types must be compatible with each other.
2.Narrow Type Casting:(Explicit)
- The process of downsizing a bigger data type into a smaller one is known as narrowing type casting.
- It doesn't just happen by itself. If we don't explicitly do that, a compile-time error will occur.
- Narrowing type casting is unsafe because data loss might happen due to the lower data types.
5)Parameters:
- Parameters are variables defined in the method declaration after the method name inside parenthesis.
- You can pass values(Argument) to the method parameters, at the method call.
- Parameters are local variables which are assigned value of the arguments when the function is called
Top comments (0)