What is OOP?
OOP stands for Object-Oriented Programming, OOP is faster and easier to execute, provides a clear structure for the programs. based on the concept of "objects." These objects are instances of classes, which define their structure (data) and behavior (methods/functions). OOP helps organize code in a more modular, reusable, and maintainable way.
Java is an object-oriented programming language.
What are Classes and Objects?
- Class: A blueprint or template for creating objects. It defines a data structure and methods to manipulate that data.
- Object: An instance of a class. It's created based on the blueprint provided by the class, and each object can have different data values.
Syntax to define a class
class ClassName { // variables // methods }
Example
class Mobile { // instance variable private boolean isOn; // method public void turnOn() { isOn = true; } // method public void turnOff() { isOn = false; } }
Here, we have created a class named Mobile.
The class contains variable named isOn and two methods turnOn() and turnOff(). These variables and methods defined within a class are called members of the class.
In the above example, we have used keywords private and public. These are known as access modifiers.
What are Access Modifiers?
In Java, access modifiers are keywords used to define the visibility and accessibility of classes, methods, constructors, and variables. They control how the members (fields, methods, etc.) of a class can be accessed from other classes. There are four main types of access modifiers in Java:
classes, interfaces, variables, methods, constructors, data members.
Modifier | Description |
---|---|
public | The code is accessible for all classes |
private | The code is only accessible within the declared class |
default | The code is only accessible in the same package. This is used when we don't specify a modifier. |
protected | The code is accessible in the same package and subclasses. |
Public Access Modifier
When methods, variables and classes are declared public, then we can access them from anywhere. The public access modifier has no scope restriction.
Example
import java.io.*; import java.util.Scanner; // public class public class Cuboid { // public variable public double lt; public double wt; public double ht; // public method public void volume( ) { System.out.println("Volume of cuboid="+ (lt*wt*ht)); } } public class myClassexamp { public static void main(String args[]) { Scanner input=new Scanner(System.in); // accessing the public class Cuboid cobj=new Cuboid(); System.out.println("Enter length,width,height"); // accessing the public variable cobj.lt=input.nextInt(); cobj.wt=input.nextInt(); cobj.ht=input.nextInt(); // accessing the public method cobj.volume(); } }
Output
Enter length, width and height
8
7
5
Volume of cuboid=280.0
Here,
- The public class Cuboid is accessed from the myClassexamp class.
- The public variables lt,wt, ht are accessed from the Cuboid class.
- The public method volume() is accessed from the myClassexamp class.
Private Access Modifier
When variables and methods are declared private, they cannot be accessed outside of the class.
Example
class ABC{ private double num = 100; private int square(int a){ return a*a; } } public class Example{ public static void main(String args[]){ ABC obj = new ABC(); System.out.println(obj.num); System.out.println(obj.square(10)); } }
Above example displays error because we are trying to access the private variable and the private method of the class from the outside class.
Default Access Modifier
No access modifier is specified for a particular class, method or a data member is called default access modifier. . Examplepackage p1;
Protected Access Modifier
//Class Msg is having Default access modifier
class Msg{
void display()
{
System.out.println("Hello World!");
}
}
The methods or data members declared as protected are accessible within the same package or subclasses in a different package.
Example
spackage p1; //Class A public class A { protected void display() { System.out.println(" RK Tutorials"); } } package p2; import p1.*; //importing all classes in package p1 //Class B is subclass of A class B extends A { public static void main(String args[]) { B obj = new B(); obj.display(); } }
RK Tutorials
Creating Class in Java
In java, a class can be defined through the use of class keyword.
Example
import java.io.*; import java.util.Scanner; class Cuboid { double lt; double wt; double ht; void initialize(double l,double w,double h) { lt=l; wt=w; ht=h; } double volume() { return(lt*wt*ht); } } class ClassDemo1 { public static void main(String args[]) { Scanner input=new Scanner(System.in); Cuboid cobj=new Cuboid(); System.out.println("Enter lt,wt,ht"); cobj.l=input.nextDouble(); cobj.w=input.nextDouble(); cobj.h=input.nextDouble(); cobj.initialize(l,w,h); double vol=cobj.volume(); System.out.println("The volume of the cuboid is: "+vol); } }
0 Comments