You are on page 1of 25

Computational Thinking for Object Oriented Design (CTOOD)

Java Programming Language

Procedure Oriented Programming VS Object Oriented Programming


Procedure oriented programming belongs to Procedure oriented paradigm
C is a procedure oriented programming language
C is oriented around procedures(functions)
Object oriented Programming belongs to Object Oriented Programming
paradigm
Java is one of the Object Oriented Programming language.
The others are Python,C++,and small talk
Java implements all of the Object Oriented Programming features strictly

Object Oriented Programming System (OOPS)

 Objects
 Classes
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
Objects: An Object is a Real World entity.In other words Object is a tangible
entity which is having physical existence
An Object is an instance of a class .Instance means copy or clone
Some of the Objects
Pen,Computer,Chair,Basket,Television etc.

Classes: A Class is a Logical Entity.Classes are not having physical existence.


A Class is a Blue print or template of a set of objects
Some of the classes are
Animal,Vehicle,Employee etc.

Abstraction: Humans manage complexity of objects by using Abstraction


Abstraction hides the data(Complexity of data) of objects to simplify an
interface
Example is The outer details of Car object such as Brake and Accelerator are
revealed to the User in the name of the interfaces,But The internal Details such
as Working of Brake and Accelerator has been hidden to the User.
In Other words Abstraction focuses on the Essentials and hides the Irrelevant
or unnecessary details(data) from the user

Encapsulation:
Is a OOP Concept by which binds the data(Variables and Constants) and Code
together.
Encapsulation lays a wrapper around the data and code to restrict the
unauthorized access
Encapsulation hides data to protect it from the outside interference and
misuse.
It is also called “Data Hiding”
Encapsulation in Java can be implemented by Access Modifiers such as
“Private”
Example of Encapsulation:
A Bank Clerk can have access rights to operations of
Depositing,Withdrawal,transferring of money.
He does not having access rights to data of Staff salaries and Profit and losses
of Bank.
That means The Internal data of bank is encapsulated from the Clerk.

Inheritance:
Is a mechanism in which A Class can get the properties of another class.
In other words, an Object can get the properties another Object
Inheritance facilitates the creation of class hierarchies
Example of Inheritance
Vehicle class contains two child classes such as Motor Driven and Pulled
Vehicles
Again Motor Driven Vehicles classified into Car,Bus,Truck etc.
Pulled Vehicles again classified into Cart and Rickshaw etc.

Polymorphism:
Is a one of the OOP Concepts Polymorphism facilitates the design of a single
interface for a group of a related activities.
Polymorphism is a Greek Word,Poly means many and morphs means forms
A Single interface can be used to handle multiple forms.
Example of Polymorphism
Real Time
Dog Actions are polymorphic
If you think of Dog as a Interface.
Dog actions are different for different situations
If a Dog smells its food, it runs around it
If a Dog identifies a new person He will bark and try to attack

Programmatic:
It is possible to design a single class(single interface) for implementing stack for
various data types
It is possible to design a single interface for implementing various arithmetic
operations such as Addition,Subtraction,Multiplication and Division.

Java Features (Java Buzzwords):

Simple and Familiar


Compiled and Interpreted
Platform In
dependent
Portable
Architecture Neutral
Object Oriented
Robust
Secure
Distributed
Multithreaded and Interactive
High Performance
Dynamic and Extensible

Simple and Familiar:


Java’s Coding style is clean,that means all the complex concepts of C Language
has been withdrawn in the Java such as Pointers,Storage
Classes,Preprocessors,Operator Overloading,Multiple Inheritance and Goto
statements.
In addition there is a “Äutomatic Garbage Collection” to remove the
unreferenced objects from the memory.
Familiar means if any user knows the C or C++ then with little efforts He can
learn the Java
Again This is possible because of elimination of Complex concepts of C such as
Pointers,Storage classes,Preprocessors,Multiple Inheritance etc.

Compiled and Interpreted:


Java is both a compiled and interpreted language.Java is going to generate
the .class file(Byte Code). After compilation of a Java Source Program(.java file).
The .class file contains Byte Code,which is the platform independent code.
The Byte code can be interpreted or executed on any platform provided the
Appropriate JVM is installed on those Systems without modifying the source
program or without recompiling the source program.
Java Combines the power of Compilation and Flexibility of Interpretation.

