0% found this document useful (0 votes)
8 views26 pages

Oops Text

Uploaded by

sathya94917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views26 pages

Oops Text

Uploaded by

sathya94917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 26

package vijay;

import java.util.*;

// METHOD
// A method is a block of code which only runs when it is called.
// You can pass data, known as parameters, into a method.
// Methods are used to perform certain actions, and they are also known as
functions.
// Why use methods? To reuse code: define the code once, and use it many times.

//public class oops {


// public static void hello(String a, String b) {
// System.out.println("Hi My Name is " + a + " i'am a " + b);
// }
//
// public static void main(String[] args) {
// hello("Vignesh" , "Java Developer");
// hello("Vikram", "Web Developer");
// hello("Tharun", "Manual Tester");
// }
//}

//public class oops {


//
// static void checkAge(int age) {
//
// if (age < 18) {
// System.out.println("Access denied - You are not old enough!");
// } else {
// System.out.println("Access granted - You are old enough!");
// }
// }
// public static void main(String[] args) {
// checkAge(20);
// checkAge(17);
// checkAge(60);
// }
//}

//public class oops {


// static int myMethod(int x, int y) {
// return x + y;
// }
// static int hello(int a, int b) {
// return a * b;
// }
//
// public static void main(String[] args) {
// int z = myMethod(5, 3);
// System.out.println(z);
// System.out.println(hello(20,2));
// }
//}

// METHOD OVERLOADING
// Multiple methods can have the same name with different parameters
//public class oops{
// static int plusMethod(int x, int y) {
// return x + y;
// }
//
// static String plusMethod(String x, String y) {
// return (x + y);
// }
//
// public static void main(String[] args) {
// int myNum1 = plusMethod(8, 5);
// String myNum2 = plusMethod("Hello ", "World");
// System.out.println("int: " + myNum1);
// System.out.println("double: " + myNum2);
// System.out.println(12 +67 +"34" +45+ 78);
// }
//}

// JAVA RECURSION
// Recursion is the technique of making a function call itself

//public class oops {


// public static void main(String[] args) {
// int result = sum(10);
// System.out.println(result);
// }
// public static int sum(int k) {
// if (k > 0) {
// return k + sum(k - 1);
// } else {
// return 0;
// }
// }
// }

//public class oops {


// public static int sum(int start, int end) {
// if (end > start) {
// return end + sum(start, end - 1);
// } else {
// return end;
// }
// }
// public static void main(String[] args) {
// int result = sum(10, 20);
// System.out.println(result);
// }
//
// }

//sum(10, 20)
//20 + sum(10, 19)
//20 + ( 19 + sum(10,18) )
//20 + ( 19 + ( 18 + sum(10, 17) ) )
//20 + ( 19 + ( 18 + ( 17 + sum(10, 16) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + sum(10, 15) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + sum(10, 14) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + sum(10, 13) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + sum(10, 12) ) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + ( 12 + sum(10,
11) ) ) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + ( 12 + ( 11 + sum(10, 10) )
) ) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + ( 12 + ( 11 + ( Conditon
false return 10 ) ) ) ) ) ) ) ) ) ) )

// CLASS AND OBJECT


16-10-2024

//Everything in Java is associated with classes and objects, along with its
attributes and methods.
//For example: in real life, a car is an object. The car has attributes, such as
weight and color,
//and methods, such as drive and brake.
//A Class is like an object constructor, or a "blueprint" for creating objects.

//class vijay { // Create a Class


// void hello(String name,int age) { // Create a Method
// System.out.println("Hello " + name + " Your are " + age + " years old. 😀
😃😀😃");
// }
//}
//public class oops {
// public static void main(String args[]) {
// vijay ob1 =new vijay(); // Create a Object
// Scanner in = new Scanner(System.in);
// System.out.println("Name : ");
// String y = in.next();
// System.out.println("Age : ");
// int x = in.nextInt();
// ob1.hello(y, x); // Access a Method
// }
//}

// Creating a class with return type

//class vijay { // Create a Class


// String hello(String name,int age) { // Create a Method
// return ("Hello " + name + " Your are " + age + " years old. 😀😃😀😃");
// }
//}
//public class oops {
// public static void main(String args[]) {
// vijay ob1 =new vijay(); // Create a Object
// Scanner in = new Scanner(System.in);
// String y = in.next();
// int x = in.nextInt();
// String hi = ob1.hello(y, x); // Access a Method
// System.out.println(hi);
// }
//}

