CS3391 – OBJECT ORIENTED
PROGRAMMING
TEXT BOOKS:
1. Herbert Schildt, “Java The complete
reference”, 8th Edition, McGraw Hill
Education, 2011.
2. Cay S. Horstmann, Gary cornell, “Core
Java Volume –I Fundamentals”, 9th Edition,
Prentice Hall, 2013
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
UNIT -1
Introduction to OOP’s and
JAVA Fundamentals
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA INTRODUCTION
James Gosling initiated Java language project in
June 1991
Sun released the first public implementation as
Java 1.0 in 1995. It promised Write Once, Run
Anywhere (WORA)
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
OBJECT ORIENTED PROGRAMMING
 Object-oriented programming (OOP) is a computer
programming model that organizes software design
around data, or objects, rather than functions and logic.
 An object can be defined as a data field that has unique
attributes and behavior
Mr. K.Loganathan , AP/IT, Mailam Engineering College
FEATURES OF JAVA
Mr. K.Loganathan , AP/IT, Mailam Engineering College
1. Object-oriented - It means we organize our
software as a combination of different types
of objects that incorporates both data and
behavior.
2. Simple - Java is very easy to learn, and its syntax
is simple, clean and easy to understand.
3. Platform Independent -Java code can be run
on multiple platforms, for example,
Windows, Linux, Sun Solaris, Mac/OS, etc.
4. Robust - Robust simply means strong. Java is
robust because:
It uses strong memory management.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
5. Portable - It facilitates you to carry the Java
byte code to any platform.
6. Architecture-neutral - No implementation
dependent features, for example, the size of
primitive types is fixed
7. Dynamic - It supports dynamic loading of
classes. It means classes are loaded on
demand. It also supports functions from
its native languages, i.e., C and C++.
8. Interpreted - Java uses interpreter to convert
byte code.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
9. High-performance - Java is faster than other
traditional interpreted programming languages
because Java byte code is "close" to native
code
10. Multi-threaded - A thread is like a separate
program, executing concurrently. We can write
Java programs that deal with many tasks at
once by defining multiple thread
11. Distributed- This feature of Java makes us able
to access files by calling the methods from any
machine on the internet.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA NAMING CONVENTIONS
CamelCase in java naming conventions
 If the name is combined with two words, the second
word will start with uppercase letter always such as
actionPerformed(), firstName, ActionEvent,
ActionListener, etc.
 Class
 It should start with the uppercase letter.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 Method
 It should start with lowercase letter.
 It should be a verb such as main(), print(), println().
 If the name contains multiple words, start it with a
lowercase letter followed by an uppercase letter
such as actionPerformed().
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 Package
 It should be a lowercase letter such as java, lang.
 If the name contains multiple words, it should be
separated by dots (.) such as java.util, java.lang.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 Constant
 It should be in uppercase letters such as RED,
YELLOW.
 If the name contains multiple words, it should be
separated by an underscore(_) such as
MAX_PRIORITY.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CREATING CLASS
 Syntax to declare a Class
class <ClassName>
{
AccessModifiers:
data member;
method;
}
Example:
class Employee
{
int eid; // data member (or instance variable)
String ename; // data member (or instance variable)
eid=101;
ename="Hitesh";
}
AccesModifiers
1. private
2. Public
3. Protected
4. default
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CREATING AN OBJECT
 Creating an object
Syntax
Classname object= new Classname()
Example:
Employee e= new Employee()
Mr. K.Loganathan , AP/IT, Mailam Engineering College
FIRST JAVA PROGRAM
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
Output:
Hello Java
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 Public – It is an access modifier that allows an
object or a variable or a class or a method to be
used anywhere in java programming.
 Static – It can execute or call a method or a
variable without using an object.
 Void - It could return any type of value.
 String - It is a class in JVM, since all the code that
we give will be taken in a string format we use
string there and args is the name of the array.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
STRUCTURE OF JAVA PROGRAM
Mr. K.Loganathan , AP/IT, Mailam Engineering College
EXAMPLE
Mr. K.Loganathan , AP/IT, Mailam Engineering College
PRIVATE:
 Private members of class in not accessible anywhere in program these are
