Object-Oriented Programming With Java
Object-Oriented Programming With Java
with Java
Introduction
• Java is a platform independent because of JVM and Byte code
• When you compile java source code it generates the byte code
which can be understood by JVM only
• When you running the byte code first it will be converted into native
code and then it will be executed (interpreter convert byte code to
native code)
• Java program goes through foll. Phases:
Edit Hello
Compiler Compiler(javac.exe)
Loading Hello.class(byte code)
Verifier Interpreter(java.exe)
Execute native code
2
Java program Structure:
class Hello{
public static void main(String as[])
{ System.out.println(“java”);
}
}
• The keyword class to declare that a new class is being defined Hello is an
identifier that is the name of the class
• All java applications begin execution by calling main()
• Static allows main() to be called without having to instantiate a particular
instance of the class
• String as[] declares a parameter named arguments present when the
program is executed
• System is predefined class (access to the system)
• out is the output stream that is converted in to the console
• println displays the string which is passed to it (displays other types of infor.
too)
3
Class
• A class is a blueprint or prototype that defines the variables and methods
common to all objects of a certain kind
• A class is declared by use of the class keyword
Syntax:
class class name
{
type instance-variable1;
type instance-variable1;
….
type instance-variable N;
type method name1(parameter list)
{ //body of method
}
………..
type method nameN(parameter list)
{ //body of method
}
4
Example:
class Hello
{ double width;
double height;
double depth;
}
• The data or variables defined with in a class are called instance variable
• The code is contained with in methods
• The methods and variables defined with in a class are called members of the
class
1. Instance variables can be -Static variables also cab be
primitive and reference primitive or reference
2. Multiple copies of Memory -Single copy of memory
will be allocated for multiple will be allocated for multiple
objects objects
5
Objects
• Objects is constructed either on a memory heap or on a stack
Memory heap:
• The objects are created using the new keyword(operator)
• Some heap memory is allocated to this newly created objects
Stack:
• During method calls, objects are created for method arguments
and method variables.These objects are created on stack
Class and object:
• The class is not shaded because it represents a blueprint of an
object rather than an object itself.
• An object is shaded, indicating that the object actually exists and
you can use it.
• Classes provide the benefit of reusability
• Objects provide the benefit of modularity and information hiding
6
Example:
Class Box
{ double width;
double height;
double depth;
}
Class BoxDemo
{
Public static void main(String as[])
{ //reference to an object of type Box
Box mybox = new Box();
Double vol;
mybox. width=10;
mybox. heighrt=20;
mybox. depth=30;
vol=mybox. width*mybox. height*mybox. depth;
System.out.println(“volume is”, +vol);
}
}
7
Statement Effect
null
null
Box
8
Assigning object reference variables
• Its acts differently than you might expect when an assignment
takes place
Box b1=new Box();
Box b2=b1;
• You may think that b2 is assigned a reference to a copy of the
object referred to a by b1.b1 and b2 refers to separate and distinct
objects but b1 and b2 both will refer to the same object
• The assignment of b1&b2 did not allocate any memory or copy any
part of the original objects
• B2 refers to the same objects as does b1.
• Change made to the object through b2 will effect to b1
Box b1=new Box();
Box b2=b1;
b1=null;
• B1=null but b2 is still points original object b1.
9
Access Modifiers
• Access Modifiers also called as Access specifiers or visibility Modifiers
• Access Modifiers will be used to specify the scope for the variables,
methods and classes
• They are four Access modifiers
Private
Default
Protected
Public
Private: members of a class can be accessed with in the class only and can’t be
occurred within the subclass and non subclass of same package.
Default, Protected, Public: member of a class can be accessed directly in the
subclass be access with object in non-subclass.
10
Example: class C
class A {
void show()
{ private int a =10; {
private int b =20; A obj =new A();
protected int c=30; System.out.println(“c-
public int d =40; class”);
void show(){
System.out.println(“a class”); System.out.println(“obj.c”);
System.out.println(“a ”);
System.out.println(“obj.d”);
System.out.println(“b”);
}
System.out.println(“c”);
}
System.out.println(“d”);
class Ex1
}
{
}
Public static void main(String as[])
class B extends A
{
{
A obj1 =new A();
void show()
B obj2 =new B();
{ System.out.println(“b- C obj3 =new C();
class”); System.out.println(“c”); Obj1.show();
System.out.println(“d”); Obj2.show();
} Obj3.show();
} }
}
11
Inheritance
• Is a process of writing a new class from the existing class.New class is
called as subclass or derived class and existing class as a super class
• To implement an inheritance we have to use “extends “ keyword
class A
{ …..
}
class B extends A
{ ……….
}
• B is extending A because of this all the A (functionality)
members become members of B class
• Super class will considered first and then subclass.All the members of super
class is members of sub class
12
class b extends A
Example: {
……………
class A ……………
{ int a =10; ……………
static int b =20; ……………
{ }
System.out.println(“a ”); class ex1
System.out.println(“ A nsatance”); {
} public static void
Static main(String as[])
{ {
System.out.println(b); B obj=new B();
Sysytem.out.println(“a-static”);
obj.show(A);
}
void show A() obj.show(B);
{ }
System.out.println(a); }
System.out.println(b);
}
}
13
Types of Inheritance
1. Simple Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
Simple Inheritance: will have only one subclass and one super class
14
Multilevel :
• Super class can have many subclass. in this one is directs subclass and all
remaining are indirect subclasses
• One subclass can have many super class
• In this one is direct super class and remaining are indirect super class
Hierarchical :
one super class have many direct sub classes and one
Sub class can have only one super class
Multiple :
• one sub class can have many super class and one super class can have
only one sub class
• Multiple inheritance is not allowed in Java using java class and is allowed
using java interfaces
Hybrid :
• Is combination of simple and multiple, hierarchical , multilevel inheritance
• We are unable to implement hybrid because it include multiple.
15
Why multiple inheritance is not allowed with java?
• Combinations:
class extends class
class implements interfaces
interface extends interface
interface not allowed class
16
Methods
• Method is a set of statements with some name
• Syntax:
[modifier] return-type methodname(datatype var1,var2….)
{ ……..
……. Method body
}
Ex:
1. void m1() 2. int m2()
{ { s.o.p(“m2”);
s.o.p(“m1”); return 10;
} }
• Method may or may not return a value when method is not
returning any value we should specify the method type as void
and no need to write returns statement inside the method
17
• When the method is returning the value we should specify the data
type of that value as the return type and we must specify the return
statement
• We can define two types of methods
Instance methods
Static methods
Ex:
class A
{
int a=10;
static int b=20;
void m1()
{
s.o.p(a); }
static void m2(){
s.o.p(b);}
}
18
Encapsulation
• Wrapping of data and function into a single unit called encapsulation
Ex: all java programs
• Nothing but data hiding ,like the variables declared under private of a
particular class are accessed only in that class and cannot access in
any other the class .or hiding the information from others is called as
Encapsulation.
• Encapsulation is the mechanism that binds together code and data it
manipulates and keeps both safe from outside interference and misuse
• Encapsulation helps clarify the distinction between an objects operation
and implementation
• The implementation of an object can change without implications for
the clients
• Encapsulation also reduces complexity, as the internal operations of
an object are hidden from the clients, who can not influence its
implementation
• Because of encapsulation, the code you write tends to be modular.
19
• Example:
public class LineOfText
{ //return the length of the line
public int getLength()
{
return length;
}
//return the current index
public int getCurrentIndex()
{
return index;
}
//advance the index by 1 if it is not already at the end
public void forwordCharacter()
{
if(index<length-1)
{
index++;
}
}
private int length;
private int index;
private String buffer;
20
• LineOfText object has length,index,buffer that are not exposed to
any piece of code outside of this particular class.
• These fields are private and are so specified by use of the private
keyword
• Only the getLength(),getCurrentIndex() and forwardCharacter()
methods in the object are visible to other parts of the program
that use this class
Java provides two kinds of encapsulation constructs:
1. Classes that are physical encapsulation and fallowes the same
notation as c++
2. Packages that are logical encapsulation and fallow the notation of
packages. In this case ,any class definition can specify that it
belongs to a particular package
A class that does not specify a package name would be placed in
an unnamed package.
21