Java Basics
(Fundamentals)
By
Mahesh Kumar G P
Agenda
Java Ws…?
Parameter passing
Packages
Encapsulation
Getter methods returning objects
Assignments
Java Ws
The below list answers some of the questions like,
what is java ? Why java ? Etc..
Simple:
Don’t believe me…?
What if I say ,java doesn’t have pointers.
Secure and Portable :
Applications written using Java are portable, in the sense that they can be executed on
any kind of computer containing any CPU or any operating system.
When an application written in Java is compiled, it generates an intermediate code file
called as “bytecode”.
Bytecode helps Java to achieve portability.
This bytecode can be taken to any computer and executed directly.
Object-Oriented and functional:
supports all the features of object oriented model like:
Encapsulation
Inheritance
Polymorphism
Abstraction
Supports functional programming through lambdas
Multithreaded
Parameter passing
public static void main(String... args)
{
int a = 10;
int b = 20;
S.O.P.(a, b); // 10, 20
swap(a, b);
S.O.P.(a, b); // 20, 10
}
Parameter passing
public static void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
Parameter passing ? stack view
Y(20)
X(10)
B(20)
A(10)
Parameter passing
public static void main(String... args)
{
Integer a = 10;
Integer b = 20;
S.O.P.(a, b); // 10, 20
swap(a, b);
S.O.P.(a, b); // 20, 10
}
Parameter passing
public static void swap(Integer x, Integer y)
{
Integer temp = x;
x = y;
y = temp;
}
Parameter passing
20
X
B
10
A
Parameter passing
Java parameter passing is always
“pass by value”
Packages: WHY?
Class name conflicts
Act as name spaces
Logical grouping of classes
ease of maintainence
Encapsulation
public class MyInt
{
private int value;
public void setValue(int value)
{
this.value = value;
}
public int getVaule()
{
return value;
}
}
Why encapsulation
Enforcing constraints
Hiding implementation
Synchronization
Encapsulation ? reference leaking
public class Employee
{
private Date joiningDate;
public Date getJoiningDate()
{
return joiningDate;
}
}
Encapsulation ? reference leaking
Returning references to
private/protected
mutable
objects
How to rectify?
cloning!
What is inheritance
Person
Employee Student
What is inheritance
To inherit: to derive or acquire from ancestors
In an OO world, what can be acquired?
data
code
In an OO world, who are the
ancestors/descendants?
classes
What is inheritance
In an OO world when a class acquires
data
methods
or both
from another class
it is inheritance
Why inheritance
Code reuse
Less code to maintain
Creation of frameworks
parent classes perform common tasks
children classes provide specific details
Loose coupling/pluggability
Polymorphism
Polymorphism
poly: many
morph: form
when a class reference takes many forms
class Parent...
class Child1 extends Parent...
class Child2 extends Parent...
Parent p = new Child1(); //or Child2() or Parent()
Method overriding: dynamic
polymorphism
public class Foo
{
public void foo(){S.O.P.(?in parent foo()?);}
}
public class FooChild extends Foo
{
public void foo(){S.O.P.(?in child foo()?);}
}
Foo f = new Foo(); f.foo(); // in parent foo()
Foo f = new FooChild(); f.foo(); // in child foo()
Dynamic polymorphism
Dynamic polymorphism because
which method to call
is decided at runtime
Static Polymorphism
public class Foo
{
public void foo(int x){S.O.P.(?int: ? + x);}
public void foo(long x){S.O.P.(?long: ? + x);}
}
int i = 10;
long j = 20;
Foo f = new Foo();
f.foo(i);
f.foo(j);
Static polymorphism
Static polymorphism because
which method to call
is decided at compile time
Method access modifier overridings
class Foo
{
public void foo()...
}
class FooChild extends Foo
{
protected void foo()...
}
What is wrong in this?
Method access modifier overridings
No clear way to resolve the ambiguity of restricted
access modifier overriding
Hence it's not allowed by the compiler
Method access modifier overridings
class Foo
{
protected void foo()...
}
class FooChild extends Foo
{
public void foo()...
}
What is wrong in this?
Method access modifier overridings
Nothing wrong in it!
The contract of allowing subclasses or package
classes to access 'foo()' will always be honoured
Hence it's allowed by the compiler
A simple way to remember this
Liskov Substitution Principle (LSP)
Sub classes should be able to replace their base
classes
public -> protected can not
protected -> public can
Member variable overriding
class Foo
{
protected int x = 10;
public void foo(){S.O.P.(x)}
}
class FooChild extends Foo
{
protected int x = 20;
}
Foo f = new FooChild();
f.foo(); // what will this print?
Member variable overriding
This will print 10
Java has no concept of member varibale overriding
Static method overriding
class Foo
{
public static void foo(){S.O.P.(?in parent?)}
}
class FooChild extends Foo
{
public static void foo(){S.O.P.(?in child?)}
}
Foo f = new FooChild();
f.foo(); // what will this print?
Static method overriding
“in parent”
Static methods belong to classes
Their calls resolved at compile time
Hence they can not be polymorphically overridden
Static method overriding
class Foo
{
public static void foo(){S.O.P.(“in parent”)}
}
class FooChild extends Foo
{
public static void foo(){S.O.P.(“in child”)}
}
FooChild f = new FooChild();
f.foo(); // what will this print?
Static member inheritance
class Foo
{
protected static int x = 10;
public static void printX(){S.O.P.(x);}
}
class FooChild extends Foo
{
public static void setX(int val){x = val;}
}
FooChild.setX(100);
Foo.printX(); // what will this print?
Static member inheritance
This will print 100
Static members are shared, not copied
Assignment
Write a simple Java program to prove that method
overloading is static polymorphism
i.e., a call to one of these methods is resolved at
compile time
public class Example {
void method(int n) {
System.out.println("Number: " + n);
}
}
public class ExampleWithString extends Example {
void method(String s) {
System.out.println("Text: " + s);
}
public static void main(String[] args) {
Example e = new ExampleWithString();
e.method(23);
e.method("Hello"); // Error. Even though instance has method.
}
}