//class A{
//// private int a; // Doesn't Work
// int a;
//}
//public class oops{
// public static void main(String agrs[]) {
// A ob = new A();
// ob.a = 10;
// System.out.println(ob.a);
// }
//}

//public class oops{


// int a;
// public static void main(String args[]) {
// oops ob = new oops();
// ob.a = 20;
// System.out.println(ob.a);
// }
//}

// JAVA CLASS METHODS

//class A{
// void get() {
// System.out.println("Beant Technology");
// }
// void put() {
// System.out.println("Welcome to Besant Terchnology");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// ob.get();
// ob.put();
// }
//}

// Static vs. Public

//public class oops {


// // Static method
// static void myStaticMethod() {
// System.out.println("Static methods can be called without creating
objects");
// }
//
// // Public method
// public void myPublicMethod() {
// System.out.println("Public methods must be called by creating objects");
// }
//
// // Main method
// public static void main(String[] args) {
// myStaticMethod(); // Call the static method
//// myPublicMethod(); // This would compile an error
//
// oops myObj = new oops(); // Create an object of Main
// myObj.myPublicMethod(); // Call the public method on the object
// }
//}

//Create a Main class

// public class oops {


//
// // Create a fullThrottle() method
// public void fullThrottle() {
// System.out.println("The car is going as fast as it can!");
// }
//
// // Create a speed() method and add a parameter
// public void speed(int maxSpeed) {
// System.out.println("Max speed is: " + maxSpeed);
// }
//
// // Inside main, call the methods on the myCar object
// public static void main(String[] args) {
// oops myCar = new oops(); // Create a myCar object
// myCar.fullThrottle(); // Call the fullThrottle() method
// myCar.speed(200); // Call the speed() method
// }
// }

//
17-10-2024

// CONSTRUCTOR
// Constructor is a special method this used to initialize objects.
// The constructor is called when an object of a class is created.
// Class name and Method name should be same in constructor and it cannot have a
return type

// Constructor types:
// No-Arg Constructor - a constructor that does not accept any arguments
// Parameterized constructor - a constructor that accepts arguments
// Default Constructor - a constructor that is automatically created by the Java
compiler

// No-Arg Constructor

//class A {
// A(){
// System.out.println("Constructor Called");
// }
//}
//class B{
// B(){
// System.out.println("Hello");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A myobj = new A();
// B myobj1 = new B();
// }
//}

// Parameterized constructor

//class A {
// A(String a){
// System.out.println("Constructor Called " + a);
// }
//}
//class B{
// B(int a, int b){
// if(a > b) {
// System.out.println("A is Greater than B");
// }
// else {
// System.out.println("A is Less than B");
// }
// }
//}
//public class oops{
// public static void main(String args[]) {
// A myobj = new A("Successfully");
// B myobj1 = new B(20, 30);
// }
//}

//class A {
// A(String a){
// System.out.println("Constructor Called " + a);
// }
//}
//class B{
// B(int a, int b){
// if(a > b) {
// System.out.println("A is Greater than B");
// }
// else {
// System.out.println("A is Less than B");
// }
// }
//}
//public class oops{
// public static void main(String args[]) {
// Scanner in = new Scanner(System.in);
// System.out.println("Sucessfully / Not");
// String a = in.next();
// A myobj = new A(a);
// System.out.println("Num1 :");
// int b = in.nextInt();
// System.out.println("Num2 :");
// int c = in.nextInt();
// B myobj1 = new B(b,c);
// }
//}

// Default Constructor

//public class oops {


//
// int a;
// boolean b;
//
// public static void main(String[] args) {
//
// // calls default constructor
// oops obj = new oops();
//
// System.out.println("Default Value:");
// System.out.println("a = " + obj.a);
// System.out.println("b = " + obj.b);
// }
//}

// Constructors Overloading in Java

//class oops {
//
// String language;
//
// // constructor with no parameter
// oops() {
// this.language = "Java";
// }
//
// // constructor with a single parameter
// oops(String language) {
// this.language = language;
// }
//
// public void getName() {
// System.out.println("Programming Language: " + this.language);
// }
//
// public static void main(String[] args) {
//
// // call constructor with no parameter
// oops obj1 = new oops();
//
// // call constructor with a single parameter
// oops obj2 = new oops("Python");
//
// obj1.getName();
// obj2.getName();
// }
//}