Platform Independent:
Java is a Platform Independent Programming Language. Java is going to
generate the .class file(Byte Code). After compilation of a Java Source
Program(.java file).
The .class file contains Byte Code,which is the platform independent code.
The Byte code can be interpreted or executed on any platform provided the
Appropriate JVM is installed on those Systems without modifying the source
program or without recompiling the source program.

Portable:
Java programs are portable in the sense that the Java programs runs on any
Hardware provided the appropriate JVM has installed on those machines.
It is not the case with C/C++.These languages needs slight chages in the code
for different hardwares.
Java is having fixed memory size for its Data types for all of the Hardware
Architectures.
In C/C++ the memory size of the data types are different for different
hardware architectures.
Architectural Neutral:
The meaning of this feature of Java is program written for a platform or OS is
independent of other platforms or environments and can run on any other
operating system without recomplinig them.
In other words it is based on the “Write Once and Run Anywhere(WORA)”
approach.
Byte code is not dependent on any machine architecture and Java Virtual
Machine can easily translate bytecode into a machine specific code
This feature is very useful when we develop applets or download applications
from the Internet.

Object Oriented:
Java is a Object Oriented Programming Language.Java implements all the
features of Object Oriented Programming System(OOPS) such as
Abstraction,Encapsulation,Inheritance and Polymorphism.
Java does not create a Executable program without creation of a Class.
This means that Java Strictly follows the concept of Encapsulation.

Robust:
Java is a Robust(Trustable) Programming Language.Many of the Programming
Languages fails in the two situations .They are Memory Management and
Exception(Error) Handling.
Java handles these two situations very Efficitently
First.Java is having Efficient Memory Management such as “Automatic Garbage
Collection”,Which destroys the unreferenced Objects Automatically.
Complex Concept of C such as Pointers has been removed in the Java.
Java provides the built in support for Exception(Error) Handling.
Java handles the Exceptions(Run time errors) by using built in program
constructs such as try,catch,throw and throws

Secure:
Java provides Security for its programs as well as underlying System.
Java provides access modifiers to check memory access and also ensure that
no viruses enters a application or applet.
Java programs runs with in a Java Virtual Machine.The JVM does not allow a
Java program to access the unnecessary parts of the Memory.
Java does not allow a programmer to crate the Pointers explicitly.

Distributed:
Java programs can be distributed over the network.
The different classes of a Java program can be distributed on different systems
in a network.The communication between these different parts of a single
program is possible through a Concept of Java called Remote Method
Invocation(RMI).
Java also supports applets which are Distributed programs.The applets can be
distributed over the Internet.
Multithreded and Interactive:
Java Supports Multithreding.A Thread is a independent path of Execution of a
Program .In other words A thread is a part of program.The Different thread of
a single Java program can run simultaneously to save the processing time.
Java programs are Interactive means Java supports Commandline user
interface(CUI) and Graphical User Interface(GUI).
Command Prompt is the Command Line User Interface and AWT and Swing
provides support for Graphical User Interface.

High Performance:
The performance of Java is impressive for an Interpreted Language because of
its Byte code.Byte code generated by the Java compiler are highly optimized
So Java Virtual Machine can execute them much faster.

Dynamic and Extensible.


Java is Dynamic and Extensible means with the help of OOPS we can add
classes and new methods to classes creating new classes through sub classes.
This makes it easier for us to expand our own classes and even modify them,
All the methods of Java can be evaluated at run time dynamically.
Java also have capability of invoking the native functions(Functions of C/C++)
This is possible by using the Java Native Development Interface(JNDI).

Arrays
1. One Dimensional Arrays
2. Two Dimensional Arrays

C Language
Data type arr_name[size]; Data type name_array1[rows][cols];

Java

Syntax

One Dimensional Array:

Data type arr[]= new Data type[size];

Example: int a[]= new int[3];

Two Dimensional Array

Data type arr_name[][]= new Data type[rows][cols];


Data type[][] arr_name= new Data type[rows][cols];
Data type [][]arr_name=new Data type[rows][cols];
Data type []arr_name[]= new Data type[rows][cols];

Example:
Int arr[][]=new int[3][3];

Note : Refer the programs posted in the Group

Access Specifiers:

There are Four Access Specifiers in Java


1. Public
2. Private
3. Protected
4. Default(No Modifier)

1. Public : This Access specifier is having global access.Any class or any


