0% found this document useful (0 votes)
5 views72 pages

Java Presentation

The document provides a comprehensive overview of Java programming, covering fundamental concepts such as data types, classes, objects, methods, and OOP principles like inheritance and polymorphism. It explains the architecture of Java, including WORA (Write Once Run Anywhere), and details various programming constructs like constructors, keywords, and variable types. Additionally, it discusses application types and the significance of methods and constructors in Java programming.
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)
5 views72 pages

Java Presentation

The document provides a comprehensive overview of Java programming, covering fundamental concepts such as data types, classes, objects, methods, and OOP principles like inheritance and polymorphism. It explains the architecture of Java, including WORA (Write Once Run Anywhere), and details various programming constructs like constructors, keywords, and variable types. Additionally, it discusses application types and the significance of methods and constructors in Java programming.
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
You are on page 1/ 72

JAVA

BY- HARSHWARDHAN ZADE


TOPICS

• Introduction to Java • Data Types in Java


• Class And Object • Constructor
• State And Behaviour • This Keyword
• Keywords • Parameter And Argument
• Syntax • Methods
• Identifiers • Variables
• WORA Architecture • OOPS Concept
• Declaration And Initialization
INTRODUCTION TO JAVA

What is Java?
Java is an Object Oriented Platform Independent Highlevel Programming Language.

1) JSE Java Standard Edition


( Core Java + JDBC)

2) JEE Java Enterprise Edition


(jsp Servlet)
Language  Medium to communicate

Program  set of instruction for machine to perform some specific task

High Level Code  human readable format

Low Level Code  machine readable format

Intermediate Code  partially readable by human & machine

Platform  combination of processor & O.S.

 It follows WORA (Write Once Run Anywhere) architecture


Understanding Applications

Application: Types of Applications:


 A set of programs that take input and 1. Standalone Applications (Desktop):
 GUI (Graphical User Interface)
produce output.
 CUI (Character User Interface)
Purpose of Using Applications:
 Simplifying tasks and saving time. 2. Web Applications:
 Presentation Layer / Frontend (HTML, CSS,
JS, Bootstrap)
 Business Layer (Java, J2EE, Frameworks)
 Persistence Layer / Database (SQL)
CLASS AND OBJECT

CLASS  it is Blueprint , Design etc.


( Logical Entity )

OBJECT  it is Physical Entity


( Entity means Existence )
COMPONENTS OF OBJECT
1) State ( Attribute )
 represents the Properties.
example int a=10;

2) Behaviour ( Method )
 represents the functionalities performed by an object.
example public void m1(){
//implementation
}
KEYWORDS

Collection of predefined words / reserved words.


Keywords forKeywords for Flow-
Keywords Keywords Class
datatypes(8)Control(11) for for related
byte if Modifiers( Exception keywords(
int else 11) Handling( 6)
short switch public 6) class
long break private try extends
float case protected catch interface
double default static finally import
boolean while final throw implements
char do abstract throws package
for synchrinized assert
continue native
return Strictfp
Object transient
related Void-return Unused
volatile enum Reserved
Keywords Type(1) Keyword(2) Keyword(1) Literal(3)
(4) void goto enum true
new const false
instanceof null
super
this
SYNTAX OF JAVA PROGRAM & MEMBERS

class Class Name{


public static void main( String[] args){
}
}

Members -->
1) State
2) Behaviour
3) Constuctor
4) Blocks
5) Main method
IDENTIFIER RULES

1) Class name should not start with number


Invalid --12Promax Valid -- Pocox5pro
2) Class name must not contain spaces
Invalid -- Xiao Mi Valid -- XiaoMi
3) Class name must not contain any special character( Except--> $ , _ )
Invalid -- Anu-Pama Valid -- $Anupama , $ , _
4) Class name can’t be a keyword
Invalid -- int Valid -- Int
5) Class name should not start with lower case
Invalid -- upma Valid -- Upma
JAVA COMPILATION

