🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

CheerfulExuberance7373

Uploaded by CheerfulExuberance7373

Tags

Java programming object-oriented programming software development

Full Transcript

Core JAVA UNIT 1 By Shivani Deopa INTRODUCTION TO JAVA Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is intended to let application developers write once, and run anywhere (WORA), mean...

Core JAVA UNIT 1 By Shivani Deopa INTRODUCTION TO JAVA Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is intended to let application developers write once, and run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java was first released in 1995 and is widely used for developing applications for desktop, web, and mobile devices. Java is known for its simplicity, robustness, and security features, making it a popular choice for enterprise-level applications. JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1995 and later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs. Java is a class- based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++. Java Terminology Before learning Java, one must be familiar with these common terms of Java. 1. Java Virtual Machine(JVM): This is generally referred to as JVM. There are three execution phases of a program. They are written, compile and run the program. Writing a program is done by a java programmer like you and me. The compilation is done by the JAVAC compiler which is a primary Java compiler included in the Java development kit (JDK). It takes the Java program as input and generates bytecode as output. In the Running phase of a program, JVM executes the bytecode generated by the compiler. Every Operating System has a different JVM but the output they produce after the execution of bytecode is the same across all the operating systems. This is why Java is known as a platform-independent language. 2. Bytecode in the Development Process: The Javac compiler of JDK compiles the java source code into bytecode so that it can be executed by JVM. It is saved as.class file by the compiler. 3. Java Development Kit(JDK): So, as the name suggests, it is a complete Java development kit that includes everything including compiler, Java Runtime Environment (JRE), java debuggers, java docs, etc. For the program to execute in java, we need to install JDK on our computer in order to create, compile and run the java program. 4. Java Runtime Environment (JRE): JDK includes JRE. JRE installation on our computers allows the java program to run, however, we cannot compile it. JRE includes a browser, JVM, applet support, and plugins. For running the java program, a computer needs JRE. 5. Garbage Collector: In Java, programmers can’t delete the objects. To delete or recollect that memory JVM has a program called Garbage Collector. Garbage Collectors can recollect the objects that are not referenced. So Java makes the life of a programmer easy by handling memory management. However, programmers should be careful about their code whether they are using objects that have been used for a long time. Because Garbage cannot recover the memory of objects being referenced. 6. ClassPath: The classpath is the file path where the java runtime and Java compiler look for.class files to load. By default, JDK provides many libraries. If you want to include external libraries they should be added to the classpath. Features of Java 1. Platform Independent: Compiler converts source code to bytecode and then the JVM executes the bytecode generated by the compiler. This bytecode can run on any platform be it Windows, Linux, or macOS which means if we compile a program on Windows, then we can run it on Linux and vice versa. Each operating system has a different JVM, but the output produced by all the OS is the same after the execution of the bytecode. That is why we call java a platform-independent language. 2. Object-Oriented Programming Language: Organizing the program in the terms of a collection of objects is a way of object-oriented programming, each of which represents an instance of the class. The four main concepts of Object-Oriented programming are: Abstraction Encapsulation Inheritance Polymorphism 3. Simple: Java is one of the simple languages as it does not have complex features like pointers, operator overloading, multiple inheritances, and Explicit memory allocation. 4. Robust: Java language is robust which means reliable. It is developed in such a way that it puts a lot of effort into checking errors as early as possible, that is why the java compiler is able to detect even those errors that are not easy to detect by another programming language. The main features of java that make it robust are garbage collection, Exception Handling, and memory allocation. 5. Secure: In java, we don’t have pointers, so we cannot access out-of-bound arrays i.e it shows ArrayIndexOutOfBound Exception if we try to do so. That’s why several security flaws like stack corruption or buffer overflow are impossible to exploit in Java. Also, java programs run in an environment that is independent of the os(operating system) environment which makes java programs more secure. 6. Distributed: We can create distributed applications using the java programming language. Remote Method Invocation and Enterprise Java Beans are used for creating distributed applications in java. The java programs can be easily distributed on one or more systems that are connected to each other through an internet connection. 7. Multithreading: Java supports multithreading. It is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of the CPU. 8. Portable: As we know, java code written on one machine can be run on another machine. The platform-independent feature of java in which its platform-independent bytecode can be taken to any platform for execution makes java portable. 9. High Performance: Java architecture is defined in such a way that it reduces overhead during the runtime and at some times java uses Just In Time (JIT) compiler where the compiler compiles code on-demand basics where it only compiles those methods that are called making applications to execute faster. 10. Dynamic flexibility: Java being completely object-oriented gives us the flexibility to add classes, new methods to existing classes, and even create new classes through sub- classes. Java even supports functions written in other languages such as C, C++ which are referred to as native methods. 11. Sandbox Execution: Java programs run in a separate space that allows user to execute their applications without affecting the underlying system with help of a bytecode verifier. Bytecode verifier also provides additional security as its role is to check the code for any violation of access. 12. Write Once Run Anywhere: As discussed above java application generates a ‘.class’ file that corresponds to our applications(program) but contains code in binary format. It provides ease t architecture-neutral ease as bytecode is not dependent on any machine architecture. It is the primary reason java is used in the enterprising IT industry globally worldwide. 13. Power of compilation and interpretation: Most languages are designed with the purpose of either they are compiled language or they are interpreted language. But java integrates arising enormous power as Java compiler compiles the source code to bytecode and JVM executes this bytecode to machine OS-dependent executable code. Java programming format // Importing classes from packages import java.io.*; // Main class public class Demo { // Main driver method public static void main(String[] args) { // Print statement System.out.println(“My first Java Code"); } } Java Tokens A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows: 1. Keywords 2. Identifiers 3. Constants 4. Special Symbols 5. Operators Keyword: Keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. Since keywords are referred names for a compiler, they can’t be used as variable names because by doing so, we are trying to assign a new meaning to the keyword which is not allowed. Java language supports following keywords: abstract, boolean, case, catch, char, class, const, do, double, else, extends, float, for, goto, if, implements, import, int, interface, long, package, private, protected, public, return, static, switch, try, void, while etc Identifiers: Identifiers are used as the general terminology for naming of variables, functions and arrays. These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers; they are reserved for special use. Once declared, you can use the identifier in later program statements to refer to the associated value. A special kind of identifier, called a statement label, can be used in goto statements. Examples of valid identifiers : MyVariable MYVARIABLE myvariable x x1 i1 _myvariable $myvariable sum_of_array Special Symbols: The following special symbols are used in Java having some special meaning and thus, cannot be used for some other purpose. Eg- [] () {}, ; * = 1. Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts. 2. Parentheses(): These special symbols are used to indicate function calls and function parameters. 3. Braces{}: These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement. 4. comma (, ): It is used to separate more than one statements like for separating parameters in function calls. 5. semi colon : It is an operator that essentially invokes something called an initialization list. 6. asterick (*): It is used to create pointer variable. Constants/Literals: Constants are also like normal variables. But, the only difference is, their values can not be modified by the program once they are defined. Constants refer to fixed values. They are also called as literals. Constants may belong to any of the data type. Syntax: final data_type variable_name; Operators: Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types are- Arithmetic Operators Unary Operators Assignment Operator Relational Operators Logical Operators Bitwise Operators etc Java Statements In Java, a statement is an executable instruction that tells the compiler what to perform. It forms a complete command to be executed and can include one or more expressions. A sentence forms a complete idea that can include one or more clauses. Types of Statements 1. Expression Statements 2. Declaration Statements 3. Control Statements Expression Statements Expression is an essential building block of any Java program. Generally, it is used to generate a new value. Sometimes, we can also assign a value to a variable. In Java, expression is the combination of values, variables, operators, and method calls. There are three types of expressions in Java: 1. Expressions that produce a value. For example, (6+9), (9%2), (pi*radius) + 2. Note that the expression enclosed in the parentheses will be evaluate first, after that rest of the expression. 2. Expressions that assign a value. For example, number = 90, pi = 3.14. Expression that neither produces any result nor assigns a value. For example, increment or decrement a value by using increment or decrement operator respectively, method invocation, etc. These expressions modify the value of a variable or state (memory) of a program. For example, count++, int sum = a + b; The expression changes only the value of the variable sum. The value of variables a and b do not change, so it is also a side effect. Declaration Statements In declaration statements, we declare variables and constants by specifying their data type and name. A variable holds a value that is going to use in the Java program. For example: int quantity; boolean flag; String message; Java also allows us to declare multiple variables in a single declaration statement. Note that all the variables must be of the same data type. int quantity, batch_number, lot_number; boolean flag = false, isContains = true; String message = "Hello", how are you; Control Statement Control statements decide the flow (order or sequence of execution of statements) of a Java program. In Java, statements are parsed from top to bottom. Therefore, using the control flow statements can interrupt a particular section of a program based on a certain condition. Java Data Types Data types in Java are of different sizes and values that can be stored in the variable that is made as per convenience and circumstances to cover up all test cases. Java has two categories in which data types are segregated 1. Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double 2. Non-Primitive Data Type or Object Data type: such as String, Array, etc. Typecasting Type casting is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion performed by the programmer. Types of Type Casting: 1. Widening Type Casting 2. Narrowing Type Casting Widening Type Casting Converting a lower data type into a higher one is called widening type casting. It is also known as implicit conversion or casting down. It is done automatically. It is safe because there is no chance to lose data. It takes place when: Both data types must be compatible with each other. The target type must be larger than the source type. byte -> short -> char -> int -> long -> float -> double Narrowing Type Casting Converting a higher data type into a lower one is called narrowing type casting. It is also known as explicit conversion or casting up. It is done manually by the programmer. If we do not perform casting then the compiler reports a compile-time error. double -> float -> long -> int -> char -> short -> byte Arrays Array in java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. In Java, all arrays are dynamically allocated. Arrays are stored in contiguous memory [consecutive memory locations]. Since arrays are objects in Java, we can find their length using the object property length. This is different from C/C++, where we find length using sizeof. A Java array variable can also be declared like other variables with [] after the data type. The variables in the array are ordered, and each has an index beginning with 0. Java array can also be used as a static field, a local variable, or a method parameter. The size of an array must be specified by int or short value and not long. Every array type implements the interfaces Cloneable and java.io.Serializable. This storage of arrays helps us randomly access the elements of an array [Support Random Access]. The size of the array cannot be altered(once initialized). However, an array reference can be made to point to another array. An array can contain primitives (int, char, etc.) and object (or non- primitive) references of a class depending on the definition of the array. In the case of primitive data types, the actual values are stored in contiguous memory locations. In the case of class objects, the actual objects are stored in a heap segment. One-Dimensional Arrays: The general form of a one-dimensional array declaration is type var-name[]; OR type[] var-name; An array declaration has two components: the type and the name. type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like an array of integers, we can also create an array of other primitive data types like char, float, double, etc., or user- defined data types (objects of a class). Thus, the element type for the array determines what type of data the array will hold. Example: // both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; Multidimensional Arrays: Multidimensional arrays are arrays of arrays with each element of the array holding the reference of other arrays. These are also known as Jagged Arrays. A multidimensional array is created by appending one set of square brackets ([]) per dimension. Syntax : datatype [][] arrayrefvariable; or datatype arrayrefvariable[][]; import java.io.*; class multiarray { public static void main (String[] args) { // Syntax int [][] arr= new int; // 3 row and 3 column } } OOPS Object-Oriented Programming or OOPs refers to languages that use objects in programming, they use objects as a primary source to implement what is to happen in the code. Objects are seen by the viewer or user, performing tasks assigned by you. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. Let us discuss prerequisites by polishing concepts of method declaration and message passing. Starting off with the method declaration, it consists of six components: Access Modifier: Defines the access type of the method i.e. from where it can be accessed in your application. In Java, there are 4 types of access specifiers: 1. public: Accessible in all classes in your application. 2. protected: Accessible within the package in which it is defined and in its subclass(es) (including subclasses declared outside the package). 3. private: Accessible only within the class in which it is defined. 4. default (declared/defined without using any modifier): Accessible within the same class and package within which its class is defined. The return type: The data type of the value returned by the method or void if it does not return a value. Method Name: The rules for field names apply to method names as well, but the convention is a little different. Parameter list: Comma-separated list of the input parameters that are defined, preceded by their data type, within the enclosed parentheses. If there are no parameters, you must use empty parentheses (). Exception list: The exceptions you expect the method to throw. You can specify these exception(s). Method body: It is the block of code, enclosed between braces, that you need to execute to perform your intended operations. OOPS concepts are as follows: Class Object Method and method passing Pillars of OOPs Abstraction Encapsulation Inheritance Polymorphism Compile-time polymorphism Runtime polymorphism Classes and Object A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. Using classes, you can create multiple objects with the same behavior instead of writing their code multiple times. This includes classes for objects occurring more than once in your code. In general, class declarations can include these components in order: Modifiers: A class can be public or have default access Class name: The class name should begin with the initial letter capitalized by convention. Superclass (if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. Interfaces (if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. Body: The class body is surrounded by braces, { }. An object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. The objects are what perform your code, they are the part of your code visible to the viewer/user. An object mainly consists of: State: It is represented by the attributes of an object. It also reflects the properties of an object. Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects. Identity: It is a unique name given to an object that enables it to interact with other objects. Method: A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping it, which is why they are considered time savers. In Java, every method must be part of some class, which is different from languages like C, C++, and Python. Static Keywords The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class. The static keyword is used for a constant variable or a method that is the same for every instance of a class. The static keyword is a non-access modifier in Java that is applicable for the following: 1. Blocks 2. Variables 3. Methods 4. Classes Characteristics of static keyword: Here are some characteristics of the static keyword in Java: 1. Shared memory allocation: Static variables and methods are allocated memory space only once during the execution of the program. This memory space is shared among all instances of the class, which makes static members useful for maintaining global state or shared functionality. 2. Accessible without object instantiation: Static members can be accessed without the need to create an instance of the class. This makes them useful for providing utility functions and constants that can be used across the entire program. 3. Associated with class, not objects: Static members are associated with the class, not with individual objects. This means that changes to a static member are reflected in all instances of the class, and that you can access static members using the class name rather than an object reference. 4. Cannot access non-static members: Static methods and variables cannot access non-static members of a class, as they are not associated with any particular instance of the class. 5. Can be overloaded, but not overridden: Static methods can be overloaded, which means that you can define multiple methods with the same name but different parameters. However, they cannot be overridden, as they are associated with the class rather than with a particular instance of the class. class Test Static blocks { If you need to do the computation in // static variable order to initialize your static variables, static int a = 10; you can declare a static block that gets static int b; executed exactly once, when the class // static block is first loaded. static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String[] args) { System.out.println("from main"); System.out.println("Value of a : "+a); System.out.println("Value of b : "+b); } } Static variables // Java program to demonstrate execution of static blocks and variables class Test When a variable is declared as static, then a { single copy of the variable is created and // static variable shared among all objects at the class level. Static variables are, essentially, global static int a = m1(); variables. All instances of the class share the // static block same static variable. static { System.out.println("Inside static block"); Important points for static variables: } We can create static variables at the class // static method level only. static int m1() { static block and static variables are executed System.out.println("from m1"); in the order they are present in a program. return 20; } // static method(main !!) public static void main(String[] args) { System.out.println("Value of a : "+a); System.out.println("from main"); } } When to use static variables and methods? Use the static variable for the property that is common to all objects. For example, in class Student, all students share the same college name. Use static methods for changing static variables. Consider the following java program, that illustrates the use of static keywords with variables and methods. class Student { static void setCllg(String name) { cllgName = name; } String name; // instance method void getStudentInfo() int rollNo; { // static variable System.out.println("name : " + this.name); static String cllgName; System.out.println("rollNo : " + this.rollNo); // static counter to set unique roll no // accessing static variable static int counter = 0; System.out.println("cllgName : " + cllgName); public Student(String name) } // Driver class { public class StaticDemo { this.name = name; public static void main(String[] args) this.rollNo = setRollNo(); { } // calling static method // getting unique rollNo through static variable(counter) // without instantiating Student class static int setRollNo() Student.setCllg("XYZ"); { Student s1 = new Student("Alice"); Student s2 = new Student("Bob"); counter++; s1.getStudentInfo(); return counter; s2.getStudentInfo(); } } // static method } Constructors In Java, Constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method that is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called. How Java Constructors are Different From Java Methods? Constructors must have the same name as the class within which it is defined it is not necessary for the method in Java. Constructors do not return any type while method(s) have the return type or void if does not return any value. Constructors are called only once at the time of Object creation while method(s) can be called any number of times. When Constructor is called? Each time an object is created using a new() keyword, at least one constructor (it could be the default constructor) is invoked to assign initial values to the data members of the same class. Rules for writing constructors are as follows: The constructor(s) of a class must have the same name as the class name in which it resides. A constructor in Java can not be abstract, final, static, or Synchronized. Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor. Types of Constructors in Java 1. Default Constructor 2. Parameterized Constructor 1. Default Constructor in Java // Java Program to demonstrate // Default Constructor A constructor that has no import java.io.*; parameters is known as default // Driver class the constructor. class ABC { A default constructor is invisible. And if we write a constructor // Default Constructor with no arguments, the compiler ABC() { does not create a default System.out.println("Default constructor"); constructor. It is taken out. } It is being overloaded and called // Driver function a parameterized constructor. The public static void main(String[] args) default constructor changed into { the parameterized constructor. ABC obj = new ABC(); But Parameterized constructor } can’t change the default } constructor. import java.io.*; 2. Parameterized Constructor in class ABC { Java String name; A constructor that has int id; ABC(String name, int id) parameters is known as { parameterized constructor. this.name = name; If we want to initialize fields of } this.id = id; the class with our own values, } then use a parameterized class GFG { constructor. public static void main(String[] args) { // This would invoke the parameterized constructor. ABC obj1 = new ABC("avinash", 68); System.out.println("Name :" + obj1.name + " and ID :" + obj1.id); } } this Key Word In Java, ‘this’ is a reference variable that refers to the current object, or can be said “this” in Java is a keyword that refers to the current object instance. It can be used to call current class methods and fields, to pass an instance of the current class as a parameter, and to differentiate between the local and instance variables. Using “this” reference can improve code readability and reduce naming conflicts. Methods to use ‘this’ in Java 1. Using the ‘this’ keyword to refer to current class instance variables. 2. Using this() to invoke the current class constructor 3. Using ‘this’ keyword to return the current class instance 4. Using ‘this’ keyword as the method parameter 5. Using ‘this’ keyword to invoke the current class method 6. Using ‘this’ keyword as an argument in the constructor call Below is the implementation of this reference: public class ThisExample { void show() { int num = 10; System.out.println("Inside show public ThisExample() { method"); } System.out.println("Insideconstructor"); public static void main(String[] args) { } ThisExample obj = public ThisExample(int num) new ThisExample(100); { obj.display(); // Invoking default constructor } this(); } // Assigning the local variable num to the instance this.num = num; } Advantages of using ‘this’ reference There are certain advantages of using ‘this’ reference in Java as mentioned below: It helps to distinguish between instance variables and local variables with the same name. It can be used to pass the current object as an argument to another method. It can be used to return the current object from a method. It can be used to invoke a constructor from another overloaded constructor in the same class. Disadvantages of using ‘this’ reference Although ‘this’ reference comes with many advantages there are certain disadvantages of also: Overuse of this can make the code harder to read and understand. Using this unnecessarily can add unnecessary overhead to the program. Using this in a static context results in a compile-time error. Overall, this keyword is a useful tool for working with objects in Java, but it should be used judiciously and only when necessary. Inheritance Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well. Why Do We Need Java Inheritance? Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can directly use the parent class code. Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by which Java achieves Run Time Polymorphism. Abstraction: The concept of abstract where we do not have to provide all details is achieved through inheritance. Abstraction only shows the functionality to the user. Important Terminologies Used in Java Inheritance Class: Class is a set of objects which shares common characteristics/ behavior and common properties/ attributes. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created. Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a base class or a parent class). Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods. Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. How to Use Inheritance in Java? The extends keyword is used for inheritance in Java. Using the extends keyword indicates you are derived from an existing class. In other words, “extends” refers to increased functionality. Syntax : class derived-class extends base-class { //methods and fields } Java Inheritance Types Below are the different types of inheritance which are supported by Java. 1. Single Inheritance 2. Multilevel Inheritance 3. Hierarchical Inheritance 4. Multiple Inheritance 5. Hybrid Inheritance Single Inheritance In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves as a base class for the derived class B. Multilevel Inheritance In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members. Hierarchical Inheritance In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived classes B, C, and D. Multiple Inheritance (Through Interfaces) In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through Interfaces. In the image below, Class C is derived from interfaces A and B. Hybrid Inheritance(Through Interfaces) It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In Java, we can achieve hybrid inheritance only through Interfaces. super Key Word The super keyword in Java is a reference variable that is used to refer to parent class objects The keyword “super” came into the picture with the concept of Inheritance. It is majorly used in the following contexts as mentioned below: 1. Use of super with variables 2. Use of super with methods 3. Use of super with constructors Characteristics of super Keyword in Java In Java, the super keyword is used to refer to the parent class of a subclass. Here are some of its characteristics: super is used to call a superclass constructor: When a subclass is created, its constructor must call the constructor of its parent class. This is done using the super() keyword, which calls the constructor of the parent class. super is used to call a superclass method: A subclass can call a method defined in its parent class using the super keyword. This is useful when the subclass wants to invoke the parent class’s implementation of the method in addition to its own. super is used to access a superclass field: A subclass can access a field defined in its parent class using the super keyword. This is useful when the subclass wants to reference the parent class’s version of a field. super must be the first statement in a constructor: When calling a superclass constructor, the super() statement must be the first statement in the constructor of the subclass. super cannot be used in a static context: The super keyword cannot be used in a static context, such as in a static method or a static variable initializer. super is not required to call a superclass method: While it is possible to use the super keyword to call a method in the parent class, it is not required. If a method is not overridden in the subclass, then calling it without the super keyword will invoke the parent class’s implementation. class Vehicle { Use of super with Variables int maxSpeed = 120; This scenario occurs when a } class Car extends Vehicle { derived class and base class has int maxSpeed = 180; the same data members. In that void display() case, there is a possibility of { ambiguity for the JVM. System.out.println("Maximum Speed: " + super.maxSpeed); } } In the example, both the base // Driver Program class and subclass have a class Test { member maxSpeed. We could public static void main(String[] args) access maxSpeed of base class in { subclass using super keyword. Car small = new Car(); small.display(); } } class Person { Use of super with Methods void message() { System.out.println("This is person class\n"); } This is used when we want to call } the parent class method. So class Student extends Person { whenever a parent and child class void message() { System.out.println("This is student class"); } have the same-named methods void display() then to resolve ambiguity we use { the super keyword. message(); super.message(); } In the example, we have seen that } class Test { if we only call method message() public static void main(String args[]) then, the current class message() is { invoked but with the use of the Student s = new Student(); super keyword, message() of the s.display(); } superclass could also be invoked. } class Person { Use of super with constructors Person() { The super keyword can also be System.out.println("Person class Constructor"); used to access the parent class } constructor. One more } class Student extends Person { important thing is that ‘super’ Student() can call both parametric as well { as non-parametric constructors super(); System.out.println("Student class Constructor"); depending on the situation. } } class Test { public static void main(String[] args) { Student s = new Student(); } } Advantages of Using Java super keyword The super keyword in Java provides several advantages in object-oriented programming: 1. Enables reuse of code: Using the super keyword allows subclasses to inherit functionality from their parent classes, which promotes the reuse of code and reduces duplication. 2. Supports polymorphism: Because subclasses can override methods and access fields from their parent classes using super, polymorphism is possible. This allows for more flexible and extensible code. 3. Provides access to parent class behavior: Subclasses can access and use methods and fields defined in their parent classes through the super keyword, which allows them to take advantage of existing behavior without having to re- implement it. 4. Allows for customization of behavior: By overriding methods and using super to call the parent implementation, subclasses can customize and extend the behavior of their parent classes. 5. Facilitates abstraction and encapsulation: The use of super promotes encapsulation and abstraction by allowing subclasses to focus on their own behavior while relying on the parent class to handle lower-level details. Polymorphism (overloading and overriding) Polymorphism is considered one of the important features of Object- Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms. In Java polymorphism is mainly divided into two types: 1. Compile-time Polymorphism 2. Runtime Polymorphism Compile-Time Polymorphism It is also known as static polymorphism. This type of polymorphism is achieved by function. class Helper { Method Overloading static int Multiply(int a, int b) When there are multiple { functions with the same name return a * b; but different parameters then } these functions are said to be static double Multiply(double a, double b) overloaded. { Functions can be overloaded by return a * b; changes in the number of } arguments or/and a change in } the type of arguments. class GFG { public static void main(String[] args) { System.out.println(Helper.Multiply(2, 4)); System.out.println(Helper.Multiply(5.5, 6.3)); } } class Parent { void Print() Runtime Polymorphism { It is also known as Dynamic System.out.println("parent class"); } Method Dispatch. It is a process } in which a function call to the class subclass1 extends Parent { overridden method is resolved } void Print() { System.out.println("subclass1"); } at Runtime. This type of class subclass2 extends Parent { polymorphism is achieved by void Print() Method Overriding. { Method overriding, on the other System.out.println("subclass2"); } hand, occurs when a derived } class has a definition for one of class GFG { the member functions of the public static void main(String[] args) { base class. That base function is Parent a; said to be overridden. a = new subclass1(); a.Print(); a = new subclass2(); a.Print(); } } Advantages of Polymorphism in Java Increases code reusability by allowing objects of different classes to be treated as objects of a common class. Improves readability and maintainability of code by reducing the amount of code that needs to be written and maintained. Supports dynamic binding, enabling the correct method to be called at runtime, based on the actual class of the object. Enables objects to be treated as a single type, making it easier to write generic code that can handle objects of different types. Disadvantages of Polymorphism in Java Can make it more difficult to understand the behavior of an object, especially if the code is complex. This may lead to performance issues, as polymorphic behavior may require additional computations at runtime. Interfaces An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. When we decide a type of entity by its behaviour and not via attribute we should define it as an interface. Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). Interfaces specify what a class must do and not how. It is the blueprint of the behaviour. Interface do not have constructor. Represent behaviour as interface unless every sub-type of the class is guarantee to have that behaviour. An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement. If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract. To declare an interface, use the interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword. Why do we use an Interface? It is used to achieve total abstraction. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances. Any class can extend only 1 class but can any class implement infinite number of interface. It is also used to achieve loose coupling. Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes? The reason is, abstract classes may contain non-final variables, whereas variables in the interface are final, public and static. import java.io.*; The major differences between a class and an interface In1 { interface are: // public, static and final final int a = 10; S. No. Class Interface // public and abstract void display(); In an interface, you In class, you can } can’t instantiate 1. instantiate variables and class TestClass implements In1 { variables and create create an object. public void display(){ an object. System.out.println(“HELLO"); The interface cannot Class can contain } contain concrete(with public static void main(String[] args) 2. concrete(with implementation) { implementation) methods methods TestClass t = new TestClass(); t.display(); The access specifiers In Interface only one System.out.println(a); used with classes are 3. specifier is used- } private, protected, and Public. public. } Abstraction In Java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces. Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects. Java Abstract classes and Java Abstract methods An abstract class is a class that is declared with an abstract keyword. An abstract method is a method that is declared without implementation. An abstract class may or may not have all abstract methods. Some of them can be concrete methods A method-defined abstract must always be redefined in the subclass, thus making overriding compulsory or making the subclass itself abstract. Any class that contains one or more abstract methods must also be declared with an abstract keyword. There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with the new operator. An abstract class can have parameterized constructors and the default constructor is always present in an abstract class. When to use abstract classes and abstract methods There are situations in which we will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. Sometimes we will want to create a superclass that only defines a generalization form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Consider a classic “shape” example, perhaps used in a computer-aided design system or game simulation. The base type is “shape” and each shape has a color, size, and so on. From this, specific types of shapes are derived(inherited)- circle, square, triangle, and so on — each of which may have additional characteristics and behaviors. For example, certain shapes can be flipped. Some behaviors may be different, such as when you want to calculate the area of a shape. The type hierarchy embodies both the similarities and differences between the shapes. abstract class Animal { class Cat extends Animal { private String name; public Cat(String name) { super(name); } public Animal(String name) public void makeSound() { this.name = name; } { public abstract void makeSound(); System.out.println(getName() + " meows"); public String getName() } { return name; } } } public class AbstractionExample { class Dog extends Animal { public static void main(String[] args) public Dog(String name) { super(name); } { public void makeSound() Animal myDog = new Dog("Buddy"); { Animal myCat = new Cat("Fluffy"); System.out.println(getName() + " barks"); myDog.makeSound(); } myCat.makeSound(); } } } Here are some reasons why we use abstract in Java: 1. Abstraction: Abstract classes are used to define a generic template for other classes to follow. They define a set of rules and guidelines that their subclasses must follow. By providing an abstract class, we can ensure that the classes that extend it have a consistent structure and behavior. This makes the code more organized and easier to maintain. 2. Polymorphism: Abstract classes and methods enable polymorphism in Java. Polymorphism is the ability of an object to take on many forms. This means that a variable of an abstract type can hold objects of any concrete subclass of that abstract class. This makes the code more flexible and adaptable to different situations. 3. Frameworks and APIs: Java has numerous frameworks and APIs that use abstract classes. By using abstract classes developers can save time and effort by building on existing code and focusing on the aspects specific to their applications. In summary, the abstract keyword is used to provide a generic template for other classes to follow. It is used to enforce consistency, inheritance, polymorphism, and encapsulation in Java. Advantages of Abstraction It reduces the complexity of viewing things. Avoids code duplication and increases reusability. Helps to increase the security of an application or program as only essential details are provided to the user. It improves the maintainability of the application and the modularity of the application. The enhancement will become very easy because without affecting end-users we can able to perform any type of changes in our internal system. Improves code reusability and maintainability. Hides implementation details and exposes only relevant information. Provides a clear and simple interface to the user. Increases security by preventing access to internal class details. Supports modularity, as complex systems can be divided into smaller and more manageable parts. Abstraction provides a way to hide the complexity of implementation details from the user, making it easier to understand and use. Abstraction allows for flexibility in the implementation of a program, as changes to the underlying implementation details can be made without affecting the user-facing interface. Abstraction enables modularity and separation of concerns, making code more maintainable and easier to debug. Disadvantages of Abstraction in Java: Abstraction can make it more difficult to understand how the system works. It can lead to increased complexity, especially if not used properly. This may limit the flexibility of the implementation. Abstraction can add unnecessary complexity to code if not used appropriately, leading to increased development time and effort. Abstraction can make it harder to debug and understand code, particularly for those unfamiliar with the abstraction layers and implementation details. Overuse of abstraction can result in decreased performance due to the additional layers of code and indirection. Abstract Classes Java abstract class is a class that can not be initiated by itself, it needs to be subclassed by another class to use its properties. An abstract class is declared using the “abstract” keyword in its class definition. abstract class Shape { int color; // An abstract function abstract void draw(); } Some important observations about abstract classes are as follows: An instance of an abstract class can not be created. Constructors are allowed. We can have an abstract class without any abstract method. There can be a final method in abstract class but any abstract method in class(abstract class) can not be declared as final or in simpler terms final method can not be abstract itself as it will yield an error: “Illegal combination of modifiers: abstract and final” We can define static methods in an abstract class We can use the abstract keyword for declaring top-level classes (Outer class) as well as inner classes as abstract If a class contains at least one abstract method then compulsory should declare a class as abstract If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child class as abstract so that the next level Child class should provide implementation to the remaining abstract method abstract class Sunstar { abstract void printInfo(); } // Abstraction performed using extends class Employee extends Sunstar { void printInfo() { String name = "avinash"; int age = 21; float salary = 222.2F; System.out.println(name); System.out.println(age); System.out.println(salary); } } class Base { public static void main(String args[]) { Sunstar s = new Employee(); s.printInfo(); } } Encapsulation Encapsulation is a fundamental concept in object-oriented programming (OOP) that refers to the bundling of data and methods that operate on that data within a single unit, which is called a class in Java. Encapsulation is a way of hiding the implementation details of a class from outside access and only exposing a public interface that can be used to interact with the class. In Java, encapsulation is achieved by declaring the instance variables of a class as private, which means they can only be accessed within the class. To allow outside access to the instance variables, public methods called getters and setters are defined, which are used to retrieve and modify the values of the instance variables, respectively. By using getters and setters, the class can enforce its own data validation rules and ensure that its internal state remains consistent. Encapsulation is defined as the class Person { private String name; wrapping up of data under a private int age; single unit. It is the mechanism public String getName() { return name; } public void setName(String name) { this.name = name; } that binds together code and the public int getAge() { return age; } data it manipulates. public void setAge(int age) { this.age = age; } } Another way to think about public class Main { encapsulation is, that it is a public static void main(String[] args) protective shield that prevents { Person person = new Person(); the data from being accessed by person.setName("John"); the code outside this shield. person.setAge(30); System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); } } Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of its own class in which it is declared. As in encapsulation, the data in a class is hidden from other classes using the data hiding concept which is achieved by making the members or methods of a class private, and the class is exposed to the end-user or the world without providing any details behind implementation using the abstraction concept, so it is also known as a combination of data-hiding and abstraction. Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. It is more defined with the setter and getter method. Advantages of Encapsulation: 1. Data Hiding: it is a way of restricting the access of our data members by hiding the implementation details. Encapsulation also provides a way for data hiding. The user will have no idea about the inner implementation of the class. It will not be visible to the user how the class is storing values in the variables. The user will only know that we are passing the values to a setter method and variables are getting initialized with that value. 2. Increased Flexibility: We can make the variables of the class read-only or write-only depending on our requirements. If we wish to make the variables read-only then we have to omit the setter methods like setName(), setAge(), etc. from the above program or if we wish to make the variables write-only then we have to omit the get methods like getName(), getAge(), etc. from the above program 3. Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements. 4. Testing code is easy: Encapsulated code is easy to test for unit testing. 5. Freedom to programmer in implementing the details of the system: This is one of the major advantage of encapsulation that it gives the programmer freedom in implementing the details of a system. The only constraint on the programmer is to maintain the abstract interface that outsiders see. class Account { public class ABC { // private data members to hide the data public static void main(String[] args) private long acc_no; { private String name, email; // creating instance of Account class private float amount; Account acc = new Account(); // public getter and setter methods // setting values through setter methods public long getAcc_no() { return acc_no; } acc.setAcc_no(7310805450L); public void setAcc_no(long acc_no) acc.setName("MD FAIZ"); { this.acc_no = acc_no; } acc.setEmail("[email protected]"); public String getName() { return name; } acc.setAmount(100000f); public void setName(String name) { this.name = name; } // getting values through getter methods public String getEmail() { return email; } System.out.println( public void setEmail(String email) acc.getAcc_no() + " " + acc.getName() + " " { this.email = email; } + acc.getEmail() + " " + acc.getAmount()); public float getAmount() { return amount; } } public void setAmount(float amount) } { this.amount = amount; } } String Manipulations String In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string. For example: char[] ch={‘h',’e',’l',’l',’o',’w','o',’r',’l',’d'}; String s=new String(ch); is same as: String s=“helloworld"; Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc. Method Description Return Type charAt() Returns the character at the specified index (position) char compareTo() Compares two strings lexicographically int compareToIgnoreCase() Compares two strings lexicographically, ignoring case differences int concat() Appends a string to the end of another string String contains() Checks whether a string contains a sequence of characters boolean contentEquals() Checks whether a string contains the exact same sequence of characters boolean of the specified CharSequence or StringBuffer copyValueOf() Returns a String that represents the characters of the character array String equals() Compares two strings. Returns true if the strings are equal, and false if boolean not equalsIgnoreCase() Compares two strings, ignoring case considerations boolean getChars() Copies characters from a string to an array of chars void indexOf() Returns the position of the first found occurrence of specified characters int in a string toString() Returns the value of a String object String toUpperCase() Converts a string to upper case letters String trim() Removes whitespace from both ends of a string String Method Description Return Type isEmpty() Checks whether a string is empty or not boolean lastIndexOf() Returns the position of the last found occurrence of specified characters in a int string length() Returns the length of a specified string int matches() Searches a string for a match against a regular expression, and returns the boolean matches replace() Searches a string for a specified value, and returns a new string where the String specified values are replaced replaceFirst() Replaces the first occurrence of a substring that matches the given regular String expression with the given replacement replaceAll() Replaces each substring of this string that matches the given regular String expression with the given replacement split() Splits a string into an array of substrings String[] startsWith() Checks whether a string starts with specified characters boolean toCharArray() Converts this string to a new character array char[] toLowerCase() Converts a string to lower case letters String valueOf() Returns the string representation of the specified value String String Buffer Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer class in Java is the same as String class except it is mutable i.e. it can be changed. There are several advantages of using StringBuffer over regular String objects in Java: 1. Mutable: StringBuffer objects are mutable, which means that you can modify the contents of the object after it has been created. In contrast, String objects are immutable, which means that you cannot change the contents of a String once it has been created. 2. Efficient: Because StringBuffer objects are mutable, they are more efficient than creating new String objects each time you need to modify a string. This is especially true if you need to modify a string multiple times, as each modification to a String object creates a new object and discards the old one. 3. Thread-safe: StringBuffer objects are thread-safe, which means multiple threads cannot access it simultaneously. In contrast, String objects are not thread-safe, which means that you need to use synchronization if you want to access a String object from multiple threads. Important Constructors of StringBuffer Class 1. StringBuffer(): creates an empty string buffer with an initial capacity of 16. 2. StringBuffer(String str): creates a string buffer with the specified string. 3. StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length. Methods of StringBuffer class Methods Action Performed append() Used to add text at the end of the existing text. length() The length of a StringBuffer can be found by the length( ) method capacity() the total allocated capacity can be found by the capacity( ) method charAt() This method returns the char value in this sequence at the specified index. delete() Deletes a sequence of characters from the invoking object deleteCharAt() Deletes the character at the index specified by loc ensureCapacity() Ensures capacity is at least equals to the given minimum. insert() Inserts text at the specified index position length() Returns length of the string reverse() Reverse the characters within a StringBuffer object replace() Replace one set of characters with another set inside a StringBuffer object String Tokenizer The java.util.StringTokenizer class allows you to break a String into tokens. It is simple way to break a String. It is a legacy class of Java. It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc. like StreamTokenizer class. We will discuss about the StreamTokenizer class in I/O chapter. In the StringTokenizer class, the delimiters can be provided at the time of creation or one by one to the tokens. There are 3 constructors defined in the StringTokenizer class. Constructor Description StringTokenizer(String str) It creates StringTokenizer with specified string. StringTokenizer(String str, String It creates StringTokenizer with specified string delim) and delimiter. StringTokenizer(String str, String It creates StringTokenizer with specified string, delim, boolean returnValue) delimiter and returnValue. If return value is true, delimiter characters are considered to be tokens. If it is false, delimiter characters serve to separate tokens. The six useful methods of the StringTokenizer class are as follows: Methods Description boolean hasMoreTokens() It checks if there is more tokens available. String nextToken() It returns the next token from the StringTokenizer object. String nextToken(String It returns the next token based on delim) the delimiter. boolean hasMoreElements() It is the same as hasMoreTokens() method. Object nextElement() It is the same as nextToken() but its return type is Object. int countTokens() It returns the total number of tokens. Example: import java.util.StringTokenizer; public class Simple{ public static void main(String args[]){ StringTokenizer st = new StringTokenizer("my name is khan"," "); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } } Output: my name is khan Packages Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for: 1. Preventing naming conflicts. For example there can be two classes with name Employee in two packages, college.staff.cse.Employee and college.staff.ee.Employee 2. Making searching/locating and usage of classes, interfaces, enumerations and annotations easier 3. Providing controlled access: protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only. 4. Packages can be considered as data encapsulation (or data-hiding). All we need to do is put related classes into packages. After that, we can simply write an import class from existing packages and use it in our program. A package is a container of a group of related classes where some of the classes are accessible are exposed and others are kept for internal purpose. We can reuse existing classes from the packages as many time as we need it in our program. Introduction to predefined packages These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are: 1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported. 2) java.io: Contains classed for supporting input / output operations. 3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations. 4) java.applet: Contains classes for creating Applets. 5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc). 6) java.net: Contain classes for supporting networking operations. User Defined Packages These are the packages that are defined by the Now we can use the MyClass class in our program. user. First we create a directory myPackage (name should be same as the name of the package). Then create the MyClass inside the directory with the first statement being the package names. import myPackage.MyClass; package myPackage; public class PrintName public class MyClass { { public static void main(String args[]) public void getNames(String s) { { System.out.println(s); String name = “Hello"; } }

Use Quizgecko on...
Browser
Browser