//
21-10-2024
// INHERITANCE
// OOP that allows us to create a new class from an existing class.
// The new class that is created is known as subclass (child or derived class)
// and the existing class from where the child class is derived is known as
superclass (parent or base class).
// The extends keyword is used to perform inheritance in Java.
//class A{ // Super (or) Parent (or) Base Class
// void put() {
// System.out.println("Hello");
// }
//}
//
//class B extends A{ // Sub (or) Child (or) Derived Class
// void get() {
// System.out.println("Hii..");
// }
//}
//public class oops{
// public static void main(String args[]) {
// B obj = new B();
// obj.put();
// obj.get();
// }
//}

////Base class
//class Person {
// String name = "John";
// int age =17;
// String city = "Delhi";
// public void show()
// {
// System.out.println("Student inheriting properties from Person:\n");
// }
//}
////child class
//class Student extends Person {
// // defining additional properties to child class
// int marks = 78;
// String tutorial = "Besant Technology, Velachery";
//}
//
//public class oops{
// public static void main(String args[]) {
// Student obj = new Student();
// obj.show();
//
// System.out.println("Name of the student is: " + obj.name);
// System.out.println("Age of the student is: " + obj.age);
// System.out.println("Student lives in: " + obj.city);
// System.out.println("Student learns from: " + obj.tutorial);
// System.out.println("Marks obtained by the student is: " + obj.marks);
// }
//}

//
// 1.Single Inheritance
// In single inheritance, there is a single child class that inherits properties
from one parent class.
// Super Class : Which class gives the properties to other class
// Sub Class : Which class get the properties from other class

////Base class
//class Person {
// String name="John";
// public void show(){
// System.out.println("Student inheriting properties from Person");
// }
//}
////child class
//class Student extends Person {
// // defining additional properties to child class
// String course = "Java Course";
// public void show1() {
// System.out.println("I am a Student who belongs to Person class");
// }
//}
//public class oops{
// public static void main(String args[]){
// Student obj = new Student();
// obj.show();
// obj.show1();
// System.out.println("Name of student: " +obj.name);
// System.out.println("Course opted by the student: " +obj.course);
// }
//}

//class A{ // Super (or) Parent (or) Base Class


// void put() {
// System.out.println("Hello");
// }
// private void hello() {
// System.out.println("Doesn't Work");
// }
//}
//
//class B extends A{ // Sub (or) Child (or) Derived Class
// void get() {
// System.out.println("Hii..");
// }
//}
//public class oops{
// public static void main(String args[]) {
// B obj = new B();
// obj.put();
// obj.get();
//// obj.hello();
// }
//}

//class A{ // Super (or) Parent (or) Base Class


// A() {
// System.out.println("Hello");
// }
// void hello() {
// System.out.println("Hi");
// }
//}
//
//class B extends A{ // Sub (or) Child (or) Derived Class
// B() {
// System.out.println("Hii..");
// }
//}
//public class oops{
// public static void main(String args[]) {
// B obj = new B();
// obj.hello();
// }
//}

//class A extends B{ // Super (or) Parent (or) Base Class


// A() {
// System.out.println("Hello");
// }
// void hello() {
// System.out.println("Hi");
// }
//}
//
//class B{ // Sub (or) Child (or) Derived Class
// B() {
// System.out.println("Hii..");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A obj = new A();
// obj.hello();
// }
//}

// 1.Multilevel Inheritance in Java


// In this type of inheritance, the child or derived class inherits the features of
the superclass and simultaneously
// this child class acts as a superclass for another derived class.

////Base class
//class Person{
// public void show(){
// System.out.println("Student inheriting properties from Person");
// }
//}
//class Student extends Person{
// public void show1(){
// System.out.println("I am a Student who belongs to Person class");
// }
//}
////child class
//class EngineeringStudent extends Student
//{
// // defining additional properties to the child class
// public void show2(){
// System.out.println("Engineering Student inheriting properties from
Student");
// }
//}
//public class oops{
// public static void main(String args[]){
// EngineeringStudent obj = new EngineeringStudent();
// obj.show();
// obj.show1();
// obj.show2();
// }
//}

//Method OverRiding
// SUPER CLASS AND SUB CLASS HAVE SAME METHOD NAME AND PARAMETER

