Introduction: • Java is a high-level programming language, where one can write computer applications like games, web pages, graphics ...
Introduction:
• Java is a high-level programming language, where one can write computer applications like games, web pages, graphics and data processing … etc.
• Java language is called as an Object-Oriented Programming language
Basic Concept of OOP (Object-Oriented Programming):
There are some basic concepts of object oriented programming as follows:
1. Classes
2. Object
3. Data abstraction
4. Data encapsulation
5. Inheritance
6. Polymorphism
Classes
• A class is a set of objects with similar attributes, common behavior, and common link to other objects. In other words, a class is a collection of objects of similar type.
• In below example, we will create an object Language belonging to the class India. The corresponding objects develop their distinctiveness from the difference in their attribute value and relationships to other objects.
Object
• Objects characterize literally everything
• Each object holds data and some methods to operate on the data. Example: Representation of object ―LAPTOP
Example Object: Laptop
|
Brand Name
Data : OS
Cost
|
Functions : Start()
Debug()
Stop()
|
How to define Classes, Methods and Constructors
Data Abstraction:• The mechanism to create new data types that are useful for an application. In other words it allows the creation of user defined data types.
• Classes use the concepts of data abstraction.
Example:
Data Encapsulation:• Data Encapsulation means wrapping of data and functions into a single unit (i.e. class). It is one of the most useful property of class. The data is not easy to get to the outside world and only those functions which are enclosed in the class can access it.
• It is also called Data Hiding
Press1. Deposit
2. Withdraw
3. Determine Balance
4. Transfer Funds
5. exit
Abstract Class Declaration:• An abstract class is a class that is declared as abstract. It may or may not include abstract methods.
2. Withdraw
3. Determine Balance
4. Transfer Funds
5. exit
Abstract Class Declaration:• An abstract class is a class that is declared as abstract. It may or may not include abstract methods.
Example:
public abstract class Television
{
// declare fields
// declare methods
void SwitchOn();
}
• Inheritance allows the important feature of OOP that is reusability.
• In the example given for class, Telangana, and A.P are part of the class Telugu speaking states and again the Telugu speaking states car is the part of the class Language. The Language is again part of the class India. India is the parent class.
General Declaration of Inheritance class using Keyword extends:
{
Varables;
Methods;
…..
}
Example
class Telangana extends Telugu-Speaking-States
{
Varables;
Methods;
…
}
class AP extends Telugu-Speaking-States
{
Varables;
Methods;
….. }
Polymorphism: • (Poly means ―many and morph means ―form). Polymorphism means the ability to take more than one form.
Features of Java Program:
• 1. Compiled and Interpreted
• 2. Platform Independent and portable
• 3. Object- oriented
• 4. Robust and secure
• 5. Distributed
• 6. Familiar, simple and small
• 7. Multithreaded and Interactive
• 8. High performance
• 9. Dynamic and Extensible
Java compilation Process
class HelloWorld {
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}
- The file name be HelloWorld.java equivalent to the class name containing the main method.
- Java is case sensitive.
- public static void main (String [] args ) shows where the program will start running.
Data types in Java :
Data Types in Java are of two types:
- primitive data type
- non-pimitive data type
- classes
- arrays
- interface
Primitive data types :
Tokens in Java:
1. Literals 2. Identifiers 3. Operators 4. Separators
Literals in Java are constant values. They can be either Integer or Floating point or Character or String or Boolean literals
Identifiers are programmer named tokens. They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program.
1. Literals 2. Identifiers 3. Operators 4. Separators
Literals in Java are constant values. They can be either Integer or Floating point or Character or String or Boolean literals
Identifiers are programmer named tokens. They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program.
Separators:
- ( )
Adjusts precedence in arithmetic expressions;
Surrounds cast types and delimits test expressions in flow control statements
- { }
- [ ] declares array types and dereferences array values
- <> Declares Generics and Collections
- ; terminates statements
- , Separates successive identifiers in variable declarations;
- . Selects a field or method from an object; separates package names from sub-package and class names
- : Used after loop labels
1. Arithmetic operators 2. Logical operators 3. Relational operators 4. Assignment operators 5. Conditional operators 6. Increment and decrement operators 7. Bit wise operators
Keywords:
In Java language 50 words are reserved for specific purpose. They cannot be used in naming variables, classes and methods. Following are some example keywords.
Double, abstract, char, catch, Boolean, default, finally, do, implements, if, private, public, and static …etc.
Control Structure in Java:
In java program, control structure is can divide in three parts:
- Selection statement like – (1) if statement and (2) switch statement
- Iteration statement like - (1) for loop (2) while loop (3) do-while loop
- Jumps in statement like
continue keyword use for continuing the loop.