OOPS (Object Oriented Programming System):
- It is based on the concept of objects that contain data and methods,
- It works on the principles of:
- Objects
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Objects: Objects can be
defined as an instance of a class.
Syntax: ClassName Reference_Variable = new
Constructor()
Example: (class Testing)
Testing obj = new Testing();
Class: Class is a collection
of variables and methods. A class can also be defined as a
blueprint from which you can create an individual object.
Types of Variables:
- Instance Variables
- Static Variables
- Local Variables
1. Instance Variables:
Declaration: Inside the class
and outside the method (main()).
Object Creation: We must
create an object to access the instance variables.
Example:
- public class InstanceVariables {
- //Instance variable
- int a = 1234;
- public static void main(String[] args) {
- //creating object
- InstanceVariables obj = new InstanceVariables();
- System.out.print(obj.a);
- }
- }
2. Static Variables:
Declaration: Inside the class and
outside the method, It will be declared using the
"static" keyword.
Object Creation: No need to
create the object to access static variables.
Example:
- public class StaticVariables {
- //declaring the static variable
- static int a = 1234;
- public static void main(String[] args) {
- System.out.print(a);
- }
- }
3. Local Variables:
Declaration: Inside the method
(Either main method or user-defined methods).
We cannot access the local variable outside the
method.
Example:
- public class LocalVariables {
- void test() {
- int a = 10;
- System.out.println("User Defined Method : " +a)
- }
- public static void main(String[] args) {
- int b = 20;
- System.out.println("Local Variable : " +b);
- //We cannot access the local variable outside the method
- //Error: System.out.println("test method variable : " +a);
- }
- }
Types of Methods:
Methods: A set of statements that are
grouped together to perform an operation.
- Predefined or Built-in or Standard-Library methods
- User-defined methods
1. Predefined methods:
- The methods that are already defined in the Java class libraries are known as Predefined methods.
- We can directly use these methods just by calling them in the program at any point.
- Some pre-defined methods are length(), equals(), compareTo(), sqrt(), print(), etc.,
2. User-defined methods:
- The method written by the user is known as a User-defined method.
- User-defined methods are of two types:
- Instance Method or Non-static Method
- Static Method
Instance Method:
- We can use both instance and static variables inside the Instance method.
- The instance method can be invoked using an object of the class.
Example:
- public class InstanceMethod {
- //Instance method
- //Method with returns values
- public int addition(int a, int b) {
- int result = a+b;
- return result;
- }
-
- //Method without return values
- public void display(int value) {
- System.out.println("Output: "+value);
- }
-
- public static void main(String[] args) {
- InstanceMethod obj = new InstanceMethod();
- int add = obj.addition(10, 20);
- obj.display(add);
- }
- }
Static Method:
- To create a static method in Java, you prefix the keyword 'static' before the name of the method.
- Inside a static method, we are only permitted to use static variables.
- We can not use instance variables inside the static method.
- The static method can be invoked simply by calling the invoke method.
Example:
- public class StaticMethod {
- //Method with returns values
- public static int addition(int a, int b) {
- int result = a+b;
- return result;
- }
- //Method without return values
- public static void display(int value) {
- System.out.println("Output: "+value);
- }
- public static void main(String[] args) {
- int add = addition(10, 20);
- display(add);
- }
- }
No comments:
Post a Comment