




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































programs.
and retrieve data from database.
Java is the most popular object-oriented programming language. Java has many advanced features known as Java Buzz Words. The java team has listed the following terms as java buzz words.
Simple Secure Portable Object-oriented Robust Multithreaded Architecture-neutral Interpreted High performance Distributed Dynamic
Java was designed to be easy for the professional programmer to learn and use effectively. Assuming that you have some programming experience, you will not find Java hard to master. If you already understand the basic concepts of object-oriented programming, learning Java will be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will require very little effort. Because Java inherits the C/C++ syntax and many of the object- oriented features of C++, most programmers have little trouble learning Java.
Although influenced by its predecessors, Java was not designed to be source-code compatible with any other language. This allowed the Java team the freedom to design with a blank slate. One outcome of this was a clean, usable, pragmatic approach to objects. Borrowing Liberally from many seminal object-software environments of the last few decades, Java manages to strike a balance between the purist’s “everything is an object” paradigm and the pragmatist’s “stay out of my way” model. The object model in Java is simple and easy to extend, while primitive types, such as integers, are kept as high-performance non objects.
The multiplatform environment of the Web places extraordinary demands on a program, because the program must execute reliably in a variety of systems. Thus, the ability to create robust programs was given a high priority in the design of Java. To gain reliability, Java restricts you in a few key areas to force you to find your mistakes early in program development. At the same time, Java frees you from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks
Java is designed for the distributed environment of the Internet because it handles TCP/IP protocols. In fact, accessing a resource using a URL is not much different from accessing a file. Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke methods across a network.
Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the Java environment, in which small fragments of bytecode may be dynamically updated on a running system.
Object-Oriented Programming System (OOPs) is a programming concept that works on the principles of abstraction, encapsulation, inheritance, and polymorphism. It allows users to create objects they want and create methods to handle those objects. The basic concept of OOPs is to create objects, re-use them throughout the program, and manipulate these objects to get results.
Concepts of object oriented programming
a) Class b) Object c) Inheritance d) Polymorphism e) Abstraction f) Encapsulation
a) Class
A class is a blue print or template for a real-time object. The class consists of variables and methods. The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. In most classes, the instance variables are acted upon and accessed by the methods defined for that class. Class doesn’t occupy memory
The class is one of the Basic concepts of OOPs which is a group of similar entities. It is only a logical component and not the physical entity. Let’s understand this one of the OOPs Concepts with example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties (data) can be price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking etc.
b) Object
An object is instance of a class. Any number of objects can be created for a class.
It is a real world entity. Object occupies the memory. An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An Object is one of the Java OOPs concepts which contains both the data and the function, which operates on the data. For example – chair, bike, marker, pen, table, car, etc.
c) Inheritance
Inheritance is one of the Basic Concepts of OOPs in which one object acquires the properties and behaviors of the parent object. It’s creating a parent-child relationship between two classes. It offers robust and natural mechanism for organizing and structure of any software.
d) Polymorphism
Polymorphism refers to one of the OOPs concepts in Java which is the ability of a variable, object or function to take on multiple forms. For example, in English, the verb run has a different meaning if you use it with a laptop, a foot race, and business. Here, we understand the meaning of run based on the other words used along with it. The same also applied to Polymorphism.
e) Abstraction
Abstraction is one of the OOP Concepts in Java which is an act of representing essential features without including background details. It is a technique of creating a new data type that is suited for a specific application. Lets understand this one of the OOPs Concepts with example, while driving a car, you do not have to be concerned with its internal working. Here you just need to concern about parts like steering wheel, Gears, accelerator, etc.
f) Encapsulation
Encapsulation is one of the best Java OOPs concepts of wrapping the data and code. In this OOPs concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. For example – in school, a student cannot exist without a class.
The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc.
There are various situations where wrapper classes are essential.
Change the value in Method : Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
Synchronization: Java synchronization works with objects in Multithreading.
Output 10 10
Unboxing
The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.
//Java program to convert object into primitives
//Unboxing example of Integer to int
public class WrapperExample { public static void main(String args[]) { //Converting Integer to int Integer a=new Integer(3); int i=a.intValue();//converting Integer to int explicitly int j=a;//unboxing, now compiler will write a.intValue() internally System.out.println(a+" "+i+" "+j); } } Output:
3 3 3
There are two types of data types
a) Primitive data types b) Reference data types(Non Primitive)
a) Primitive data types
Primitive data types consists of Integer,Float,Character,Boolean data types
Integer data types are
i) byte ii) short iii) int iv) long
Floating point types
i) float ii) double
Character type
i) char
Boolean Type i) Boolean
Integer data types:
byte:
The smallest integer type is byte. This is a signed 8-bit type that has a range from – 128 to 127. Variables of type byte are especially useful when you’re working with a stream of data from a network or file. They are also useful when you’re working with raw binary data that may not be directly compatible with Java’s other built-in types. Byte variables are declared by use of the byte keyword.
For example, the following declaration represents two byte variables called b and c:
byte b, c;
short:
short is a signed 16-bit type. It has a range from – 32,768 to 32,767. It is probably the least- used Java type. Here are examples of short variable declarations:
short s;
short t;
int:
The most commonly used integer type is int. It is a signed 32-bit type that has a range from
Here are some example float variable declarations: float hightemp, lowtemp;
double
Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values.
Char:
When you need to maintain accuracy over Characters In Java, the data type used to store characters is char. char in Java is not the same as char in C or C++. In C/C++, char is 8 bits wide. This is not the case in Java. Instead, Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic,Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits. Thus, inJava char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars. The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255.
Since Java is designed to allow programs to be written for worldwide use, it makes sense that it would use Unicode to represent characters. Of course, the use of Unicode is somewhat inefficient for languages such as English, German, Spanish, or French, whose characters can easily be contained within 8 bits.
Boolean
Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators, as in the case of a < b. boolean is also the type required by the conditional expressions that govern the control statements such as if and for. Here is a program that demonstrates the boolean type: // Demonstrate boolean values. class BoolTest { public static void main(String args[]) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); // a boolean value can control the if statement if(b) System.out.println("This is executed.");
b = false; if(b) System.out.println("This is not executed."); // outcome of a relational operator is a boolean value System.out.println("10 > 9 is " + (10 > 9)); } } The output generated by this program is shown here: b is false b is true This is executed. 10 9 is true b) Reference data types (Non Primitive)
i) class ii) interface iii) String
(Hint: Single inheritance, multilevel inheritance, Hierarchical inheritance)
Ans: Deriving one class into another class is called inheritance. extends is the keyword used to inherit the properties of a class.
Sub Class/Child Class/Derived class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Super Class/Parent Class/Base class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java:
a) Single Inheritance b) Multiple Inheritance c) Multi-Level Inheritance d) Hierarchical Inheritance e) Hybrid Inheritance
In java programming, multiple and hybrid inheritances are implemented through the concept of interface.
Single inheritance:
Single Inheritance is the simple inheritance of all, When a class extends another class(Only one class) then we call it as Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class.
When a class inherits another class, it is known as a single inheritance.
Multilevel inheritance
When there is a chain of inheritance, it is known as multilevel inheritance.
In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. ClassB inherits the property of ClassA and again ClassB act as a parent for ClassC. In Short ClassA parent for ClassB and ClassB parent for ClassC.
Class First is derived into Second and class Second is derived into Third
class First
{
void show() { System.out.println("C PROGRAMMING"); }
}
class Second extends First
{
void display()
System.out.println("PHP PROGRAMMING"); }
}
class Third extends Second
{
public void sample() { System.out.println("JAVA PROGRAMMING"); }
}
// Drived class
public class Multilevel
{
public static void main(String[] args) { Third g = new Third(); g.show(); g.display(); g.sample(); }
}
Hierarchical Inheritance in Java
In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example ClassA will be inherited by ClassB, ClassC and ClassD. ClassA will be acting as a parent class for ClassB, ClassC and ClassD.
public class ClassA { public void dispA() {
d.dispA(); } } Output : disp() method of ClassB disp() method of ClassA disp() method of ClassC disp() method of ClassA disp() method of ClassD disp() method of ClassA
Hybrid Inheritance in Java
Hybrid inheritance is a type of inheritance that combines single inheritance and multiple inheritances. As multiple inheritances is not supported by Java, hybrid inheritance can also be achieved through interfaces only.
Ans: Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both. Overloading is related to compile-time (or static) polymorphism..
class Shapes
{
double a; double r; double l; void area() { a = (Math.PI)rr; System.out.println("Area of circle="+a); } void area(double l,double b)
a = lb; System.out.println("Area of Rect="+a); } double area(double l) { a = ll; return a; }
}
class Overload
{
public static void main(String args[]) { Shapes s = new Shapes(); double k; s.r = 5.6; s.area(); s.area(7,8); k = s.area(10); System.out.println("Area of Square="+k); }
}
Output
Area of circle=98.
Area of Rect=56.
Area of Square=100.
Here same method area() is overloaded.
Constructor Overloading will have more than one constructor with different parameters which can be used for different operations. Compilers will differentiate these constructors by taking into account the number of parameters.
class Sample
{
Sample()
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
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:
a) Compile time Polymorphism---overloading b) Runtime Polymorphism---overriding a) Compile-time polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by method overloading or operator overloading. But Java doesn’t support the Operator Overloading.
Method Overloading: When there are multiple methods with same name but different parameters then these methods are said to be overloaded. Methods can be overloaded by change in number of arguments or/and change in type of arguments.
Example: By using different types of arguments // Java program for Method overloading class Shapes { double a; double r; double l; void area() { a = (Math.PI)rr; System.out.println("Area of circle="+a); }
void area(double l,double b) { a = l*b; System.out.println("Area of Rect="+a); }
double area(double l) { a = l*l; return a; } }
class Overload { public static void main(String args[]) { Shapes s = new Shapes(); double k; s.r = 5.6; s.area(); s.area(7,8); k = s.area(10); System.out.println("Area of Square="+k); } }
b) Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
Method overriding, on the other hand, occurs when a derived class has a definition for one of the method of the base class. That base class method is said to be overridden.
Example:
class Test { void show()// method overriding { System.out.println("JAVA PROGRAMING"); } } class Sample extends Test {
void show() { super.show(); System.out.println("PYTHON PROGRAMING"); } }
class Overriding { public static void main(String args[]) { Sample s = new Sample();//object creation s.show(); } }