//class Base{
// void Str() {
// System.out.println("Hello");
// }
//}
//class Derived extends Base{
// void str() {
// System.out.println("Hii.");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Derived ob = new Derived();
// ob.str();
// }
//}

//class Base{
// void Str() {
// System.out.println("Hello");
// }
//}
//class Derived extends Base{
// void str() {
// super.Str();
// System.out.println("Hii.");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Derived ob = new Derived();
// ob.str();
// }
//}

// 3.Hierarchical Inheritance
// In Hierarchical Inheritance, one class acts as a superclass (base class) for
more than one subclass.
// More than one subclass can inherit the features of a base class.

////Base class
//class Person {
// public void show() {
// System.out.println("I am a Person");
// }
//}
//
////child class1
//class Student extends Person {
// public void show1() {
// System.out.println("I am a Student who is Person ");
// }
//}
//
////child class2
//class Teacher extends Person {
//// defining additional properties to the child class
// public void show2() {
// System.out.println("I am a Teacher who is a Person");
// }
//}
////child class3
//class Doctor extends Person {
//// defining additional properties to the child class
// public void show3() {
// System.out.println("I am a Doctor who is a Person");
// }
//}
//public class oops {
// public static void main(String args[]) {
// Teacher teacher = new Teacher();
// Student student = new Student();
// Doctor doctor = new Doctor();
// student.show();
// student.show1();
// teacher.show();
// teacher.show2();
// doctor.show();
// doctor.show3();
// }
//}
////
22-10-2024

//4. Multiple Inheritance in Java


//In Multiple Inheritance, one child or subclass class can have more than one base
class or superclass and
//inherit features from every parent class which it inherits.

//We have already discussed that Java does not support multiple inheritances with
classes.
//We can achieve multiple inheritances only with the help of Interfaces.

// Interface have five rules

// 1. Interface name as declare


// interface A

// 2. we declare interface method but we do not define the method


// interface A{
// void get();
// }

// 3.Interface method should be in public


//interface A{
// public void get();
//}

// 4. We use implements instead of extends


// class B implements A{
// }

// 5. Which class implements interface that can define the interface method
//class B implements A{
// public void get(){
// System.out.println("Hello");
// }
//}

// interface FirstInterface {
// public void myMethod(); // interface method
// }
//
// interface SecondInterface {
// public void myOtherMethod(); // interface method
// }
//
// class DemoClass implements FirstInterface, SecondInterface {
// public void myMethod() {
// System.out.println("Some text..");
// }
// public void myOtherMethod() {
// System.out.println("Some other text...");
// }
// }
//
// public class oops {
// public static void main(String[] args) {
// DemoClass myObj = new DemoClass();
// myObj.myMethod();
// myObj.myOtherMethod();
// }
// }

//// EXAMPLES
////A simple interface
//interface In1 {
//
// // public, static and final
// final int a = 10;
//
// // public and abstract
// void display();
//}
//
////A class that implements the interface.
//class TestClass implements In1 {
//
// // Implementing the capabilities of
// // interface.
// public void display(){
// System.out.println("Hello");
// }
//}
//public class oops{
// // Driver Code
// public static void main(String[] args)
// {
// TestClass t = new TestClass();
// t.display();
// System.out.println(t.a);
// }
//}

//Java program to demonstrate the


//real-world example of Interfaces

//interface Vehicle {
//
// // all are the abstract methods.
// void changeGear(int a);
// void speedUp(int a);
// void applyBrakes(int a);
//}
//
//class Bicycle implements Vehicle{
// int speed;
// int gear;
//
// // to change gear
// public void changeGear(int newGear){
// gear = newGear;
// }
//
// // to increase speed
// public void speedUp(int increment){
// speed = speed + increment;
// }
//
// // to decrease speed
// public void applyBrakes(int decrement){
// speed = speed - decrement;
// }
//
// public void printStates() {
// System.out.println("speed: " + speed + " gear: " + gear);
// }
//}
//
//class Bike implements Vehicle {
// int speed;
// int gear;
//
// // to change gear
// public void changeGear(int newGear){
// gear = newGear;
// }
//
// // to increase speed
// public void speedUp(int increment) {
// speed = speed + increment;
// }
//
// // to decrease speed
// public void applyBrakes(int decrement){
// speed = speed - decrement;
// }
//
// public void printStates() {
// System.out.println("speed: " + speed + " gear: " + gear);
// }
//}
//public class oops {
//
// public static void main (String[] args) {
//
// // creating an instance of Bicycle
// // doing some operations
// Bicycle bicycle = new Bicycle();
// bicycle.changeGear(2);
// bicycle.speedUp(3);
// bicycle.applyBrakes(1);
//
// System.out.println("Bicycle present state :");
// bicycle.printStates();
//
// // creating instance of the bike.
// Bike bike = new Bike();
// bike.changeGear(1);
// bike.speedUp(4);
// bike.applyBrakes(3);
//
// System.out.println("Bike present state :");
// bike.printStates();
// }
//}