WORA Architecture
DECLARATION AND INITIALIZATION OF STATES &
BEHAVIOUR
Declaration --> Is the process of writing the properties
Eg -- class Laptop {
String Brand;
String Ram;
int Price;
}
Initialization --> Is the process of assigning the data to the properties
Eg – class Laptop {
String Brand=“ Asus ”;
String Ram=“ 32GB ”;
int price=98000;
}
WAYS OF INITIALIZATION

1) Directly
2) Object reference
3) Constructors
4) Blocks
5) methods
DIRECT INITIALIZATION

Def :- When we declare and initialize the property at the same time is known as Direct Initialization.
Eg–

class Cycle {
String Name =“Next”;
String Color =“Red”;
int Price =20000;
}
main()
{

}
INITIALIZATION USING OBJECT REFERENCE
Def :- When we declare the property using object reference is known as Initialization using Object Reference.
Eg—
class Pen {
String Name ;
String Color ;
}
public class Demo{
public static void main(String[] args) {
Demo d=new Demo();
d.Name=“Tri-Max”;
d.Color=“Blue-Black”;
System.out.println(d.Name+” ”+d.Color);
}
}
INITIALIZATION USING CONSTRUCTOR
Def :- When we initialize the property by using constructor is k/a Initialization using Constructor.
Eg-- class Student {
String name;
Student () {
this.name=“Sadhu”;
}
}
public class Demo {
public static void main ( String[] args ) {
Student s=new Student();
System.out.println( s.name );
}
}
INITIALIZATION USING METHODS
Def :- When we initialize the property by using methods is known as Initialization using Method.

Eg-- class Mobile {


void display () {
int i=10;
System.out.println(i);
}
}
public class Demo {
public static void main ( String[] args ) {
Mobile m=new Mobile ();
m.display();
}
}
DATA TYPES

Def :- Data types are basically used in java to describe the data.
CONSTRUCTORS

Def :- Constructor is a member of a class which is also known as Specialized Method.


-->Constructor is mainly used for two purpose Object Creation and Initialization of
Property.
Advantage--> we can declare & initialize the property at the same time
by this we can achieve code optimization.

Types of Constructor-->
DEFAULT CONSTRUCTOR

Def :- When there is no user-defined constructor, compiler will generate a constructor is


known as Default Constructor.

Eg--
class Tesla{ class Tesla{
Tesla (){
}
} }

User View Compiler View


USER- DEFINED CONSTRUCTOR
Def :- When there is constructor which is defined by user is k/a User-Defined Constructor.

Eg--
class pen{ class pen{
Pen(){
Pen(){
}
}
} }

User View Compiler View


‘THIS’ KEYWORD

--> ‘this’ is a reference variable which will act as a Current calling object address.
--> We can’t use this keyword inside main method or any static method.
Eg-- public class Car{
String name;
Car(){
this.name=“Porsche”;
}
public static void main(String[] args){
Car c = new Car();
System.out.println(c.name);
}
}
PARAMATERIZED CONSTRUCTOR
Def :- When there is constructor which is defined by user by using parameter is k/a
Paramaterized Constructor.

Eg--
class Car{
String name;
Car(String n){
this.name=n;
}
public static void main(String[] args){
Car c = new Car(“Volvo”);
System.out.pritnln(c.name);
}
}
NON-PARAMATERIZED CONSTRUCTOR
Def :- When there is constructor which is defined by user with Zero-parameter is k/a Non-
Paramaterized Constructor or Zero-Paramaterized Consrtuctor.

Eg--
public class Car{
String name;
Car(){
this.name=“Porsche”;
}
public static void main(String[] args){
Car c = new Car();
System.out.println(c.name);
}
}
METHOD

Defination :- Methods can be set of instructions to perform some specific tasks.

