Introduction to Java

Concept of Object Oriented Programming
Procedure-Oriented vs Object Oriented Programming
Procedure-Oriented: - Procedural language is a type of computer programming language that specifies a series of well-structured steps and procedures within its programming context to compose a program. It contains a systematic order of statements, functions and commands to complete a computational task or program.
When program become larger, it is divided into function & each function has clearly defined purpose. Dividing the program into functions & module is one of the cornerstones of structured programming.
Characteristics of Procedural oriented programming: -
* It focuses on process rather than data.
* It takes a problem as a sequence of things to be done such as reading, calculating and printing. Hence, a number of functions are written to solve a problem.
* A program is divided into a number of functions and each function has clearly defined purpose.
* Most of the functions share global data.
* Data moves openly around the system from function to function.
Drawback of Procedural oriented programming (structured programming):-
* It emphasis on doing things. Data is given a second class status even through data is the reason for the existence of the program.
* Since every function has complete access to the global variables, the new programmer can corrupt the data accidentally by creating function. Similarly, if new data is to be added, all the function needed to be modified to access the data.
* It is often difficult to design because the components function and data structure do not model the real world.
Procedural language is one of the most common programming languages in use with notable languages such as C/C++, Java, ColdFusion and PASCAL.
Object Oriented Programming: - A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inheritcharacteristics from other objects.
One of the principal advantages of object-oriented programming techniques over procedural programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer can simply create a new object that inherits many of its featuresfrom existing objects. This makes object-oriented programs easier to modify.
The concepts and rules used in object-oriented programming provide these important benefits: -
* The concept of a data class makes it possible to define subclasses of data objects that share some or all of the main class characteristics. Called inheritance, this property of OOP forces a more thorough data analysis, reduces development time, and ensures more accurate coding.
* Since a class defines only the data it needs to be concerned with, when an instance of that class (an object) is run, the code will not be able to accidentally access other program data. This characteristic of data hiding provides greater system security and avoids unintended data corruption.
* The definition of a class is reuseable not only by the program for which it is initially created but also by other object-oriented programs (and, for this reason, can be more easily distributed for use in networks).
* The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.
Simula was the first object-oriented programming language. Java, Python, C++, Visual Basic .NET and Ruby are the most popular OOP languages today.
Basics of OOPS
1. Abstraction: - abstraction is the process of separating ideas from specific instances of those ideas at work. Computational structures are defined by their meanings (semantics), while hiding away the details of how they work. Abstraction tries to factor out details from a common pattern so that programmers can work close to the level of human thought, leaving out details which matter in practice, but are exigent to the problem being solved. For example, a system can have several abstraction layers whereby different meanings and amounts of detail are exposed to the programmer; low-level abstraction layers expose details of the computer hardware where the program runs, while high-level layers deal with the business logic of the program.
Abstraction captures only those details about an object that are relevant to the current perspective; in both computing and in mathematics, numbers are concepts in programming languages. Numbers can be represented in myriad ways in hardware and software, but, irrespective of how this is done, numerical operations will obey identical rules.
Abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures.
* Control abstraction involves the use of subprograms and related concepts control flows.
* Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype
2. Inheritance : - Inheritance is a mechanism in which one object acquires all the properties and behaviours of parent object.
The idea behind inheritance is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you reuse (or inherit) methods and fields, and you add new methods and fields to adapt your new class to new situations.
We use Inheitance for :-
* For Method Overriding (Runtime Polymorphism)
* For Code Reusability
Inheritance in java can be best understood in terms of Parent and Child relationship, also known as Super Class (Parent) and Sub class(Child) in Java language.
Inheritance defines is-a relationship between a Super class and its Sub class extends and implemetns keywords are used to descsribe inheritance in Java.