//Java program to demonstrate How Diamond Problem


//Is Handled in case of Default Methods

////Interface 1
//interface API {
// // Default method
// default void show(){
// // Print statement
// System.out.println("Default API");
// }
//}
//
////Interface 2
////Extending the above interface
//interface Interface1 extends API {
// // Abstract method
// void display();
//}
//
////Interface 3
////Extending the above interface
//interface Interface2 extends API {
// // Abstract method
// void print();
//}
//
////Main class
////Implementation class code
//class TestClass implements Interface1, Interface2 {
// // Overriding the abstract method from Interface1
// public void display(){
// System.out.println("Display from Interface1");
// }
// // Overriding the abstract method from Interface2
// public void print(){
// System.out.println("Print from Interface2");
// }
//}
//public class oops{
// // Main driver method
// public static void main(String args[]) {
// // Creating object of this class
// // in main() method
// TestClass d = new TestClass();
//
// // Now calling the methods from both the interfaces
// d.show(); // Default method from API
// d.display(); // Overridden method from Interface1
// d.print(); // Overridden method from Interface2
// }
//}

//interface A {
// void method1();
// void method2();
//}
//// B now includes method1 and method2
//interface B {
// void method3();
//}
//// the class must implement all method of A and B.
//class gfg implements B , A{
// public void method1(){
// System.out.println("Method 1");
// }
// public void method2(){
// System.out.println("Method 2");
// }
// public void method3(){
// System.out.println("Method 3");
// }
//}
//public class oops{
// public static void main(String args[]) {
// gfg ob = new gfg();
// ob.method1();
// ob.method2();
// ob.method3();
// }
//}

//interface Student {
// public void data();
//}
//class avi implements Student {
// public void data () {
// String name="avinash";
// int rollno=68;
// System.out.println(name);
// System.out.println(rollno);
// }
//}
//public class oops {
// public static void main (String args []) {
// avi h= new avi();
// h.data();
// }
//}

////base interface1
//interface Moveable {
// public void run();
//}
//
////base interface2
//interface Speakable {
// public void speak();
//}
//
////child interface inheriting two base interfaces
//interface Ability extends Moveable, Speakable {
// public void show();
//}
//
//class Person implements Ability {
// public void run() {
// System.out.println("I can run !!");
// }
// public void speak(){
// System.out.println("I can speak !!");
// }
// public void show() {
// System.out.println("I am a person, I can speak and run !!");
// }
//}
//
//public class oops {
// public static void main(String[] args) {
// Person obj = new Person();
// obj.run();
// obj.speak();
// obj.show();
// }
//}

// Static Variable

// No need to create object


//class A{
// static int a = 5;
//}
//public class oops{
// public static void main(String args[]) {
// A n = new A();
// n.a=90;
// A y = new A();
// y.a = 26;
// System.out.println(n.a);
// System.out.println(y.a);
// }
//}

// Static Method

//class A{
// static void get() {
// System.out.println("hai");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A.get();
// }
//}

// Anonymus Object

//class A{
// A(){
// System.out.println("Hai");
// }
//}
//public class oops{
// oops(String a){
// System.out.println("Hi " + a);
// }
// public static void main(String args[]) {
//// oops ob = new oops("Vijay");
// new oops("vijay"); // Anonymus Object
// }
//}

//class A{
// A(){
// System.out.println("Hai Building");
// }
// A(String a){
// System.out.println(a);
// }
//}
//class B extends A{
// B(){
// System.out.println("Hai Building");
// }
// B(String a){
// System.out.println(a);
// }
//}
//public class oops{
// public static void main(String args[]) {
// B ob = new B("Besant");
// }
//}

//
23-10-2024
// Final Keyword
// we don't reassign the value in final variable

