Java OOPs Concept
Java OOPs Concept
What is JRE?
class Program
d:>cd java10am
d:>java10aam>javac Program.java
d:>java10am>java Program
class Program1
class ProgramOne
System.out.printf("%d %d\n",10,20);
JAVA
System.out.println(10+" "+20);
System.out.println(10+"+"+20+"="+(10+20));
class Program1
int n1=10,n2=20;
System.out.printf("n1=%d n2=%d",n1,n2);
System.out.println("n1="+n1+" n2="+n2);
Method
Syntax
return_type methodname()
statements;
}
JAVA
Class
Syntax
class classname
data members;
methods;
Object
Syntax
Variable
Rules
1. Static method
2. Instance method.
1 Static method
When we want to call logic one time then we go for static. When we want to
initialise static variable then we should create static method.
A method which we will call w.r.t class name is known as static method.
It is preceded with static keyword.
One static method can call another static method directly if it is in same class.
One static method can call another static method of another class by using
class name.
One static method can call another non static method w.r.t object of the class.
2 Instance Method
When we need to call logic repeatedly then we should use instance method
A method which we will call w.r.t object is known as instance method.
Syntax
return_type methodname()
statements;
//Example program
class MainDemo
{
JAVA
System.out.println("this is demo method");
void display()
void show()
}
JAVA
// Example program 2
class MetDemo
void display()
void fun()
display();
MetDemo.show();
show();
m.fun();
}
JAVA
// wajp to create multiple method in a class.
class Program1
void display()
void show()
void fun()
class Program2
p.display();
p.show();
p.fun();
}
JAVA
Method overloading
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 a mixture of both.
1. No of parameter
2. Order of parameter
3. Type of parameter
class Program1
void display()
void display(int a)
System.out.println("a="+a);
void display(float a)
System.out.println("a="+a);
System.out.println("a="+a+" b="+b);
{
JAVA
System.out.println("a="+a+" b="+b);
class Program2
p.display();
p.display(10);
p.display(10,10);
p.display(10.5f);
p.display(10.5f,10);
Types of variable
Instance variable
Instance variable are object level variable which will create memory for every
object saprately.
instance variable are not preceded with static keywords
Static variable
Static variables are preceded with static keyword and it will create only one
copy of a memory for a program
It can call with respect to class name or object name
Static variables are called as class level variable and instance variable are
called object level variable.
JAVA
//wajp for instance variable and static variable
class Employee
String name;
this.name = name;
this.mobile = mobile;
void getdisplay()
System.out.println("name:-"+name);
System.out.println("depy:-"+Employee.dept);
System.out.println("mobile:-"+mobile);
e.getdisplay();
e1.getdisplay();
}
JAVA
Constructor
Rules
Types of Constructors
1. Default constructor
2. Parameterized constructor
Default constructor
When we want set same default values for all object then we will go with
default constructor.
import java.util.Scanner;
class Demo1
int a,b;
Demo1()
a=100;
b=200;
void readNumber()
a=s.nextInt();
JAVA
b=s.nextInt();
void display()
System.out.println(a+" "+b);
d.display();
Parameterised constructor
import java.util.Scanner;
class Demo1
int a,b;
a=x;
b=y;
void display()
System.out.println(a+" "+b);
d.display();
d1.display();
d2.display();
}
JAVA
Mixed constructor or constructor overloading
When we have multiple constructors with different data types then it is known
as mixed constructor or constructor overloading.
class Demo1
int a,b;
a=1;
b=1;
a=x;
b=y;
a=1;
b=y;
JAVA
System.out.println("1 parameter constructor"+a+" "+b);
"This keyword"
Whenever class level variables and method level variables are same then use
"this " keyword with class level variables.
class Demo1 {
int a,b;
Demo1(int a,int b)
this.a=a;
this.b=b;
void display()
System.out.println(a+" "+b);
}
JAVA
public class ConDemo
d1.display();
Inheritance
It is the mechanism in java by which one class is allowed to inherit the
features (fields and methods) of another class.
Important terminology:
Super Class: The class whose features are inherited is known as a super class (or a
base class or a parent class).
Sub Class: The class that inherits the other class is known as a sub class (or a
derived class, extended class, or child class). The subclass can add its
own fields and methods in addition to the super class fields and
methods.
Types of inheritance
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
JAVA
Single inheritance
If we have one parent and one child class is known as single inheritance.
Multilevel inheritance
When we have one parent class, one child class and multiple intermediate
classes then it is known as multilevel inheritance.
Multiple inheritance
When two class details are coming into one class is known as multiple
inheritance and this does not support in java
Note: it is not possible because if two classes are having same method then there
will be problem in child class.
Hierarchical inheritance
when one class giving features to multiple classes is known hierarchical inheritance
Hybrid inheritance
combination of any two inheritance is known as hybrid inheritance
class Iphone1
void display()
System.out.println("16 inches");
void camera()
}
JAVA
class Iphone2 extends Iphone1
void selfiecamera()
System.out.println("selfiecamera available");
i1.display();
i1.camera();
i2.display();
i2.camera();
i2.selfiecamera();
class Iphone1
void display()
System.out.println("16 inches");
JAVA
}
void camera()
void selfiecamera()
System.out.println("selfiecamera available");
void facerec()
System.out.println("face recog");
i2.display();
JAVA
i2.camera();
i2.selfiecamera();
i2.facerec();
class charger
void charging()
System.out.println("charging ");
void iphone()
System.out.println("iphone details");
void sam()
System.out.println("Samsung details");
}
JAVA
}
i.charging();
i.iphone();
s.charging();
s.sam();
IS-A RELATIONSHIP
When we take one class properties into another class by using inheritance is known as IS-A
Relationship.
HAS-A RELATIONSHIP
When we take one class properties into another class by creating object of one class into the
method of another class is known as HAS-A Relationship.
class Iphone1
void display()
System.out.println("16 inches");
}
JAVA
void camera()
class Iphone2
void selfiecamera()
System.out.println("selfiecamera available");
i1.display();
i1.camera();
i2.selfiecamera();
}
JAVA
Method overriding
when parent class and child has same method then it is known as method overriding.
class Iphone1 {
void display() {
System.out.println("16 inches");
void camera()
void camera()
System.out.println("camera 20px");
i2.display();
i2.camera();
}
JAVA
super in java
variable level
When parent class and child class variable are same, and from child class
you want to call parent class variable then we will used super with parent class
variable.
class Iphone
int a=10000;
int a=20000;
void camera()
System.out.println("Iphone a="+super.a);
System.out.println("Iphone2 a="+a);
i.camera();
}
JAVA
method level
When parent class and child class has same method, and from child class
you want to call parent class method then we will use super with parent class
method.
class Iphone
void display()
void camera()
System.out.println("16mp camera");
void camera()
System.out.println("32mp camera");
super.camera();
{
JAVA
public static void main(String[] args)
i.display();
i.camera();
Final in java
final supports at three levels
1.variable level
2.method level
3.class level
1.variable level
class Iphone
void camera()
System.out.println("a="+a);
}
JAVA
public class Demo
i.camera();
If we make any method as final then we can't override that method only you can use
that method in child classes.
class Iphone
System.out.println("Iphone Camera");
i.camera();
}
JAVA
3.class level
If you make any class as final then we can't inherite that class properties into child
classes.
void camera()
System.out.println("Iphone Camera");
i.camera();
class Employee1
void getdisplay()
System.out.println("A:-"+a);
}
JAVA
public class EmpDemo1
e.getdisplay();
POLYMORPHISM
The process of representing one form in multiple ways is known as polymorphism.
In java original method represent one form and overrided method represent multiple
ways.
DYNAMIC BINDING
The process of binding appropriate version of derived classes which are inherited
from base class with base class object is known as dynamic binding.
class BaseClass
void sum()
System.out.println("this is sum");
void sum()
int a=10;
int b=20;
System.out.println(a+b);
void sum()
float a=10;
float b=20;
System.out.println(a+b);
}
JAVA
public class LambdaDemo {
b.sum();
b=new Child1();
b.sum();
b=new Child2();
b.sum();
Abstraction
Abstract Method
If a method does not have body is known as abstract method abstract method
preceded with abstract keyword.
1. Concrete class
2. Abstract class
Concrete class
example
class Demo1 {
void display()
System.out.println("Hello");
}
JAVA
Abstract Class
If a class has atleast one abstract method then it is known as abstract class.
If class is abstract you can't create object of that class
If your inheriting abstract class feature then you must override abstract
method otherwise your class will become abstract class.
example
void display()
int a=10;
int b=20;
System.out.println(a+b);
void show()
b.display();
b.show();
when we write empty body method in class then it is known as null body method.
example
class Demo1
void display()
}
JAVA
Interface
Interface is a collection of abstract method.
We can't create the object of interface.
To use interface details we will use implements keyword.
in interface writing abstract to method is optional even if you don't write your method
will be public abstract void display();
Syntax
interface interface_name
abstract method
interface I1
void display();
interface I1
interface I2 extends I1
{
JAVA
public void display()
class Imain
i1.display();
i1.show();
}
JAVA
interface to interface
we use extends keyword to take one interface details into another interface.
example
interface I1
interface I2 extends I1
default method
when we want to make any method optional in interface then we will make
that method as default method in interface.
Once we make any method as default then overriding that method is optional.
example
interface I1
void display();
}
JAVA
//wajp for default method
interface I1
void display();
class IMain
i1.display();
}
JAVA
Lambda Expressions
Lambda Expressions were added in Java 1.8.
Lambda expressions are similar to methods, but they do not need a name and
they can be implemented right in the body of a method.
Syntax
interface I1
void display();
i.display();
i.display();
}
JAVA
//java program for lambda expression with parameter
interface I1 {
I1 i=(a,b)->System.out.println("sum="+(a+b));
i.sum(10,20);
i.sum(100, 200);
interface I1
I1 i= (a)->a*a;
System.out.println(i.sqrt(5));
}
JAVA
GENERIC CLASS
A Generic class simply means that the items or functions in that class can
be generalized with the parameter(example T) to specify that we can add any type
as a parameter in place of T like Integer, Character, String, Double or any other
user-defined type.
T obj;
Test(T obj)
this.obj = obj;
public T getObject()
return this.obj;
class Main
System.out.println(iObj.getObject());
JAVA
Test <String> sObj =new Test<String>("prsoftwares");
System.out.println(sObj.getObject());
System.out.println(sObj1.getObject());
class University
void show()
System.out.println("University Information");
class College
void department()
{
JAVA
System.out.println("Department Information");
u.show();
cg.department();
Exception Handling
Errors are two types
syntax
try
finally
Try block:
3. This is the block in which we write the block of statements which are to be
monitored by JVM at run time i.e., try block must contain those statements which
causes problems at run time.
5. If any exception is taking place in try block execution will be terminated and the
rest of the statements in try block will not be executed at all and the control will
go to catch b lock.
6. For every try block we must have at least one catch block. It is highly
recommended to write ‘n’ number of catch’s for ‘n’ number of problematic
statements.
JAVA
Catch block:
1. This is used for providing user friendly messages by catching system error
messages.
2. In the catch we must declare an object of the appropriate execution class and it
will be internally referenced JVM whenever the appropriate situation taking place.
3. If we write ‘n’ number of catch’s as a part of JAVA program then only one catch
will be executing at any point.
4. After executing appropriate catch block even if we use return statement in the
catch block the control never goes to try block.
Finally block:
1) This is the block which is executing compulsory whether the exception is taking
place or not.
2) This block contains same statements which releases the resources which are
obtained in try block (resources are opening files, opening databases, etc.).
import java.util.Scanner;
import java.util.InputMismatchException;
try
int a,b;
b=s.nextInt();
System.out.println(a/b);
catch(InputMismatchException e)
catch(ArithmeticException e) {
finally
throws keyword: This is the keyword which gives an indication to the calling function
to keep the called function under try and catch blocks.
Syntax:
Learning User about JAVA is nothing but learning about various packages. By
default one
Predefined package is imported for each and every JAVA program and whose
name is java.lang.*.
Whenever we develop any java program, it may contain many number of user
defined classes and user defined interfaces. If we are not using any package name
to place user defined classes and interfaces, JVM will assume its own package
called NONAME package.
In java we have two types of packages they are predefined or built-in or core
packages and user or secondary or custom defined packages.
Syntax:
package pack1[.pack2[.pack3……[.packn]…..]];
Here, package is a keyword which is used for creating user defined packages,
pack1 represents upper package and pack2 to packn represents sub packages.
For example:
Rules:-
package prsoftwares.demo;
import java.util.Scanner;
int a,b;
a=s.nextInt();
b=s.nextInt();
System.out.println(a+b);
javac -d . PackDemo1.java
import prsoftwares.demo.PackDemo1;
class PackagDemo {
p.sum();
}
JAVA
//wajp to write interface in package
package prsoftwares.demo;
public interface I1
void display();
import prsoftwares.demo.I1;
class MainI1
i.display();
}
JAVA
ACCESS MODIFIER
In order to use the data from one package to another package or within the
package, we have to use the concept of access modifiers.
1. Private : Private data accessible only within same class outside class
we can't access.
2. Defaults : This data can be access within same package. Outside the
package is not possible.
3. Protected : Protected data is accessible within the same file outside
package it is possible only if inherit parent class properties. But
independent class can't call.
4. Public : Public data is accessible anywhere in the package or outside
the package.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Access specifiers make us to understand how to access the data within the
package (class to class, interface to interface and interfaces to class) and across the
package (class to class, interface to interface and interfaces to class).
In other words access specifiers represent the visibility of data or accessibility of data.
int d=40;
------------------------------
package prsoftwares.demo;
import prsoftwares.demo.MyDemo1;
------------------------------------
JAVA
import prsoftwares.demo.MyDemo1;
System.out.println(b+" "+c);
class MainDemo
m1.fun();
System.out.println(m.b);
Encapsulation
Encapsulation in Java is a mechanism of wrapping the data (variables) and
code acting on the data (methods) together as a single unit. In encapsulation, the
variables of a class will be hidden from other classes, and can be accessed only
through the methods of their current class. Therefore, it is also known as data
hiding.
JAVA
public class EncapTest
{
private String name;
private String idNum;
private int age;
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
public String getIdNum()
{
return idNum;
}
It can be used to get property value based on the property key. The
Properties class provides methods to get data from the properties file and store data
into the properties file. Moreover, it can be used to get the properties of a system.
import java.io.FileReader;
import java.util.Properties;
class TestMain
try
p.load(mr);
System.out.println(p.getProperty("drivername"));
System.out.println(p.getProperty("username"));
System.out.println(p.getProperty("password"));
JAVA
System.out.println(p.getProperty("dbname"));
catch(Exception e)
System.out.println("this is exception");
Instance methods:
StringBuffer class:
1. StringBuffer ()
2. StringBuffer (String)
COLLECTION
Collection
Whenever we want to store different types of data into the single unit then we
will use collection framework concept.
It is dynamic in nature.
We can add or remove data at any point of time for that collection has given
predefined methods.
It has sorting and searching techniques inbuilt.
1D COLLECTION
2D COLLECTION
COLLECTION FRAMEWORK:
Collection framework is divided into two categories. They are new collection
framework and legacy (old) collection framework.
Interfaces Classes
---------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
System.out.println(ll);
ll.add(10);
ll.add(10.5f);
ll.add("Megha");
ll.add('a');
Iterator itr=ll.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
JAVA
//wajp for adding only integer values to Array list
import java.util.ArrayList;
import java.util.Iterator;//Iterator=interface
import java.util.Scanner;
System.out.println(ll);
ll.add(10);
ll.add(80);
ll.add(50);
ll.add(90);
Iterator itr=ll.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
JAVA
HashSet V/S TreeSet
HashSet TreeSet
The operations like insertion,deletion and The operations like deletion and
modifications takes and modifications modifications takes more amount of time.
take very less time.
Creating HashSet is nothing but creating Creating TreeSet is nothing but creating
an object of HashSet () class. an object of TreeSet () class.
States of a thread:
When we write any multithreading applications, there exist ‘n’ numbers of threads.
All the threads will undergo different types of states. In JAVA for a thread we have
five states. They are new, ready, running, waiting and halted or dead state.
New state: It is one in which the thread about to enter into main memory.
Ready state: It is one in which the thread is entered into memory space allocated
and it is waiting for CPU for executing.
Running state: A state is said to be a running state if and only if the thread is under
the controls of CPU.
Waiting state: It is one in which the thread is waiting because of the following factors:
a) For the repeating CPU burst time of the thread (CPU burst time is an
amount of the required by the thread by the CPU).
b) Make the thread to sleep for sleep for some specified amount of time.
Halted state: It is one in which the thread has completed its total execution.
JAVA
As long as the thread is in new and halted states whose execution status is false
whereas when the thread is in ready, running and waiting states that execution
states is true
for(int i=0;i<10;i++)
System.out.println("this is display");
t.start();//thread 0
}
JAVA
//wajp to check current thread name:
System.out.println("Thread name="+Thread.currentThread().getName());
for(int i=0;i<10;i++)
System.out.println("this is display");
t.start();//thread 0
System.out.println("Thread name="+Thread.currentThread().getName());
}
JAVA
Creating a thread:
In order to create a thread in JAVA we have two ways. They are by using
class Test {
System.out.println("Thread name="+t.getName());
System.out.println("Thread name="+t1.getName());
System.out.println("Thread name="+t2.getName());
System.out.println("Thread name="+t3.getName());
Instance methods:
The above two methods are used for setting the name of the thread and getting the
name from the thread respectively.
JAVA
// wajp to set and get thread name:
class Test
System.out.println("Thread name="+t.getName());
t.setName("Prsoftwares");
System.out.println("Thread name="+t.getName());
try
for(int i=1;i<=10;i++)
catch(InterruptedException i)
System.out.println(i);
JAVA
}
t.start();
try{
for(int i=1;i<=10;i++)
catch(InterruptedException i)
System.out.println(i);
}
JAVA
}
t.start();
Daemon Thread :
try
for(int i=1;i<=10;i++)
catch(InterruptedException i)
System.out.println(i);
t.setDaemon(true);
t.start();
for(int i=1;i<=5;i++)
try {
Thread.sleep(500);
catch(InterruptedException ie)
System.out.println(ie);