0% found this document useful (0 votes)
13 views39 pages

Java Fundamentals Overview

Uploaded by

Dilip Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views39 pages

Java Fundamentals Overview

Uploaded by

Dilip Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

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)
{
[Link] = 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(); [Link](); // in parent foo()


Foo f = new FooChild(); [Link](); // 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();
[Link](i);
[Link](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();


[Link](); // 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();


 [Link](); // 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();


[Link](); // 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;}
}

[Link](100);
[Link](); // 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) {
[Link]("Number: " + n);
}
}

public class ExampleWithString extends Example {


void method(String s) {
[Link]("Text: " + s);
}

public static void main(String[] args) {


Example e = new ExampleWithString();

[Link](23);
[Link]("Hello"); // Error. Even though instance has method.
}
}

Common questions

Powered by AI

Static polymorphism, also known as compile-time polymorphism, occurs via method overloading. It is resolved during compile time based on the method signature. Example: If a class has multiple methods with the same name but different parameters, the compiler decides which method to call based on the arguments supplied. Dynamic polymorphism, or runtime polymorphism, uses method overriding, where the method to be invoked is determined at runtime based on the object reference. An example is when a subclass provides a specific implementation for a method declared in its superclass, which is invoked through a superclass reference .

Dynamic polymorphism in Java allows a method to perform different tasks based on the object invoking it, making method calls resolved at runtime. This is beneficial for method overriding as it enables a class to provide a specific implementation of a method that is already defined in its superclass. It facilitates the use of a single interface to represent different types of objects, improving code flexibility and reusability. For example, a method call to an overridden method in a subclass will execute that method in the subclass, not the superclass's version .

Static method overriding is not possible in Java because static methods are class-specific and are bound to the class, not the object instance. Their resolution is done at compile time, which means that the method call is determined from the type of reference variable, not the actual type of the object it points to. Hence, there is no dynamic method dispatch, which is required for method overriding .

Java ensures security by eliminating the use of pointers, which prevents direct access to memory. This design choice minimizes the risk of pointer manipulation and memory corruption, thereby safeguarding applications from a variety of security vulnerabilities .

Packages in Java serve several purposes: they prevent naming conflicts by acting as namespaces, provide a logical grouping of related classes, and enhance code maintainability. By grouping classes that serve similar functionalities, packages help in organizing code in a modular way, making it easier to manage large codebases .

Inheritance reduces code redundancy by allowing common code to reside in a superclass from which multiple subclasses can inherit. This reduces duplication as subclasses reuse the existing code. For instance, a superclass 'Vehicle' might have a method for 'startEngine'. Different subclasses like 'Car', 'Bike', or 'Truck' can inherit 'Vehicle' and start their engines without rewriting the code. This promotes less maintenance since changes in the superclass automatically reflect across all subclasses. Inheritance enables framework creation by allowing the superclass to define generic behavior (startEngine), while subclasses provide specifics (how they start).

Encapsulation in Java, facilitated by access modifiers, supports the Liskov Substitution Principle (LSP) by ensuring that subclasses can replace their superclasses without altering the desired properties of the program. By controlling access to class members and enforcing constraints through encapsulation, Java ensures that methods in subclasses fulfill contracts defined in superclasses. For example, modifying access from 'public' to 'protected' violates LSP as it restricts access, while moving from 'protected' to 'public' is compliant since it widens access .

Java uses 'pass-by-value' for parameter passing, meaning that when a method is called, a copy of each argument is passed to the method. Hence, modifications to the parameters within the method do not affect the original arguments. For primitive data types, changes made to the parameter will not affect the original value outside the method. For objects, the actual reference is copied, so the original object can be modified, but reassigning the object within the method to a new reference does not affect the actual reference outside the method .

Encapsulation in Java involves restricting direct access to certain components of an object and can be achieved using access modifiers like private, protected, and public. It helps prevent reference leaking by controlling the exposure of class members. For instance, private variables can be accessed only via public methods, often called getters and setters. To mitigate reference leaking, when a method returns a reference to a mutable object, a copy of the object should be returned instead. In the case of a class with a private Date field, returning a clone of the Date object can prevent the caller from modifying the original object .

Java achieves platform independence through the use of bytecode. When a Java application is compiled, it is converted into bytecode rather than machine-specific code. This bytecode can be executed on any system that has a Java Virtual Machine (JVM), which interprets the bytecode into machine code appropriate for the host system, allowing the application to run on any platform without modification .

You might also like