//class a{
// final void get() {
// System.out.println("hai");
// }
//}
//class b extends a{
//// void get() { // does't Override
//// System.out.println("hello");
//// }
//}
//public class oops{
// public static void main(String args[]) {
//// final int a = 10; // FINAL VARIABLE
//// a = 30;
//// System.out.println(a);
//
// b ob = new b();;
// ob.get();
// }
//}

//It will shows error because every method should be in same return type.

//class Base{
// void get(String a) {
// System.out.println("hai");
// }
//}
//class Derived extends Base{
// int get(String a) {
// System.out.println("hello");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Derived ob = new Derived();
// ob.get();
// }
//}

//doesn't print the put() method because the object is created only the 'a' class

//class Base{
// void get() {
// System.out.println("hai");
// }
//}
//class Derived extends Base{
// void put() { // does't Override
// System.out.println("hello");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Base ob = new Derived();;
// ob.get();
//// ob.put();
// }
//}

// The empty class is known as Marker class

//class A{
// // Marker class
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
//
// }
//}

// Incase we use return type in any class we should print in the method only in the
main method

//class A{
// int get() {
// return 1;
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// System.out.println(ob.get());
// }
//}

// We create a object in class and call the method and parameter have any integer
or string or double
// incase we dont have the parameter type it takes the object method

//class A{
// void get(int a) {
// System.out.println("Integer Print");
// }
// void get(Object a) {
// System.out.println("Object Print");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// ob.get(123);
// }
//}

//class A{
// void get(Object a, Object b) {
//// System.out.println(a + b); // Doesn't print
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// ob.get(10, 20);
// }
//}

// EXCEPTION HANDLING 28-10-2024


// In Java, Exception is an unwanted or unexpected event, which occurs during the
execution of a program,
// i.e. at run time, that disrupts the normal flow of the program’s instructions.

// TYPE OF EXCEPTION

// 1.Arithmetic Exception
// This exception occurs when an illegal arithmetic operation is performed, such as
dividing by zero

//public class oops{


// public static void main(String args[]) {
// int a = 10, b = 0, c;
// try {
// c = a / b;
// System.out.println(c);
// }
// catch(ArithmeticException e) {
// System.out.println(e);
// }
// }
//}

// 2.ArrayIndexOutOfBoundsException
// This exception is thrown when trying to access an array with an invalid index,
// either negative or greater than or equal to the size of the array.

//public class oops{


//public static void main(String args[]) {
//
// try {
// int a[] = new int[3];
// System.out.println(a[7]);
// }
// catch(ArrayIndexOutOfBoundsException e) {
// System.out.println(e);
// }
//}
//}

// 3.NegativeArraySizeExcepption
// NegativeArraySizeException is a runtime exception in Java that occurs when an
application tries to create an array with a negative size.

//public class oops{


//public static void main(String args[]) {
//
// try {
// int a[] = new int[-3];
// System.out.println(a[7]);
// }
// catch(NegativeArraySizeException e) {
// System.out.println(e);
// }
//}
//}

// 4.StringIndexOutOfBoundException
// It is thrown by String class methods to indicate that an index is either
negative or greater than the size of the string

//public class oops{


// public static void main(String args[]) {
// try {
// String a = "Vijay";
// System.out.println(a.charAt(10));
// }
// catch(StringIndexOutOfBoundsException e) {
// System.err.println(e);
// }
// }
//}

// 5.InputMismatchException
// This exception often arises when using Scanner to read input. For example, if
you expect an integer but the user inputs a string or a float.

//public class oops {


// public static void main(String args[]) {
// Scanner in = new Scanner(System.in);
// int a;
// try {
// a = in.nextInt();
// System.out.println(a);
// }
// catch (InputMismatchException e) {
// System.err.println(e);
// }
// finally {
// System.out.println("Program comes out in Finally :)");
// }
// System.out.println("Program Ended");
// }
//}

//public class oops {


// public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// int number = 0;
//
// while (true) {
// try {
// System.out.print("Enter an integer: ");
// number = scanner.nextInt(); // This line can throw
InputMismatchException
// break; // Exit loop if input is valid
// } catch (InputMismatchException e) {
// System.out.println("That's not an integer! Please try again.");
// scanner.next(); // Clear the invalid input
// }
// }
//
// System.out.println("You entered: " + number);
// }
//}

// 6.NumberFormatException
// NumberFormatException occurs when trying to convert a string to a number and the
string is not in the correct format.

//public class oops {