Syntax :- Void MethodName() {


// instruction
}
Advantage :-
1) Avoid code duplication
2) Achieve code reusability
3) Achieve code readability
4) Modification becomes simple
TYPES OF METHODS

1) Non-Returning Method
Syntax :- Void MethodName() {
// instructions
}

2) Return type Method


Syntax :- datatype MethodName() {
return data ;
}
TYPES OF RETURN-TYPE METHOD

1) Method Returning Data


2) Method Returning Variable
3) Method Returning Parameters
4) Method Returning Expressions
METHOD RETURNING DATA
class Pen {
int display () {
return 10;
}
}
public class Demo {
public static void main ( String[] args ) {
Pen p=new Pen ();
p.display();
}
}
METHOD RETURNING VARIABLE
METHOD RETURNING PARAMETERS
class Pen {
String display (String name) {
return name;
}
}
public class Demo {
public static void main (String[] args) {
Pen p=new Pen ();
String s = p.name(“Abhimanyu”);
System.out.println (s);
}
}
METHOD RETURNING EXPRESSIONS

class Pen {
int Add(int a +int b) {
return int a + int b;
}
}
public class Demo {
public static void main (String[] args) {
Pen p=new Pen ();
int sum = p.Add(100,200);
System.out.println (sum);
}
}
VARIABLES

Variables are nothing but data containers that save data values.

There are two types of variable


1) Global Variable
2) Local Variable
INSTANCE VARIABLE
If the value of a variable is varied from object to object such type of variable is known as
Instance Variable.

class Grass {
int x=10;
Instance Variable
main() {

}
}
STATIC VARIABLE
If the value of a variable is not varied from object to object such type of variable is known as
Static Variable.

class Test {
static int x=10;
Static Variable
main() {

}
}
LOCAL VARIABLE
Sometimes to meet temporary requirements of the program we can declare a variable inside a
method or block or constructor such type of variable are called “Local Variable”.

Test {
Test (){
int x=1000;
}
}
VARIABLE SHADOWING
When Global variable and Local variable having same name inside the member this term is k/a Variable
Shadowing.
Eg--> public class Test{
int x=10;
void show(int x){
System.out.println(x);
System.out.println(x);
}
public static void main(String[] args){
Test t = new Test();
System.out.println(t.x);
}
}
OOPS(OBJECT ORIENTED PROGRAMMING SYSTEM) CONCEPT
INHERITANCE
Def :- It is a process of one class taking properties and behaviour of another class is k/a
Inheritance.

Parent class :- Which give properties and behaviour.


Child class :- Which take properties and behaviour.
--> Inheritance in JAVA can be achieved by using extends and implements keywords.
--> In Java, Inheritance is uni-directional means child can access parent class but parent
class cannot access child class.
Advantage :-
--> We can achieve code reusability, generalization, polymorphism.
--> We can avoid code duplication.
EXAMPLE

class Test{
class Father{
public static void main (String[] args){
Public void m1(){
Father f = new Father();
Sopln(“Father”);
f.m1(); // Valid
}
f.m2(); //Invalid (CE—cannot find symbol)
}
Child c=new Child();
Class Child extends
Father{ c.m1(); //valid

Public void m2(){ c.m2(); //valid

Sopln(“Child”); Father f =new Child();

} f.m1(); //valid
} f.m2(); //Invalid (CE—cannot find symbol)
}
}
COMMON PROPERTIES AND COMMON BEHAVIOUR
SPECIFIC PROPERTIES AND SPECIFIC BEHAVIOUR
Def :- If the Properties and Behaviour present inside the Parent class ,then it is k/a
Common Properties and Common Behaviour.
Def :-If the Properties and Behaviour present inside the Child class ,then it is k/a Specific
Properties and Specific Behaviour.
public class vegetables{ class Carrot extends Vegetables{
//Common Properties Public static void main(String[]
args){
String color;
Vegetables v=new Vegetables();
Int Price;
Void WashVegetables(){}
//Common Behaviour
Void CutVegetables(){}
Void WashVegetables(){}
}
Void CutVegetables(){}
}
}
RESTRICTION WITH RESPECT TO INHERITANCE

