Class: A class in used in order to group variables (data) and functions (methods) in a single unit.
Using
class java supports the concept of encapsulation. Object: An object may be defined as an instance of a
class.
Steps for creating object of a class:
Create a reference variable of a class
Create an object of the class using the ‘new’ keyword and assign it to the reference variable of the
class.
eg: class T{ …. .... } //creating an object T ob = new T();
Tokens: They are the smallest unit that makes up the programming language. They consists of the
following: 1. Identifier 2. Variables 3. Constants 4. Keywords 5. Separators 6. Operators.
Identifiers: Name given to various programming elements.
Rules to name an identifier: It should consists of letter and digits It cannot start with a digit should
not contain blank space in between. Should not contain any special character except _. It may start
with _. It should not be a keyword. Identifiers written in upper case and lower case are different.
Types of constructor: 1. Default Constructor: It doesn’t take any argument. 2. Parameterised
Constructor: Constructor which takes argument.
Why a non parameterised constructor is called a default constructor? → When we does not provide
any constructor to a program, the compiler by default provides a non parameterised constructor.
Why in java a character occupies 2byte of memory space? → As java has the support for unicode
character set. In unicode character set all the characters are present which are found in all human
known language, that is why in order to accomodate those characters java requires 2 bytes of memory
space for that character.
Constructor overloading: In java it is possible to have multiple constructors within the same class, as
long as they differ in their number or types of arguments. This mechanism is called constructor
overloading and the constructors are called as overloaded constructors.
Command Line Argument: Some times in java we require to pass values to java programs through
terminal. This can be achieved using command line arguments. When values are passed through
terminal the values are accepted in the form of ‘String’. The value are taken in the form of array of
string.
Why the main method in java is declared as public static and void? → The main method is declared as
public, so that it can be accessible by any outside code here the outside code is the JVM. The main
method is declared as static so that the JVM can call the main method without having to create an
object of the class within which main resides. public static void main and static public void main both are
same.
Why java is called platform independent language? → When we compile a java program, it creates a
highly optimizable code called ‘byte code’. This Byte code can run on any computer with different
hardware and software architecture. This is the reason why java is platform independent.
Why java is called compiled as well as interpreted language? → In java both compiler and interpreter
are required in order to compile and run a java program. The java compiler converts the java source
code to the corresponding byte code and the java interpreter(JVM) converts the byte code to the
executable code.
Programming Language: A programming language is a language using which we can provide instructions
to a computer. Software: It is defined as the inter-related programs.
Program: It is a set of instructions. Instructions: Commands which we give to a computer in order to
achieve a particular task using a specific language.
Object: It is defined as an representation of a real world entity with unique identity, embedded
properties and the ability to interact with other objects.
Object oriented programming: Program dealing with objects are called oop. Procedural Oriented
Programming: In a procedural oriented programming the programs are split into multiple tasks.
A programming language is called oop if and only if it has the support for the following features:
Encapsulation: It is the mechanism by which data(variable) and methods(functions) are grouped
together in a single unit. It is done using a concept called class.
Data Abstraction and data hiding: It means hiding the working complexity of the data from the
outside world. Encapsulation is the mechanism and its outcome is data abstraction.
Polymorphism: The term ‘poly’ means many and the term ‘morph’ means forms. It is the mechanism
by which the same interface can be used in order to achieve multiple tasks. In a oop, the polymorphism
is achieved using the concepts of ‘Method Overloading’ and ‘Method Overwriting’ etc.
Inheritance: It is the mechanism by which one class acquires the feature of another class. Through
inheritance an oop supports the concept of ‘Code Reusability’.
Types of Object Oriented Programming Languages:
Partial object oriented: A language is called partial object oriented if it supports all the 4 features of
oops but supports any 1/2/3 features partially. eg: C++ Note: In c++ the encapsulation feature is partially
supported because the main() is written outside the class.
Fully object oriented: A programming language is called fully object oriented if it supports all the 4
features fully. eg: Java
Complete object oriented: A programming language is called a complete object oriented if and only if
it has the full support of all the 4 features of oops and it doesn’t have any support for primitive
datatypes. As java has the support for primitive datatype, hence it is not a complete object oriented. eg:
Smalltalk.
Abstract Class An abstract class is a class which has abstract keyword applied to it. An abstract class
may contain normal methods as well as abstract methods. 💡 An abstract method is a method which has
abstract keyword applied to it and it does not have any body.
Dynamic method dispatch: A parent class reference variable can refer to any of the child class objects
but the vise versa is not true. Dynamic method dispatch is a mechanism by which it calls two overridden
methods which are resolved during run time.
In java polymorphism can be classified into two types:
1. Compile time polymorphism: It is checked at the time of compilation. It is also referred to as early
binding. e.g- Method overloading and constructor overloading 2. Run time polymorphism: It is
implemented by the concept called dynamic method dispatch. It is also referred as late binding.
A package: may be defined as a collection of classes, interfaces, other packages. It is used to support
the concept of code reusability as well as provides security to the code.
Various ways of creating a thread.
A thread in java can be created in 2 ways:
1. By Implementing runnable Interface
2. By Extending the Thread class
Steps for creating a thread by Implementing Runnable Interface
1. Create a class which Implementing runnable Interface
2. Create an object of the Thread class.
3. Using the Thread class object call the start() method.
4. The start method automatically calls the run method. It is inside the run method where we
have to write the code for another Thread
Steps for creating a Thread class by Extending the thread
5. create a class which extends the Thread class.
6. call the start() method.
7. the start() method calls the run() method. It is inside the run method where
we have to write the code for another thread.
8. Which way is better among this two ways? and why?→ If we create a thread by
extending the Thread class then that class will not be able to extend any other class if
needed but if we create a thread by Implementing runnable interface then that class still
has the option to extend any other class if needed that is why we can say creating a thread
implementing runnable interface is the best.
In java, a string is implemented by using two classes:
String class
String Buffer class
String Class
A string class is an immutable class i.e. any changes made to a particular string object creates a
fresh new string object without affecting the original string object. This string class has some
inbuilt method using which we can manipulate a given string.
There are multiple methods in the String class:
len()This method is used to determine the total number of character in a string.
concat() It is used to concatenate two strings.
endsWith() The endsWith method is used in order to check whether a particular string ends with
a given string or not. The method returns a boolean value.
startsWith() The startsWith method is used in order to check whether a particular string starts
with a given string or not. The method returns a boolean value.
equals() The equals() is used to compare two strings and return boolean value.
String Buffer Class: It is a mutable class that means any changes made to a current string object
is affected in the original string.
A string buffer also contains multiple method:
append()
It is used to concatenate two strings.
In java, applet is a class using which java implements the concepts of graphics. The main
difference of an applet program as co pared to other java programs is that an applet program does
not contain main() and that is why applet is a dependant program.
As applet programs does not contains main(), it follows a life cycle. An applet life cycle consists
of the following methods
public void init()
public void start()
public void stop()
public void destroy()
public void paint(Graphics)
init()
This method is used to load the applet in the computer’s memory. This method is called only
once in the entire life cycle of the applet.
start()
The start method is called just after the init(). This start() is responsible for viewing the applet in
the front of the screen. The start() can be called multiple times.
stop()
The stop() is responsible for moving the applet at the background of the screen. This method can
also be called multiple times.
paint()
The paint() is responsible for displaying anything in the applet. It can also be called multiple
times. The paint() contains a class called Graphics using which we can draw anything.
destroy()
This is responsible for unloading the applet from the memory, once its operation is over. This
method is only called once.