// public static void main(String args[]) {
// try {
// String a = "Vijay";
// int b = Integer.parseInt(a);
// System.out.println(b);
//
// }
// catch (NumberFormatException e) {
// System.out.println(e);
// }
// }
//}

//public class oops {


// public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// int number = 0;
//
// while (true) {
// try {
// System.out.print("Enter an integer: ");
// String input = scanner.nextLine(); // Read input as a string
// number = Integer.parseInt(input); // Attempt to parse the input
to an integer
// break; // Exit loop if parsing is successful
// } catch (NumberFormatException e) {
// System.out.println("That's not a valid integer! Please try
again.");
// }
// }
//
// System.out.println("You entered: " + number);
// }
//}

// 7.NullPointerException

//public class oops {


//public static void main(String args[]) {
// try {
// String a =null;
// System.out.println(a.length());
//
// }
// catch (NullPointerException e) {
// System.out.println(e);
// }
//}
//}

//public class oops {


// static class Person {
// String name;
// }
// public static void main(String[] args) {
// String str = null;
//
// try {
// // Attempting to call a method on a null reference
// int length = str.length(); // This will throw NullPointerException
// } catch (NullPointerException e) {
// System.out.println("Caught a NullPointerException: " +
e.getMessage());
// }
//
// // Example with an object
// Person person = null;
// try {
// System.out.println(person.name); // This will also throw
NullPointerException
// } catch (NullPointerException e) {
// System.out.println("Caught a NullPointerException when accessing
person's name.");
// }
// }
//
//
//}

// EXAMPLES IN EXCEPTION
// 1.
public class oops{
static void divideNumbers(int num, int den) {
int result = 0;
try {
result = num / den;
System.out.println("Program Ended , Result : " + result);
if(result == 0) {
throw new ArithmeticException("Denominator should be
greater than Zero");
}
}
catch(ArithmeticException e) {
System.err.println("Cannot Divide By Zero");
}

}
public static void main(String args[]) {
divideNumbers(10,0);
divideNumbers(10,2);
Scanner in = new Scanner(System.in);
// oops ob = new oops();
int n1 = in.nextInt();
int n2 = in.nextInt();
divideNumbers(n1, n2);
System.err.println("Cannot Divide By Zero");
System.err.println("Cannot Divide By Zero");
System.err.println("Cannot Divide By Zero");

}
}

// 2.
//class invalidAgeException extends Exception{
// invalidAgeException(String s){
// super(s);
// }
//}
//class AgeValidator{
// static void CheckAge(int age) {
// try {
// if(age < 0 ||age >150) {
// throw new invalidAgeException("Age should not be less than
0 and greater than 150");
// }
// else {
// System.out.println("Age is valid");
// }
// }
// catch(invalidAgeException e){
// System.err.println(e);
// }
// }
//}
//public class oops{
// public static void main(String args[]) {
// Scanner in = new Scanner(System.in);
// int n1 = in.nextInt();
// AgeValidator ob = new AgeValidator();
// ob.CheckAge(n1);
// }
//}

// 3.
//class oops {
// static void fun()
// {
// try {
// throw new NullPointerException("demo");
// }
// catch (Exception e) {
// System.out.println("Caught inside fun()."+ e);
// throw e; // rethrowing the exception
// }
// }
//
// public static void main(String args[])
// {
// try {
// fun();
// }
// catch (Exception e) {
// System.out.println("Caught in main. "+e);
// }
// }
// }

//class A extends Thread{


// public void run() {
// for(int i=1;i<4;i++) {
// try {
// Thread.sleep(3000);
// System.out.println("Current Thread
Name :"+Thread.currentThread().getName());
// System.out.println("Current Thrad
Id :"+Thread.currentThread().getId());
// }
// catch(Exception e) {
// System.out.println(e);
// }
// }
// }
//}
//
//public class oops{
// public static void main(String args[]) {
// A ob=new A();
// ob.start();
// ob.setName("Vignesh");
// A ob1=new A();
// ob1.start();
// ob1.setName("Vicky");
// }
//}

//class A extends Thread{


// void get() {
// System.out.println("Current Thread
Name :"+Thread.currentThread().getName());
// }
//}
//
//public class oops{
// public static void main(String args[]) {
// A ob=new A();
//// ob.start();
//// ob.start();
//// ob.get();
//// ob.setName("vijay");
// }
//}

You might also like