only accessible within the class.
class Hello
{
private int a=20;
private void show()
{
System.out.println("Hello java");
}
}
public class Demo
{
public static void main(String args[])
{
Hello obj=new Hello();
System.out.println(obj.a); //Compile Time Error, you can't access private data
obj.show(); //Compile Time Error, you can't access private methods
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
PUBLIC:
Public members of any class are accessible anywhere in the program in the same
class and outside of class, within the same package and outside of the package.
class Hello
{
public int a=20;
public void show()
{
System.out.println("Hello java");
}
}
public class Demo
{
public static void main(String args[])
{
Hello obj=new Hello();
System.out.println(obj.a);
obj.show();
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 At What happens at Compilation time?
 What happens at runtime?
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Compile and Run Java Program
Mr. K.Loganathan , AP/IT, Mailam Engineering College
STEPS FOR COMPILING AND EXECUTING THE JAVA PROGRAM
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA VARIABLES
 Variables are nothing but reserved memory locations to store
values.
 Syntax
[data_type] [variable_name] = [variable_value];
E.g:
int i = 10; //Variable of int type
String str = "howtodoinjava.com"; //Variable of string type
Object obj = new Object(); //Variable of object type
int[] scores = [1,2,3,4,5,6,7,8,9]; / //Variable of int type
Mr. K.Loganathan , AP/IT, Mailam Engineering College
TYPES OF VARIABLES
1. Instance Variable
 A variable declared inside the class but outside the
body of the method, is called instance variable.
 It is not declared as static.
2. Static Variable
 A variable which is declared as static is called static
variable. It cannot be local.
 Memory allocation for static variable happens only
once when the class is loaded in the memory.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
3. Local variable
 A variable declared inside the body of the method is
called local variable.
Example
class A
{
int data=50; //instance variable
static int m=100; //static variable
void method()
{
int n=90; //local variable
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA - BASIC DATA TYPES
There are two data types available in Java −
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA COMMENTS
 Comments can be used to explain Java code,
and to make it more readable
 // - Single-line comments start with two
forward slashes.
 /* */ - Multi-line comments.
Any text between /* and */ will be ignored by
Java.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA OPERATORS
 Operators are used to perform operations on variables and
values.
 The value is called an operand, while the operation (to be
performed between the two operands) is defined by an operator.
Java divides the operators into the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
Mr. K.Loganathan , AP/IT, Mailam Engineering College
1. Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Example:
public class MyClass
{
public static void main(String[] args)
{
int x = 5;
int y = 3;
System.out.println(x + y);
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
2. Assignment Operators
Assignment operators are used to assign values to variables.
public class MyClass
{
public static void main(String[] args)
{
int x = 5;
x += 3;
System.out.println(x);
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
3. COMPARISON OPERATORS
 Comparison operators are used to compare two
values.
public class MyClass {
public static void main(String[] args) { int x = 5;
int y = 3;
System.out.println(x == y);}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
4. LOGICAL OPERATORS
 Logical operators are used to determine the logic between
variables or values.
public class MyClass {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 && x < 10);
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
5. BITWISE OPERATORS
Bitwise operator works on bits and performs bit-
by-bit operation
if a = 60 and b = 13; now in binary format they
will be as follows −
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CONTROL STATEMENTS
 The Java if statement is used to test the
condition. It checks Boolean
condition: true or false.
 There are various types of if statement in java.
 if statement
 if-else statement
 if-else-if ladder
 nested if statement
Mr. K.Loganathan , AP/IT, Mailam Engineering College
SIMPLE IF STATEMENT
 It executes the if block if condition is true.
Syntax:
if(condition)
{
True Statemetns;
}
Example:
Class Great
{
Public static void main(String args[])
{
int a = 10, b = 5;
if(a>b)
{
System.out.println(“a is greater than b”);
}
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
IF-ELSE STATEMENT
 It executes the if block if condition is true otherwise else block is executed
Syntax:
if(condition)
{
//code if condition is true
}else
{
//code if condition is false
}
Example:
public class Example
{
public static void main(String[] args)
{
int a=13; int b-20;
if(a>b){
System.out.println(“A is big number");
}else{
System.out.println(“B is big number");
}
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
IF-ELSEIF LADDER
 The if-else-if ladder statement executes one condition from multiple
statements.
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
else
{
//code to be executed if all the conditions are false
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
EXAMPLE
public class Example
{
public static void main(String[] args)
{
int a=13; int b=20, int c=30;
if(a>b)&&(a>c)
{
System.out.println(“A is big number");
}
elseif(b>c)
{
System.out.println(“B is big number");
}
else
{
System.out.println(“Cis big number");
}
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
LOOPING STATEMENT
 Looping statement are the statements execute
one or more statement repeatedly several
number of times.
While Loop:
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 do-while
A do-while loop is similar to a while loop, except
that a do-while loop is execute at least one time
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 For Loop
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 For Loop Example
Mr. K.Loganathan , AP/IT, Mailam Engineering College
OOP'S CONCEPT IN JAVA
 Object-Oriented Programming is a methodology
or paradigm to design a program using classes
and objects.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
OBJECT
Object is a instance of class, or real time entites
object has state and behaviors.
 An Object in java has three characteristics:
 State - Represents data (value) of an object.
 Behavior - Represents the behavior (functionality) of
an object such as deposit, withdraw etc
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CLASS
 Class is a blue print which is containing only
list of variables and method and no memory is
allocated for them. A class is a group of objects
that has common properties.
 A class in java contains:
 Data Member
 Method
 Constructor
 Block
 Class and Interface
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
ENCAPSULATION
 Encapsulation is a process of wrapping of data
and methods in a single unit is called
encapsulation.
Advantage
 To secure the data from other
methods, when we make a data private then
these data only use within the class, but these
data not accessible outside the class.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
ABSTRACTION
 Hiding of data is known as data abstraction.
 Abstraction is the concept of exposing only the
required essential characteristics and behavior
with respect to a context.
Real Life Example of Abstraction in Java
Mr. K.Loganathan , AP/IT, Mailam Engineering College
INHERITANCE
 The process of obtaining the data members and
methods from one class to another class is
known as inheritance.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
POLYMORPHISM
 The word "poly" means many and "morphs" means
forms. So polymorphism means many forms.
 The process of representing one form in multiple
forms is known as Polymorphism.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
PROTECTED
 Protected are also called derived level access
modifiers.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
ARRAY
 An array is defined as the collection of similar type of
data items stored at contiguous memory locations.
 Advantage
 One variable can store multiple value
 Code Optimization
 Random access
 Disadvantage
Once we declare array there is no chance to increase
and decrease the size of an array according to our
requirement,
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 Types of Array
1. Single Dimensional Array 2.Two Dimensional
Array
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CONSTRUCTOR
 Constructor is a special member method which
will be called automatically whenever object is
created.
 The purpose of constructor is to initialize an
object called object initialization.
Advantages of constructors in Java
 A constructor eliminates placing the default values.
 A constructor eliminates calling the normal or
ordinary method implicitly.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
HOW CONSTRUCTOR ELIMINATE DEFAULT VALUES
 Constructor are mainly used for eliminate default
values by user defined values, whenever we create an
object of any class then its allocate memory for all the
data members and initialize there default values.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
PROPERTIES OF A CONSTRUCTOR
 Constructor name must be similar to name of the class.
 Constructor should not return any value even void also.
 A Java constructor cannot be abstract, static, final, and
synchronized
 Constructors will not be inherited from one class to another
class.
 Constructor should not be private provided an object of one
class is created in another class
 The access specifier of the constructor may or may not be
private.
 If the access specifier of the constructor is private then
an object of corresponding class can be created in the
context of the same class but not in the context of
some other classes.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CONSTRUCTOR EXAMPLE
Mr. K.Loganathan , AP/IT, Mailam Engineering College
TYPES OF CONSTRUCTORS
Based on creating objects in Java constructor are
classified in two types. They are
 Default or no argument Constructor
 Parameterized constructor.
1. Default or no argument Constructor
 A constructor is said to be default constructor if
and only if it never take any parameters.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
2. PARAMETERIZED CONSTRUCTOR
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
PACKAGE
 A package is a collection of similar types of classes,
interfaces and sub-packages
 The purpose of package concept is to provide common
classes and interfaces for any program separately.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
TYPE OF PACKAGE
Package are classified into two type which are
given below.
1. Predefined or built-in package
2. User defined package
1. Predefined or built-in package
built-in packages such as java, lang, awt,
javax, swing, net, io, util, sql etc.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
USER DEFINED PACKAGE
 Rules to create user defined package
 package statement should be the first statement of any
package program.
 Choose an appropriate class name or interface name and
whose modifier must be public.
 Any package program can contain only one public class or
only one public interface but it can contain any number of
normal classes.
 Package program should not contain any main class (that
means it should not contain any main())
 The modifier of method of class or interface which is present
in the package must be public (This rule is optional in case of
interface because interface methods by default public)
 Every package program should be save either with public
class name or public Interface name
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
COMPILE PACKAGE PROGRAMS
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA - DOCUMENTATION COMMENTS
Mr. K.Loganathan , AP/IT, Mailam Engineering College
UNIT-2
INHERITANCE
&
INTERFACES
Mr. K.Loganathan , AP/IT, Mailam Engineering College
INHERITANCE
 Inheritance is a mechanism in which one object acquires all the
properties and behaviors of a parent object
Terms used in Inheritance
 Class: A class is a group of objects which have common properties. It
is a template or blueprint from which objects are created.
 Sub Class/Child 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: Super class is the class from where a
subclass inherits the features. It is also called a base class or a
parent class.
 Reusability: As the name specifies, reusability is a mechanism which
facilitates you to reuse the fields and methods of the existing class
when you create a new class. You can use the same fields and
methods already defined in the previous class.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
SYNTAX
class Subclass-name extends Superclass-name
{
//methods and fields
}
 extends is the keyword used to inherit the
properties of a existing class(Base Class)
Mr. K.Loganathan , AP/IT, Mailam Engineering College
TYPES OF INHERITANCE
1.Single Inheritance
In single inheritance there exists single
base class and single derived class
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
2. Multilevel Inheritance
In Multilevel inheritances there exists single base
class, single derived class and multiple
intermediate base classes.
Single base class +
single derived class +
multiple intermediate base classes.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
3.Hierarchical Inheritance
If more than one class is inherited from the
base class, it's known as hierarchical
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
4. Hybrid inheritance
Hybrid inheritance is a combination of
multiple inheritance and multilevel inheritance.
A class is derived from two classes as in
multiple inheritance. However, one of the parent
classes is not a base class. It is a derived class
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 Multiple Inheritance
Multiple Inheritance is a feature of object
oriented concept, where a class
can inherit properties of more than one parent
class. The problem occurs when there exist
methods with same signature in both the super
classes and subclass.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
FUNCTION OVERLOADING
 If a class has multiple methods having same
name but different in parameters, it is known
as Method Overloading.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
 Three ways to overload a method
1. Number of parameters.
2. Data type of parameters.
3. Sequence of Data type of parameters
Mr. K.Loganathan , AP/IT, Mailam Engineering College
class DisplayOverloading
{
int add(int a, int b)
{
int sum = a+b;
return sum;
}
int add(int a, int b, int c)
{
int sum = a+b+c;
return sum;
}
}
class JavaExample
{
public static void main(String args[])
{
DisplayOverloading obj = new
DisplayOverloading();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20,30));
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
ABSTRACT CLASS
 Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
 A class that is declared with abstract keyword,
is known as abstract class.
 An abstract class is one which is containing
some defined method and some undefined
method.
 In java programming undefined methods are
known as un-Implemented, or abstract method.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
abstract class A
{
abstract void callme();
void callmetoo()
{
System.out.println("This is a
concrete method.");
}
}
class B extends A
{
void callme()
{
System.out.println("B's
implementation of callme.");
}
}
class AbstractDemo
{
public static void main(String args[])
{
B b = new B();
b.callme();
b.callmetoo();
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
abstract class Vachile
{
abstract void speed(); // abstract method
}
class Bike extends Vachile
{
void speed()
{
System.out.println("Speed limit is 40 km/hr..");
}
public static void main(String args[])
{
Vachile obj = new Bike(); //indirect object creation
obj.speed();
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CONSTRUCTOR IN ABSTRACT CLASS
abstract class abst
{
abst()
{
System.out.println("I am in Constructor");
}
abstract void run();
void display()
{
System.out.println("I am in display function");
}
}
class sample extends abst
{
void run()
{
System.out.println("I am in Abstract method");
}
}
class TestAbstract
{
public static void main(String
args[])
{
abst obj = new sample();
obj.run();
obj.display();
}
}
OutPut:
I am in Constructor
I am in Abstract method
I am in display function
Mr. K.Loganathan , AP/IT, Mailam Engineering College
INTERFACES
 These methods will not have implementation
part hence all the methods declared in an
interface are abstract methods
 We can implement multiple inheritance using
interface.
 An interface method can’t be final or static or
private or native or protected.
 An interface can be extended from another
interface.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
SYNTAX
interface <interfacename>
{
// final variables declaration
//methods declaration
}
The general syntax for a class that implements an interface
is given below.
class <classname> implements <interface name>
{
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
interface Area // Interface defined
{
final static float pi =3.14F;
float compute (float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return (x*y);
}
}
class Circle implements Area
{
public float compute (float x, float y)
{
return (pi*x*x);
}
}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Circle cir =new Circle();
System.out.println("Area of
Rectangle+rect.compute(10,20);
System.out.println("Area of Circle “+
cir.compute(10,0);
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
interface Developer
{
void disp();
}
interface Manager
{
void show();
}
class Employee implements Developer, Manager
{
public void disp()
{ System.out.println("Hello Good Morning"); }
public void show()
{ System.out.println("How are you ?"); }
public static void main(String args[])
{ Employee obj=new Employee();
obj.disp();
obj.show();
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
RELATIONSHIP BETWEEN CLASS AND INTERFACE
Mr. K.Loganathan , AP/IT, Mailam Engineering College
Mr. K.Loganathan , AP/IT, Mailam Engineering College
FINAL CLASS
//example of abstract class that have method body
final class Employee
{
final int salary=10000;
}
class Developer
{
void show()
{
System.out.println("Hello Good Morning");
}
}
class TestAbstraction2
{
public static void main(String args[])
{
//Developer obj=new Developer();
Employee o=new Employee();
//o.salary=100;
System.out.println(o.salary);
//Developer obj=new Developer();
//obj.show();
Mr. K.Loganathan , AP/IT, Mailam Engineering College
ARRAYLIST CLASS
 Java ArrayList class uses a dynamic array for storing the
elements. It inherits AbstractList class and implements List
interface.
The important points about Java ArrayList class are:
 ArrayList class is not Synchronized.
 ArrayList class elements can be access randomly.
 In the ArrayList value will be stored in the same order as
inserted.
 ArrayList class uses a dynamic array for storing the
elements.It extends AbstractList class and implements List
interface.
 ArrayList class can contain duplicate elements.
 ArrayList allows random access because array works at the
index basis.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CONSTRUCTORS OF JAVA ARRAYLIST
 Syntax:
ArrayList<String> al=new ArrayList<String>();
Mr. K.Loganathan , AP/IT, Mailam Engineering College
WAYS TO ITERATE THE ELEMENTS OF THE COLLECTION IN JAVA
class arrayli
{
public static void main(String[] args) throws IOException
{
int n = 5; // size of ArrayList
ArrayList<Integer> arrli = new ArrayList<Integer>(n);
for (int i=1; i<=n; i++)
arrli.add(i);
System.out.println(arrli);
arrli.remove(3);
System.out.println(arrli);
for (int i=0; i<arrli.size(); i++)
System.out.print(arrli.get(i)+" ");
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CLONING
 Object cloning refers to creation of exact copy
of an object.
 It creates a new instance of the class of current
object and initializes all its fields.
Mr. K.Loganathan , AP/IT, Mailam Engineering College
CREATING A COPY USING CLONE() METHOD
 Every class that implements clone() should call
super.clone() to obtain the cloned object reference.
 The class must also implement java.lang.Cloneable
interface whose object clone we want to create
otherwise it will throw
CloneNotSupportedException when clone method
is called on that class’s object.
 Syntax: protected Object clone() throws
CloneNotSupportedException
Mr. K.Loganathan , AP/IT, Mailam Engineering College
import java.util.ArrayList;
class Test
{
int x, y;
}
class Test2 implements Cloneable
{
int a;
int b;
Test c = new Test();
public Object clone() throws
CloneNotSupportedException
{
return super.clone();
}
}
public class Main
{
public static void main(String args[]) throws
CloneNotSupportedException
{
Test2 t1 = new Test2();
t1.a = 10;
t1.b = 20;
t1.c.x = 30;
Test2 t2 = (Test2)t1.clone();
t2.a = 100;
t2.c.x = 300;
t1(shallow copy)
System.out.println(t1.a + " " + t1.b + " "
+
t1.c.x + " " + t1.c.y);
System.out.println(t2.a + " " + t2.b + " "
+
t2.c.x + " " + t2.c.y);
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
class Student18 implements Cloneable
{
int rollno;
String name;
Student18(int rollno,String name)
{
this.rollno=rollno;
this.name=name;
}
public Object clone()throws CloneNotSupportedException
{
return super.clone();
}
public static void main(String args[])
{
try
{
Student18 s1=new Student18(101,"amit");
Student18 s2=(Student18)s1.clone();
System.out.println(s1.rollno+"
"+s1.name);
System.out.println(s2.rollno+"
"+s2.name);
}catch(CloneNotSupportedException c){}
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
JAVA INNER CLASSES
 Java inner class or nested class is a class which is declared inside the class or
interface
Syntax of Inner class
class Java_Outer_class
{
//code
class Java_Inner_class
{
//code
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
TYPES OF INNER CLASSES
1) Nested Inner class
2) Method Local inner classes
3) Anonymous inner classes
4) Static nested classes
1) Nested Inner class can access any private
instance variable of outer class
Mr. K.Loganathan , AP/IT, Mailam Engineering College
class Outer
{
// Simple nested inner class
class Inner
{
public void show()
{
System.out.println("In a nested class method");
}
}
}
class Main
{
public static void main(String[] args)
{
Outer.Inner in = new Outer().new Inner();
Mr. K.Loganathan , AP/IT, Mailam Engineering College
2) METHOD LOCAL INNER CLASSES
 Inner class can be declared within a method of
an outer class
class Outer
{
void outerMethod() {
int x = 98;
System.out.println("inside outerMethod");
class Inner {
void innerMethod() {
System.out.println("x= "+x);
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class MethodLocalVariableDemo {
public static void main(String[] args) {
Outer x=new Outer();
x.outerMethod();
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
3. STATIC NESTED CLASSES
 Static nested classes are not technically an
inner class.
class Outer {
private static void outerMethod() {
System.out.println("inside outerMethod");
}
// A static inner class
static class Inner {
public static void main(String[] args) {
System.out.println("inside inner class Method");
outerMethod();
}
}
}
Mr. K.Loganathan , AP/IT, Mailam Engineering College
4. ANONYMOUS INNER CLASSES
 Anonymous inner classes are declared without any
name at all
class Demo {
void show() {
System.out.println("i am in show method of super class");
}
}
class Flavor1Demo {
// An anonymous class with Demo as base class
static Demo d = new Demo() {
void show() {
super.show();
System.out.println("i am in Flavor1Demo class");
}
};
public static void main(String[] args){
d.show();
}
}

CS3391 - Object Oriented Programming AU.Reg-2021

  • 1.
    CS3391 – OBJECTORIENTED PROGRAMMING TEXT BOOKS: 1. Herbert Schildt, “Java The complete reference”, 8th Edition, McGraw Hill Education, 2011. 2. Cay S. Horstmann, Gary cornell, “Core Java Volume –I Fundamentals”, 9th Edition, Prentice Hall, 2013 Mr. K.Loganathan , AP/IT, Mailam Engineering College
  • 2.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College UNIT -1 Introduction to OOP’s and JAVA Fundamentals Mr. K.Loganathan , AP/IT, Mailam Engineering College
  • 3.
    JAVA INTRODUCTION James Goslinginitiated Java language project in June 1991 Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once, Run Anywhere (WORA) Mr. K.Loganathan , AP/IT, Mailam Engineering College
  • 4.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College OBJECT ORIENTED PROGRAMMING  Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic.  An object can be defined as a data field that has unique attributes and behavior
  • 5.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College FEATURES OF JAVA
  • 6.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 1. Object-oriented - It means we organize our software as a combination of different types of objects that incorporates both data and behavior. 2. Simple - Java is very easy to learn, and its syntax is simple, clean and easy to understand. 3. Platform Independent -Java code can be run on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. 4. Robust - Robust simply means strong. Java is robust because: It uses strong memory management.
  • 7.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 5. Portable - It facilitates you to carry the Java byte code to any platform. 6. Architecture-neutral - No implementation dependent features, for example, the size of primitive types is fixed 7. Dynamic - It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++. 8. Interpreted - Java uses interpreter to convert byte code.
  • 8.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 9. High-performance - Java is faster than other traditional interpreted programming languages because Java byte code is "close" to native code 10. Multi-threaded - A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple thread 11. Distributed- This feature of Java makes us able to access files by calling the methods from any machine on the internet.
  • 9.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College JAVA NAMING CONVENTIONS CamelCase in java naming conventions  If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.  Class  It should start with the uppercase letter.
  • 10.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  Method  It should start with lowercase letter.  It should be a verb such as main(), print(), println().  If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed().
  • 11.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  Package  It should be a lowercase letter such as java, lang.  If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang.
  • 12.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  Constant  It should be in uppercase letters such as RED, YELLOW.  If the name contains multiple words, it should be separated by an underscore(_) such as MAX_PRIORITY.
  • 13.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CREATING CLASS  Syntax to declare a Class class <ClassName> { AccessModifiers: data member; method; } Example: class Employee { int eid; // data member (or instance variable) String ename; // data member (or instance variable) eid=101; ename="Hitesh"; } AccesModifiers 1. private 2. Public 3. Protected 4. default
  • 14.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CREATING AN OBJECT  Creating an object Syntax Classname object= new Classname() Example: Employee e= new Employee()
  • 15.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College FIRST JAVA PROGRAM class Simple { public static void main(String args[]) { System.out.println("Hello Java"); } } Output: Hello Java
  • 16.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  Public – It is an access modifier that allows an object or a variable or a class or a method to be used anywhere in java programming.  Static – It can execute or call a method or a variable without using an object.  Void - It could return any type of value.  String - It is a class in JVM, since all the code that we give will be taken in a string format we use string there and args is the name of the array.
  • 17.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College STRUCTURE OF JAVA PROGRAM
  • 18.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College EXAMPLE
  • 19.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College PRIVATE:  Private members of class in not accessible anywhere in program these are only accessible within the class. class Hello { private int a=20; private void show() { System.out.println("Hello java"); } } public class Demo { public static void main(String args[]) { Hello obj=new Hello(); System.out.println(obj.a); //Compile Time Error, you can't access private data obj.show(); //Compile Time Error, you can't access private methods } }
  • 20.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College PUBLIC: Public members of any class are accessible anywhere in the program in the same class and outside of class, within the same package and outside of the package. class Hello { public int a=20; public void show() { System.out.println("Hello java"); } } public class Demo { public static void main(String args[]) { Hello obj=new Hello(); System.out.println(obj.a); obj.show(); } }
  • 21.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  At What happens at Compilation time?  What happens at runtime?
  • 22.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College Compile and Run Java Program
  • 23.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College STEPS FOR COMPILING AND EXECUTING THE JAVA PROGRAM
  • 24.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College JAVA VARIABLES  Variables are nothing but reserved memory locations to store values.  Syntax [data_type] [variable_name] = [variable_value]; E.g: int i = 10; //Variable of int type String str = "howtodoinjava.com"; //Variable of string type Object obj = new Object(); //Variable of object type int[] scores = [1,2,3,4,5,6,7,8,9]; / //Variable of int type
  • 25.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College TYPES OF VARIABLES 1. Instance Variable  A variable declared inside the class but outside the body of the method, is called instance variable.  It is not declared as static. 2. Static Variable  A variable which is declared as static is called static variable. It cannot be local.  Memory allocation for static variable happens only once when the class is loaded in the memory.
  • 26.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 3. Local variable  A variable declared inside the body of the method is called local variable. Example class A { int data=50; //instance variable static int m=100; //static variable void method() { int n=90; //local variable } }
  • 27.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College JAVA - BASIC DATA TYPES There are two data types available in Java −
  • 28.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 29.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College JAVA COMMENTS  Comments can be used to explain Java code, and to make it more readable  // - Single-line comments start with two forward slashes.  /* */ - Multi-line comments. Any text between /* and */ will be ignored by Java.
  • 30.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College JAVA OPERATORS  Operators are used to perform operations on variables and values.  The value is called an operand, while the operation (to be performed between the two operands) is defined by an operator. Java divides the operators into the following groups: 1. Arithmetic operators 2. Assignment operators 3. Comparison operators 4. Logical operators 5. Bitwise operators
  • 31.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 1. Arithmetic Operators Arithmetic operators are used to perform common mathematical operations. Example: public class MyClass { public static void main(String[] args) { int x = 5; int y = 3; System.out.println(x + y); } }
  • 32.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 2. Assignment Operators Assignment operators are used to assign values to variables. public class MyClass { public static void main(String[] args) { int x = 5; x += 3; System.out.println(x); } }
  • 33.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 3. COMPARISON OPERATORS  Comparison operators are used to compare two values. public class MyClass { public static void main(String[] args) { int x = 5; int y = 3; System.out.println(x == y);}
  • 34.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 4. LOGICAL OPERATORS  Logical operators are used to determine the logic between variables or values. public class MyClass { public static void main(String[] args) { int x = 5; System.out.println(x > 3 && x < 10); }
  • 35.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 5. BITWISE OPERATORS Bitwise operator works on bits and performs bit- by-bit operation if a = 60 and b = 13; now in binary format they will be as follows −
  • 36.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CONTROL STATEMENTS  The Java if statement is used to test the condition. It checks Boolean condition: true or false.  There are various types of if statement in java.  if statement  if-else statement  if-else-if ladder  nested if statement
  • 37.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College SIMPLE IF STATEMENT  It executes the if block if condition is true. Syntax: if(condition) { True Statemetns; } Example: Class Great { Public static void main(String args[]) { int a = 10, b = 5; if(a>b) { System.out.println(“a is greater than b”); } } }
  • 38.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College IF-ELSE STATEMENT  It executes the if block if condition is true otherwise else block is executed Syntax: if(condition) { //code if condition is true }else { //code if condition is false } Example: public class Example { public static void main(String[] args) { int a=13; int b-20; if(a>b){ System.out.println(“A is big number"); }else{ System.out.println(“B is big number"); } } }
  • 39.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College IF-ELSEIF LADDER  The if-else-if ladder statement executes one condition from multiple statements. Syntax: if(condition1) { //code to be executed if condition1 is true }else if(condition2) { //code to be executed if condition2 is true } else if(condition3) { //code to be executed if condition3 is true } else { //code to be executed if all the conditions are false }
  • 40.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College EXAMPLE public class Example { public static void main(String[] args) { int a=13; int b=20, int c=30; if(a>b)&&(a>c) { System.out.println(“A is big number"); } elseif(b>c) { System.out.println(“B is big number"); } else { System.out.println(“Cis big number"); } } }
  • 41.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College LOOPING STATEMENT  Looping statement are the statements execute one or more statement repeatedly several number of times. While Loop:
  • 42.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  do-while A do-while loop is similar to a while loop, except that a do-while loop is execute at least one time
  • 43.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  For Loop
  • 44.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  For Loop Example
  • 45.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College OOP'S CONCEPT IN JAVA  Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.
  • 46.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College OBJECT Object is a instance of class, or real time entites object has state and behaviors.  An Object in java has three characteristics:  State - Represents data (value) of an object.  Behavior - Represents the behavior (functionality) of an object such as deposit, withdraw etc
  • 47.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CLASS  Class is a blue print which is containing only list of variables and method and no memory is allocated for them. A class is a group of objects that has common properties.  A class in java contains:  Data Member  Method  Constructor  Block  Class and Interface
  • 48.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 49.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College ENCAPSULATION  Encapsulation is a process of wrapping of data and methods in a single unit is called encapsulation. Advantage  To secure the data from other methods, when we make a data private then these data only use within the class, but these data not accessible outside the class.
  • 50.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College ABSTRACTION  Hiding of data is known as data abstraction.  Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context. Real Life Example of Abstraction in Java
  • 51.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College INHERITANCE  The process of obtaining the data members and methods from one class to another class is known as inheritance.
  • 52.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College POLYMORPHISM  The word "poly" means many and "morphs" means forms. So polymorphism means many forms.  The process of representing one form in multiple forms is known as Polymorphism.
  • 53.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College PROTECTED  Protected are also called derived level access modifiers.
  • 54.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 55.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College ARRAY  An array is defined as the collection of similar type of data items stored at contiguous memory locations.  Advantage  One variable can store multiple value  Code Optimization  Random access  Disadvantage Once we declare array there is no chance to increase and decrease the size of an array according to our requirement,
  • 56.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  Types of Array 1. Single Dimensional Array 2.Two Dimensional Array
  • 57.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CONSTRUCTOR  Constructor is a special member method which will be called automatically whenever object is created.  The purpose of constructor is to initialize an object called object initialization. Advantages of constructors in Java  A constructor eliminates placing the default values.  A constructor eliminates calling the normal or ordinary method implicitly.
  • 58.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College HOW CONSTRUCTOR ELIMINATE DEFAULT VALUES  Constructor are mainly used for eliminate default values by user defined values, whenever we create an object of any class then its allocate memory for all the data members and initialize there default values.
  • 59.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College PROPERTIES OF A CONSTRUCTOR  Constructor name must be similar to name of the class.  Constructor should not return any value even void also.  A Java constructor cannot be abstract, static, final, and synchronized  Constructors will not be inherited from one class to another class.  Constructor should not be private provided an object of one class is created in another class  The access specifier of the constructor may or may not be private.  If the access specifier of the constructor is private then an object of corresponding class can be created in the context of the same class but not in the context of some other classes.
  • 60.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 61.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CONSTRUCTOR EXAMPLE
  • 62.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College TYPES OF CONSTRUCTORS Based on creating objects in Java constructor are classified in two types. They are  Default or no argument Constructor  Parameterized constructor. 1. Default or no argument Constructor  A constructor is said to be default constructor if and only if it never take any parameters.
  • 63.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 64.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 2. PARAMETERIZED CONSTRUCTOR
  • 65.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 66.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College PACKAGE  A package is a collection of similar types of classes, interfaces and sub-packages  The purpose of package concept is to provide common classes and interfaces for any program separately.
  • 67.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College TYPE OF PACKAGE Package are classified into two type which are given below. 1. Predefined or built-in package 2. User defined package 1. Predefined or built-in package built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
  • 68.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College USER DEFINED PACKAGE  Rules to create user defined package  package statement should be the first statement of any package program.  Choose an appropriate class name or interface name and whose modifier must be public.  Any package program can contain only one public class or only one public interface but it can contain any number of normal classes.  Package program should not contain any main class (that means it should not contain any main())  The modifier of method of class or interface which is present in the package must be public (This rule is optional in case of interface because interface methods by default public)  Every package program should be save either with public class name or public Interface name
  • 69.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 70.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College COMPILE PACKAGE PROGRAMS
  • 71.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 72.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College JAVA - DOCUMENTATION COMMENTS
  • 73.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College UNIT-2 INHERITANCE & INTERFACES
  • 74.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College INHERITANCE  Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent object Terms used in Inheritance  Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.  Sub Class/Child 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: Super class is the class from where a subclass inherits the features. It is also called a base class or a parent class.  Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
  • 75.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College SYNTAX class Subclass-name extends Superclass-name { //methods and fields }  extends is the keyword used to inherit the properties of a existing class(Base Class)
  • 76.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College TYPES OF INHERITANCE 1.Single Inheritance In single inheritance there exists single base class and single derived class
  • 77.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 78.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 2. Multilevel Inheritance In Multilevel inheritances there exists single base class, single derived class and multiple intermediate base classes. Single base class + single derived class + multiple intermediate base classes.
  • 79.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 80.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 3.Hierarchical Inheritance If more than one class is inherited from the base class, it's known as hierarchical
  • 81.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 82.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 4. Hybrid inheritance Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. A class is derived from two classes as in multiple inheritance. However, one of the parent classes is not a base class. It is a derived class
  • 83.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 84.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  Multiple Inheritance Multiple Inheritance is a feature of object oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with same signature in both the super classes and subclass.
  • 85.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College FUNCTION OVERLOADING  If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
  • 86.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College  Three ways to overload a method 1. Number of parameters. 2. Data type of parameters. 3. Sequence of Data type of parameters
  • 87.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College class DisplayOverloading { int add(int a, int b) { int sum = a+b; return sum; } int add(int a, int b, int c) { int sum = a+b+c; return sum; } } class JavaExample { public static void main(String args[]) { DisplayOverloading obj = new DisplayOverloading(); System.out.println(obj.add(10, 20)); System.out.println(obj.add(10, 20,30)); } }
  • 88.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College ABSTRACT CLASS  Abstraction is a process of hiding the implementation details and showing only functionality to the user.  A class that is declared with abstract keyword, is known as abstract class.  An abstract class is one which is containing some defined method and some undefined method.  In java programming undefined methods are known as un-Implemented, or abstract method.
  • 89.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 90.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College abstract class A { abstract void callme(); void callmetoo() { System.out.println("This is a concrete method."); } } class B extends A { void callme() { System.out.println("B's implementation of callme."); } } class AbstractDemo { public static void main(String args[]) { B b = new B(); b.callme(); b.callmetoo(); } }
  • 91.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College abstract class Vachile { abstract void speed(); // abstract method } class Bike extends Vachile { void speed() { System.out.println("Speed limit is 40 km/hr.."); } public static void main(String args[]) { Vachile obj = new Bike(); //indirect object creation obj.speed(); } }
  • 92.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CONSTRUCTOR IN ABSTRACT CLASS abstract class abst { abst() { System.out.println("I am in Constructor"); } abstract void run(); void display() { System.out.println("I am in display function"); } } class sample extends abst { void run() { System.out.println("I am in Abstract method"); } } class TestAbstract { public static void main(String args[]) { abst obj = new sample(); obj.run(); obj.display(); } } OutPut: I am in Constructor I am in Abstract method I am in display function
  • 93.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College INTERFACES  These methods will not have implementation part hence all the methods declared in an interface are abstract methods  We can implement multiple inheritance using interface.  An interface method can’t be final or static or private or native or protected.  An interface can be extended from another interface.
  • 94.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College SYNTAX interface <interfacename> { // final variables declaration //methods declaration } The general syntax for a class that implements an interface is given below. class <classname> implements <interface name> { }
  • 95.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College interface Area // Interface defined { final static float pi =3.14F; float compute (float x, float y); } class Rectangle implements Area { public float compute(float x, float y) { return (x*y); } } class Circle implements Area { public float compute (float x, float y) { return (pi*x*x); } } class InterfaceTest { public static void main(String args[]) { Rectangle rect = new Rectangle(); Circle cir =new Circle(); System.out.println("Area of Rectangle+rect.compute(10,20); System.out.println("Area of Circle “+ cir.compute(10,0); } }
  • 96.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College interface Developer { void disp(); } interface Manager { void show(); } class Employee implements Developer, Manager { public void disp() { System.out.println("Hello Good Morning"); } public void show() { System.out.println("How are you ?"); } public static void main(String args[]) { Employee obj=new Employee(); obj.disp(); obj.show(); } }
  • 97.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College RELATIONSHIP BETWEEN CLASS AND INTERFACE
  • 98.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College
  • 99.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College FINAL CLASS //example of abstract class that have method body final class Employee { final int salary=10000; } class Developer { void show() { System.out.println("Hello Good Morning"); } } class TestAbstraction2 { public static void main(String args[]) { //Developer obj=new Developer(); Employee o=new Employee(); //o.salary=100; System.out.println(o.salary); //Developer obj=new Developer(); //obj.show();
  • 100.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College ARRAYLIST CLASS  Java ArrayList class uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface. The important points about Java ArrayList class are:  ArrayList class is not Synchronized.  ArrayList class elements can be access randomly.  In the ArrayList value will be stored in the same order as inserted.  ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.  ArrayList class can contain duplicate elements.  ArrayList allows random access because array works at the index basis.
  • 101.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CONSTRUCTORS OF JAVA ARRAYLIST  Syntax: ArrayList<String> al=new ArrayList<String>();
  • 102.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College WAYS TO ITERATE THE ELEMENTS OF THE COLLECTION IN JAVA class arrayli { public static void main(String[] args) throws IOException { int n = 5; // size of ArrayList ArrayList<Integer> arrli = new ArrayList<Integer>(n); for (int i=1; i<=n; i++) arrli.add(i); System.out.println(arrli); arrli.remove(3); System.out.println(arrli); for (int i=0; i<arrli.size(); i++) System.out.print(arrli.get(i)+" "); } }
  • 103.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CLONING  Object cloning refers to creation of exact copy of an object.  It creates a new instance of the class of current object and initializes all its fields.
  • 104.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College CREATING A COPY USING CLONE() METHOD  Every class that implements clone() should call super.clone() to obtain the cloned object reference.  The class must also implement java.lang.Cloneable interface whose object clone we want to create otherwise it will throw CloneNotSupportedException when clone method is called on that class’s object.  Syntax: protected Object clone() throws CloneNotSupportedException
  • 105.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College import java.util.ArrayList; class Test { int x, y; } class Test2 implements Cloneable { int a; int b; Test c = new Test(); public Object clone() throws CloneNotSupportedException { return super.clone(); } } public class Main { public static void main(String args[]) throws CloneNotSupportedException { Test2 t1 = new Test2(); t1.a = 10; t1.b = 20; t1.c.x = 30; Test2 t2 = (Test2)t1.clone(); t2.a = 100; t2.c.x = 300; t1(shallow copy) System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y); System.out.println(t2.a + " " + t2.b + " " + t2.c.x + " " + t2.c.y); } }
  • 106.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College class Student18 implements Cloneable { int rollno; String name; Student18(int rollno,String name) { this.rollno=rollno; this.name=name; } public Object clone()throws CloneNotSupportedException { return super.clone(); } public static void main(String args[]) { try { Student18 s1=new Student18(101,"amit"); Student18 s2=(Student18)s1.clone(); System.out.println(s1.rollno+" "+s1.name); System.out.println(s2.rollno+" "+s2.name); }catch(CloneNotSupportedException c){} } }
  • 107.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College JAVA INNER CLASSES  Java inner class or nested class is a class which is declared inside the class or interface Syntax of Inner class class Java_Outer_class { //code class Java_Inner_class { //code } }
  • 108.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College TYPES OF INNER CLASSES 1) Nested Inner class 2) Method Local inner classes 3) Anonymous inner classes 4) Static nested classes 1) Nested Inner class can access any private instance variable of outer class
  • 109.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College class Outer { // Simple nested inner class class Inner { public void show() { System.out.println("In a nested class method"); } } } class Main { public static void main(String[] args) { Outer.Inner in = new Outer().new Inner();
  • 110.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 2) METHOD LOCAL INNER CLASSES  Inner class can be declared within a method of an outer class class Outer { void outerMethod() { int x = 98; System.out.println("inside outerMethod"); class Inner { void innerMethod() { System.out.println("x= "+x); } } Inner y = new Inner(); y.innerMethod(); } } class MethodLocalVariableDemo { public static void main(String[] args) { Outer x=new Outer(); x.outerMethod(); } }
  • 111.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 3. STATIC NESTED CLASSES  Static nested classes are not technically an inner class. class Outer { private static void outerMethod() { System.out.println("inside outerMethod"); } // A static inner class static class Inner { public static void main(String[] args) { System.out.println("inside inner class Method"); outerMethod(); } } }
  • 112.
    Mr. K.Loganathan ,AP/IT, Mailam Engineering College 4. ANONYMOUS INNER CLASSES  Anonymous inner classes are declared without any name at all class Demo { void show() { System.out.println("i am in show method of super class"); } } class Flavor1Demo { // An anonymous class with Demo as base class static Demo d = new Demo() { void show() { super.show(); System.out.println("i am in Flavor1Demo class"); } }; public static void main(String[] args){ d.show(); } }