programming construct can access the public methods and variables.
2. Private: This Access specifier is having limited access.only the class which is
holding the private members can access the same.
3. Protected: These members can be accessed only by the classes of the
same package and sub classes of the other packages.
4. Default: These members are accessed only by the members of the same
package (classes and sub classes

Control Statements:
There are various Control Statements available in Java
They are classified into two types

1. Selection statements
2. Iterative statements

1. Selection statement
a). If Statement
b). Switch statement

a) If statement

Four types
 Simple if
 If-else
 If-else if ladder
 Nested if
Syntax:
If(condition){
// statements
}
If(condition){
//statements
}
else{
//statements
}

If(condition){
//statements
}
else if(condition){
//statements
}
else if(condition){
//statements
}
else if(condition){
//statements
}
.
.
.

else{
//statements
}
If(condition){
If(condition){
//statements
}
else{
//statements
}
}
else{
//statements
}

Switch Statement

Syntax:
switch(condition){
case 0: //statements
break;
case 1: //statements
break;
case 2: //statements
break
.
.
.
case N: //statements
break;

default:
//statements
}

 Iterative statements

1. While loop
2. Do-while loop
3. For loop
4. For each loop
Syntax:

while(condition){
//statements
}
do{
//statements
}while(condition);

for(Initialization;Condition;Iteration){
//statements
}