Restricted inheritance for selected property and selected behaviour we can achieve this by making
the parent class property and behaviour as private.
Restricting inheritance for all the property and behaviour this can achieved by making the class
final.
SINGLE LEVEL INHERITANCE
When a class take properties and behaviour from one class this type of inheritance can be called
Single level inheritance.
class Object{
}
Object Object
class Student{
}
class Employee{
}

Student Employee
MULTI LEVEL INHERITANCE
When a class take properties and behaviour from more than one class this type of inheritance can
be called Multi level inheritance.
class Amitabh{ Amitab
} h
class Abhishek extends Amitabh{
} Abhishe
class Aradhya extends Abhishek{ k
}

Aradhya
HIERARCHY LEVEL INHERITANCE
When a class have more than one child this type of inheritance can be called Hierarchy level
inheritance.
class Vehicle{
Objec
} t
class Bike extends Vehicle{
}
class Car extends Vehicle{ Vehicle
}

Bike Car
MULTIPLE LEVEL INHERITANCE
When a class have more than one Parent this type of inheritance can be called Multiple level
inheritance.
class A extends class B,C{
}
B C

A
HAS A RELATIONSHIP

has a relationship is also known as composition or aggregation. There is no specific keyword to


implement has a relationship. But most of the time we are depending on “new” keyword.
The main advantages of has a relationship is reusability of code

Class car { Class engine {


engine ee=new engine (); //engine specific functions
} }
COMPOSITION
--> Without existing container object if there is no chance of existing Contained object then container and
contained object strongly associated and this association is nothing but composition or strong has a
relationship.

--> University consists of several dept. without existing university there is no chance of existence of
determent. Hence university department are strongly associated and this strong association nothing but
composition
AGGREGATION

Without existing container object. if there is a chance of existing contained object. Then container
and contained object are weekly associated and this weak association is nothing but aggregation.
Ex: department consists of several professor without existing department. There maybe a chance
of existing professor objects hence determent and professor objects are weekly associated and
this weak association is nothing but aggregation
POLYMORPHISM

 Polymorphism in Java is a concept by which we can perform a single action in different ways

 There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism.

 We can perform polymorphism in java by method overloading and method overriding.


Method Overloading in Java

If a class has multiple methods having same name but different in parameter list, it is known
as Method Overloading.

In Method Overloading Method Resolution Always taken care by Compiler based on Reference
Type.
That’s why Method Overloading is Known as Compile-time Polymorphism

There are two ways to overload the method in java


1.By changing number of arguments
2.By changing the data type

Advantages:
3. Method overloading increases the readability of the program.
4. It reduces execution time because the binding is done in compile time.

In Java, Method Overloading is not possible by changing the return type of the method only.
By changing number of arguments

