Java Notes-oops1
Topic: OOP's Part #1
Introduction to the Classes🔻
Theory:
In Java, a class is a blueprint for creating objects, representing real-world entities such as people, places, and things. A class defines a set of attributes (instance variables) and behaviours (methods) that are common to all instances of that class. The concept of a class is a fundamental aspect of object-oriented programming (OOP), which is a programming paradigm that organizes code into objects that represent real-world entities.
⭐A Java class typically consists of the following components:
Class declaration: This includes the class name, class modifiers (public, private, etc.), and class body.
Instance variables: These are variables declared within the class body, and they represent the state of an object.
Methods: These are the behaviour of an object and perform various operations on its instance variables.
Main method: This is the entry point of a Java application, and it's where the program execution begins. ( it will be in the main class which has the same name as the java file)
⭐Here are some of the rules for writing a Java class:
Class names should begin with a capital letter and follow PascalCase conventions. (Ex. class MainClass, class CircleClass, class PaymentClass )
The class body should be enclosed within curly braces ({}).
Methods should be declared within the class body and should specify a return type, except for the void type if the method does not return a value.
The class must be saved with the .java extension, and the file name must match the class name. (Main class)
⭐Types of variables in Java:
- Local variable:
In Java, variables declared within a method or a block of code are called local variables.
public class Main {
public void myMethod() {
int x = 10; // local variable
// code that uses x
}
}
Instance Variable:
Non-static, unique to each instance of a class.
Declare inside the class and outside of the method.
class ExampleLet {
int x; // instance variable
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
}
Static variable:
Static variables, on the other hand, are shared across all instances of a class. There is only one copy of a static variable, which is stored in the class, rather than in each instance. The value of a```` static variable is shared by all instances of a class and is the same for all instances.
class Example {
static int x; // static variable
public static void setX(int x) {
Example.x = x;
}
public static int getX() {
return x;
}
}
Creation of class and calling methods🔻
(Class as a Module creation mechanism)
In this section, we learn to create a class step by step and call methods in the Main class from the created class: (here is the learning step, not created direct class, step-by-step explanation)
Step1️⃣: Create a method to find the area of a circle
(Procedural Style of Programming)
Create a java file with the name Main.java
Create a main class with the name Main (public class Main) this main class has a main method (public static void main(String args[]){})
Write a method to find the area of the circle
public class Main {
public static float rad= 10f;
public static void main(String[] args) {
System.out.println(areaOfCircle(rad));
//function calling
}
public static float areaOfCircle(float rad){
return 3.14f * rad * rad ;
}
//method defination
}
Now we have to create a new Class and put this areOfcircle method in that class.
Step2️⃣: Create A class:
(Object-Oriented Style of Programming)
Create a new class with the name Circle.
e.g. class circle{}
One java file can have only one public class
All others in that file are not defined as public
Step3️⃣: Putting Method In class and Calling:
Put areaOfCircle method in Created circle class :
To call this areaOfCircle method which is in the Circle class for call in the main method of main class syntax is:
public class Main{
public static void main(String args[]){
Syntax: ClassName.methodName(argument);
}
}
public class Main {
public static float rad= 10f;
public static void main(String[] args) {
System.out.println(Circle.areaOfCircle(rad));
Circle.main(args);
}
} //main class
class Circle{
public static float areaOfCircle(float rad){
return 3.14f * rad * rad ;
}
public static void main(String[] args) {
System.out.println("j");
}
}//created circle class
##OutPut:
314.0
j
Summary:
There should be one Public class which have the name same as the .java file
the execution of code start from the main method so this main method should be in that public class which has the same as the .java file
we can call any method from another class with the syntax: ClassName.methodName(argument);
Multiple Classes🔻
why Do we need multiple classes, the answer is When we come across the dilemma of whether we need to create a method of the same name, attributes, and parameters with the same data type. Then Java doesn't allow the creation of such methods The solution for this like method overriding also failed because if we have the same name and same parameters data type.
So the solution is the Create a different class and define methods in it.
For example:
❓Calculate the Area of the circle and Square:
class Circle{
public static float area(float radius){
return 3.141f radius radius;
}
class Square{
public static float area(float length){
return length * length;
}
The above two methods have the same:
Name
parameter data types
So method overriding not gonna help.
❓Calculate the Area of the circle and Square Using multiple classes**:**
public class Main{
public static float rad = 10.0f;
public static float len = 10.0f;
public static void main(String[] a){
System.out.println("area of circle: " + Circle.area(radius));
System.out.println("area of square: " + Square.area(length));
}
}
class Square{
public static float area(float len){
return len * len;
}
}
class Circle{
public static float area(float rad){
return 3.141f * rad * rad;
}
}
##OUTPUT:
Are of Square: 100.0
Are of Circle: 314.0