for(data type loop-var: programming-construct){


//statements

Note : Refer the programs posted in the group

Wrapper Classes

int a;
long b=2345;
a=(int)b;

Conversion of primary data types(int ,float etc) to Wrapper classes is called as


Autoboxing
Conversion of Wrapper classes to primary types is called as Unboxing
Note: See the program shared in the Group

Classes and Methods

class class-name{
data type var1 =val1;
data type var2=val2;
data type var3=val3;
return type method_name1(param1,param2,param3… paramN){
//Statements
}
return type method_name2(param1,param2,param3,… paramN){
//Statements
}
.
.

.
return type method_nameN(param1,param2,param3…paramN){
//Statements
}
}
Note: See the program shared in the group

Constructors
A Constructor is a special method of a class which is used to initialize a class.
A Constructor is in turn initializes the objects of a class
There are two kinds of constructors
1. Default Constructor
2. Parameterized Constructor

Default Constructor: This Constructor is a non parameterized constructor.


It means this constructor is not having any parameters.

Syntax:
Class-name(){
// Statements
}
Ex:
My_Class() {
Int a=10;
Float b = 3.6;
}

Parameterized Constructor: This Constructor contains a set of parameters.


Syntax:
Class-name(param1,param2,param3…paramN){
// Statements
}
Ex:
My_Class(int a,int b,float c,double d){
p=a;
q=b;
r-c;
s=d;
}

Constructor Overloading : Constructors can be overloaded just like


methods.Overloading means specifying constructors with different list of
parameters.
Note : Refer the Programs in the Group

Scanner Class:
Scanner class is a built in class of java.util package.This class is used to
accept the input from a end user.This class allows the easy method of
accepting input from a user.
Scanner class accepts the System.in as argument for implementing input.
This class accepts various data types such as int ,float,char and long etc.
Syntax:
Scanner object-name=new Scanner(input-stream);
Ex:
Scanner obj = new Scanner(System.in);
Note: Refer the Scanner Program in the Group

Method Overloading:
In Java it is possible to define multiple methods for a class with the same
name,but those methods should have the different list of parameters or
different return types
Java supports only the different set of parameters for methods of method
overloading concept.
Note: Refer the Group for the program

Inheritance
Inheritance is one of the Object Oriented Programming Features
It is a mechanism in which a class can get the properties of another class.
In turn an object of a class can get the properties of some other object of
another class.
The advantages of Inheritance is:
1. Code Reusability
2. Run time Polymorphism
1. Code Reusability:
The derived class can get the state and behavior of its base class
That means the derived class is reusing the state and behavior of its base
class
In Java Base class is called as Super class and derived class is called as
sub class.

2. Run time polymorphism:


The derived class/classes can override(over write) the methods of its
base class.This concept is called as method overriding

Java implements run time polymorphism by using method overriding.

Types of Inheritance:
1. Single Inheritance
2. Multi level Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance

 Hybrid Inheritance is the sub type of Multiple Inheritance


1. Single Inheritance:
A class can get the state and behavior of another class.only two classes are
present in this type of Inheritance

EMP

SOFTWARE ENGINEER

2. Multi level Inheritance:


There are multiple levels in this inheritance.In each level at least one class
should be present
ANIMAL

CAT

KITTEN

3. Hierarchical Inheritance
In this Inheritance two or more derived classes can be inherited from a
single base class.

VEHICLE

CAR BIKE

4. Multiple Inheritance:
A class can get the properties of more than one class.
Java does not support the Multiple Inheritance directly using the classes.
Instead it is implementing multiple inheritance by using the concept of
“Interface”

GRAND PARENT PARENT

CHILD

If a class inherits the properties of two or more classes then there is a


ambiguity of members of two or more base classes .
That is how Java implements Multiple Inheritance by using the concept of
Interfaces.

Hybrid Inheritance:
This inheritance is a combination of Multiple and Hierarchical Inheritance.

CLASS A

CLASS B CLASS C

CLASS D
Method Overriding:
Method Overriding is a concept in which a derived class can override (over
write ) the method/methods of its base class.
Java implements run time polymorphism by using method overriding.
Note: Refer programs in the group.

Association:
Association mean relationship between two entities.
In Java Association means relationship between two classes.
The relationships are four types
 One to one
 One to many
 Many to one
 Many to many
In one to one one class is having relation with another class.
Ex: A Professor works for a Department.
In one to many one class is having relation with multiple classes
Ex: A Professor can work for multiple departments.
In many to one many classes is having relation with a single class
Ex: Group of professors can work for a single department
In many to many ,many classes can have relation with another set of
classes.
Ex: Many professors can have relation with many departments.

Forms of Association in Java


Before we learn about forms of associations in Java.
We need to learn about relation between objects in OOPS.
There are two types of relation between objects.
1. IS-A
2. HAS-A

1.IS-A Relation
IS-A Relation in Java can be implemented by the concept of “Inheritane”

2.HAS-A Relation(Association)
In this a class is “HAS-A” Relation with another class.
Ex: Dept class is having HAS-A Relation with Professor class.

There are two forms of Association .


 Aggregation
 Composition

Aggregation in Java is a form of HAS-A Relation between two classes.


It is a relatively more loosely coupled relation than composition.
It is a weak Assocation means one class can exist independently with
out the another class.

Ex: Country class is having HAS-A Relation with SportsPerson class


An Apple can exist without a basket.

Composition in Java is a form of relation that is more tightly coupled.


Its is also strong association.
This is also called “BELONGS-TO” Association as one class ,for all
intents and purpose belongs to another class and exist because of it.
In a composition association the classes can not exist independently.

Ex: The composition(association) between College and Student.


College class can not exist without a Student class and vice versa.
Car class can not exist without a Engine class and vice versa.

Inner Classes
Java inner class or nested class is a class that is declared inside the class
or interface.Inner classes can be used to logically group classes and
interfaces in one place to be more readable and maintainable.
Additionally it can access all the members of the outer class including
private members and methods.
Syntax:
class Outer{
class Inner{
// statements
}
}
Advantages of Java Inner classes:
1. Nested classes represent a perticular type of relationship that is it can
access all the members of the outer class including private.
2. Nested classes are used to develop more readable and maintainable
code because it logically group classes and interfaces in one place only
3. Code optimization: It requires less code to write

There are four types of inner classes


1. Member inner class
2. Anonymous inner class
3. Local inner class
4. Static nested class

Member Inner class: It is a class defined with in the class scope and not with in
the method scope.
Anonymous Inner class : It is a inner class defined with no name.
Local Inner class: It is a class defined with in the scope of a method of some
other class.
Static Nested class: it is a static class defined with in the scope of some other
class.
Note: Refer the Programs of the Group.

Singleton Class
In Java Singleton class is a class which is having only one object.If a user try to
create some more objects,those objects refers to first object(instance) of the
class.
While deigning a singleton class the following points to be remembered.
1. We need to make constructor private.
2. Write a static method that has the return type as object of the singleton
class.
Advantages of Singleton class:
The primary use of singleton class is to restrict the limit of the number of
object creation to only one.this often ensures that there is access control to
resources. For instance Socket or database connection
There is no wastage of memory in the case of Singleton class,because it
restricts the instance creation.

Array of Objects:

In Java it is possible to create an array of objects just like the creation of an


array of primary types such as int ,float etc.
Syntax:
Class-name obj[] = new Class-constructor[size];
Ex:
Demo obj[] = new Demo[5];
Note: Refer the Group for Programs.

You might also like