class Adder{
static int add(int a,int b){
return a+b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
Output:
22
33
By changing the data type

class Adder{
static int add(int a, int b){
return a+b;
}
static double add(double a, double b){
return a+b;
}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}
}

Output:
22
24.9
AUTOMATIC TYPE PROMOTION IN OVERLOADING
One type is automatically promoted to another implicitly if no matching
datatype is found.
Example of Method Overloading with TypePromotion

class OverloadingCalculation1{
void sum(int a,long b){System.out.println(a+b);}
void sum(int a,int b,double c){System.out.println(a+b+c);}

public static void main(String args[]){


OverloadingCalculation1 obj=new OverloadingCalculation1();
obj.sum(20,20);//now second int literal will be promoted to long
obj.sum(20,20,20);
}
}
Output: 40
60.0
Example of Method Overloading with Type Promotion if matching found

If Parent and Child both method argument get matched always priority goes to Child

class OverloadingCalculation2{
void sum(int a,int b){
System.out.println("int arg method invoked");
}
void sum(long a,long b){
System.out.println("long arg method invoked");
}
public static void main(String args[]){
OverloadingCalculation2 obj=new OverloadingCalculation2();
obj.sum(20,20);//now int arg sum() method gets invoked
}
}

Output:
int arg method invoked
Example of Method Overloading with Type Promotion in case of ambiguity

If there are no matching type arguments in the method, and each method
promotes similar number of arguments, there will be ambiguity.

class OverloadingCalculation3{
void sum(int a,long b){
System.out.println("a method invoked");
}
void sum(long a,int b){
System.out.println("b method invoked");
}
public static void main(String args[]){
OverloadingCalculation3 obj=new OverloadingCalculation3();
obj.sum(20,20);//now ambiguity
}
}

Output: Compile Time Error


METHOD OVERRIDING
If subclass (child class) has the same method as declared in the parent class to provide some
specific Implementation, it is known as method overriding in Java.
In Overriding Method Resolution Always taken care by JVM based on Runtime Object .
That’s why Method Overriding is also known as Runtime Polymorphism or Dynamic Polymorphism or
Latebinding.
The Parent class method which is Overriden is called Overriden Method.
The Child class method which is Overriding is called Overriding Method.
Rules for method overriding

1.The method must have the same name as in the parent class
2.The method must have the same parameter as in the parent class.

class Vehicle{
void run(){ //defining a method
System.out.println("Vehicle is running");
}
}
//Creating a child class
class Bike2 extends Vehicle{
void run(){ //defining the same method as in the parent class
System.out.println("Bike is running safely");
}
public static void main(String args[]){
Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}

Output: Bike is running safely


1. Overriding and Access Modifiers

In Method Overriding we cant reduce the scope of access modifiers but we can increase the
scope.

class Parent {
protected void m2(){
System.out.println("From parent m2()");
}
}
class Child extends Parent {
public void m2(){
System.out.println("From child m2()");
}
}
class Main {
public static void main(String[] args){
Parent obj2 = new Child();
obj2.m2();
}
}
Output: From child m2()
2. Final methods can not be overridden

We cant override parent class final method in child class. If we try to override the method we
get compile time error.

// A Java program to demonstrate that


// final methods cannot be overridden

class Parent {
final void show() {} // Can't be overridden

class Child extends Parent {


void show() {} // This would produce error
}

Output : error: show() in Child cannot override show() in Parent void show() { }
^ overridden method is final
3.Static methods can not be overridden
We cannot Override a static method as non-static otherwise we will get compile time error.
Similarly we cannot Override non-static method as static .
If both Parent and Child class methods are static then it is not Overriding it is method hiding.

class P{ class P{ class P{


public static void m1(){ public void m1(){ public static void m1(){

} } }
} } }
class C extends P{ class C extends P{ class C extends P{
public void m1(){ public static void m1(){ public static void m1(){

} } }
} } }
Compile time error Compile time error No error
m1() in C cant override m1() in C cant override Method Hiding
m1() in P m1() in P
Overriden method is Static Overriding method is
Static
4. Private methods cannot be overridden

Private methods cannot be overridden as they are bonded during compile


time. Therefore we can’t even override private methods in a subclass.

class P{
private void m1(){ //Parent class specific method

}
}
class C extends P{
private void m1(){ // Child class specific method

}
}

We get compiler error


5. The overriding method must have the same return type
(orFrom
subtype)
Java 5.0 onwards it is possible to have different return types for an overriding method in the child class, but the child’s return
type should be a sub-type of the parent’s return type. This phenomenon is known as the covariant return type.

class SuperClass {
public Object method(){
System.out.println("This is the method in SuperClass");
return new Object();
}
}
class SubClass extends SuperClass {
public String method(){
System.out.println("This is the method in SubClass");
return "Hello, World!";
}
}
public class Test {
public static void main(String[] args){
SuperClass obj1 = new SuperClass();
obj1.method();
SubClass obj2 = new SubClass();
obj2.method();
}
}
Output : This is the method in SuperClass
This is the method in SubClass
6. Invoking overridden method from sub-
class
We can call the parent class method in the overriding method using the super keyword.
// Base Class // A Java program to demonstrate that overridden
class Parent { // method can be called from sub-class
void show() { System.out.println("Parent's show()"); }
}
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
@Override void show(){
super.show();
System.out.println("Child's show()");
}
}
// Driver class
class Main {
public static void main(String[] args){
Parent obj = new Child();
obj.show();
}
}
Output : Parent's show()
Child's show()
ABSTRACTION

Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Abstract Class :
A class which is declared as abstract is known as an abstract class.

Example of abstract class


abstract class Vehicle{}