3. Encapsulation: - Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
* The fields of a class can be made read-only or write-only.
* A class can have total control over what is stored in its fields.
* The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.
4. Classes,subclasses, and super Classes: -
Classes: - A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.
A class the basic building block of an object-oriented language such as Java is a template that describes the data and behavior associated with instances of that class. When you instantiate a class you create an object that looks and feels like other instances of the same class. The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods. Methods are similar to the functions or procedures in procedural languages such as C.
Java classes contain fields and methods. A field is like a C++ data member, and a method is like a C++ member function. Each field and method has an access level: -
* private: - accessible only in this class
* (package): - accessible only in this package
* protected: - accessible only in this package and in all subclasses of this class
* public: - accessible everywhere this class is available
Sub Class: - A subclass is a class that extends another class. The subclass inherits the state and behaviors of the class it extends. It is also known as derived class, child class. You can make a class (called a subclass, derived class, or child class) a specialization of another class (called a superclass, base class, or parent class). A subclass inherits the methods and instance data of its superclasses, and is related to its superclasses by an is-a relationship. For example, if subclass P inherits from superclass Q, and subclass Q inherits from superclass S, then an instance of P is an instance of Q and also (by transitivity) an instance of S. An instance of P inherits the methods and data of Q and S.
* Reuse of code. Through inheritance, a subclass can reuse methods that already exist in a superclass.
* Specialization. In a subclass you can add new methods to handle cases that the superclass does not handle. You can also add new data items that the superclass does not need.
* Change in action. A subclass can override a method that it inherits from a superclass by defining a method of the same signature as that in the superclass. When you override a method, you might make only a few minor changes or completely change what the method does.
Super Classes: - A superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors. It is also known as base class, parent class.Through inheritance a new class inherits some existing behavior and states of another class which is called as the superclass. This promotes the concept of code reusability. Creation of a superclass saves work as the already existing class allows the new class to inherit all the properties of the old general class. Superclass allows various subclassed objects to share certain things in common.
* To introduce better data analysis
* Reduction in development time
* Less coding efforts
* Better performance
5. Polymorphism and Overloading: -
Polymorphism: - is an important Object oriented concept and widely used in Java and other programming language. Polymorphism in java is supported along with other concept like Abstraction, Encapsulation and Inheritance.Polymorphism word comes from ancient Greek where poly means many so polymorphic are something which can take many form.Java Polymorphism we see what is Polymorphism in Java , How Polymorphism is implemented in Java e.g method overloading and overriding, why should we use Polymorphism and how can we take advantage of polymorphism while writing code in Java. Along the way we will also see a real world example of using Polymorphism in Java in OOPS programming concepts
Polymorphism is an Oops concept which advice use of common interface instead of concrete implementation while writing code. When we program for interface our code is capable of handling any new requirement or enhancement arise in near future due to new implementation of our common interface. If we don't use common interface and rely on concrete implementation, we always need to change and duplicate most of our code to support new implementation. Its not only Java but other object oriented language like C++ also supports polymorphism and it comes as fundamental along with other OOPS concepts like Encapsulation , Abstraction and Inheritance.
Overloading: -Overloading is the ability to define more than one method with the same name in a class. The compiler is able to distinguish between the methods because of their method signatures.
Example: - here are nine different ways the print method of the System.out object can be used:
* print.(Object obj)
* print.(String s)
* print.(boolean b)
* print.(char c)
* print.(char[] s)
* print.(double d)
* print.(float f)
* print.(int i)
* print.(long l)
Each time a different print method is being called because the parameter type being passed is different. It's useful because the print method will need to vary how it works depending on whether it has to deal with a String or integer or boolean.
Overloading refers to the ability to use a single identifier to define multiple methods of a class that differ in their input and output parameters. Overloaded methods are generally used when they conceptually execute the same task but with a slightly different set of parameters.
Overloading is a concept used to avoid redundant code where the same method name is used multiple times but with a different set of parameters. The actual method that gets called during runtime is resolved at compile time, thus avoiding runtime errors. Overloading provides code clarity, eliminates complexity, and enhances runtime performance.


Free Web Hosting