 Abstract class is Partially Implemented (incomplete) as we cannot create object for abstract class.
 Any class that contains one or more abstract methods must also be declared with an abstract keyword.
 We can have an abstract class without any abstract method.
 We can define static methods in an abstract class
Abstract Method:
A method which is declared as abstract and does not have implementation is known as an abstract method.
Abstract method representation

abstract void printStatus();//no method body and abstract


Example of Abstract class that has an abstract method

abstract class Bike{ // abstract class


abstract void run(); // abstract method
}
class Honda4 extends Bike{
void run(){
System.out.println("running safely");
}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
Output : running safely
If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child
class as abstract so that the next level Child class should provide implementation to the remaining abstract method.

abstract class Demo { class GFG {


abstract void m1(); public static void main(String[] args){
abstract void m2(); SecondChild s = new SecondChild();
abstract void m3(); s.m1();
} s.m2();
abstract class FirstChild extends Demo { s.m3();
public void m1() { }
System.out.println("Inside m1"); }
} Output : Inside m1
} Inside m2
class SecondChild extends FirstChild { Inside m3
public void m2() {
System.out.println("Inside m2");
}
public void m3() {
System.out.println("Inside m3");
}
}
ENCAPSULATION

The Process of binding the data and corresponding method into a single unit is nothing but encapsulation.

Rules for Encapsulation


 Class must be Public and non-static

 Properties or data members must be private

 For every private properties there must be public getter and setter.

 Parameterized constructor should be avoided


Setter :

The Setter method is used to Write the data as well as we can perform validation.

• All Setter method must be of void type


• All Setter method must contain 1 parameter

Write-Only class
//A Java class which has only setter methods.
public class Student{
private String college; //private data member
public void setCollege(String college){ // setter method for college
this.college=college;
}
}

// Now, you can't get the value of college,now you can only change the value of college data member.

System.out.println(s.getCollege());//Compile Time Error, because there is no such method


Getter :

The Getter method is used to read the data

Getter method must be of return type

Read-Only class
//A Java class which has only getter methods.
public class Student{
//private data member
private String college="AKG";
//getter method for college
public String getCollege(){
return college;
}
}
Output : AKG
Now, you can't change the value of the college data member which is "AKG".
s.setCollege("KITE");//will render compile time error
Tight Encapsulation
A class is said to be tightly encapsulated if and only if, all the data members(variables) of that class is declared as private.

If parent class is not Tightly encapsulated then none of the child will be Tightly Encapsulated

//Tight Encapsulation // No-Tight Encapsulation


class A { class A {
private int x; int x;
} }
class B extends A{ class B extends A{
private int y; private int y;
} }
class C extends A{ class C extends A{
private int z; private int z;
} }
} }
Advantages of Encapsulation:

• We can avoid data mis-handling


• We can achieve data validation
• We can achieve data hiding
• The main advantage of encapsulation is we can achieve Security

Disadvantage of Encapsulation:

• The main disadvantage is it increases length of code and slow down Execution

You might also like