0% found this document useful (0 votes)
11 views

JAVA

The document discusses various Java programming concepts like flow control statements, arrays, variables, data types, classes and objects, inheritance, polymorphism, abstraction, encapsulation and exception handling.

Uploaded by

jaswanthk3280
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

JAVA

The document discusses various Java programming concepts like flow control statements, arrays, variables, data types, classes and objects, inheritance, polymorphism, abstraction, encapsulation and exception handling.

Uploaded by

jaswanthk3280
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA

Flow control Statements


1. Selection --> If, if else, if else if, Switch
2. Iterative --> While, Do while, For Loop, For each loop
3. Transfer --> Break, Continue, return

ARRAY
-->Single Dimensional
-->Two Dimensional
Three Dimensional

Declare - Create - Assign - Access

int a[]=new int[]; b<--> int[] a=new int[];

a.length = Size of array


ArrayIndexOutOfBoundException

int[][] a= new int[][];

public class Name{


public static void main(String[] args){

public static void Meth(){


sysout("METHOD");
}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

VARIABLES --> Local, Instance, Static

DATATYPES
1. Primitive --> int, float, double, long, char, short, byte, boolean
2. Non-Primitive --> Classes, Interface, Arrays

FOR LOOP -> for(variable; condition; change)


statement

WHILE LOOP ->


int i=1;
while(i<=5)
statement;
i++;

***********************************************************************************
***********************

CLASSES & OBJECTS

Classes --> enclosed Variables(to store DATA) & Methods(to store Programming Logic)

-->
class

static int a=; // Static Variable


int b=; // Instance Variable
int c=; // Instance Variable

main
int x=; // Local Variable
int y=; // Local Variable
int sum=x+y+a;
SOP(sum);

--> Instance & Static variables which outside the methods are also known as GLOBAL
Variables

--> Static methods can only access the static stuff --> Main Method is related to
static

<--> Class is a template for creating objects(Real world entities)

-->TEMPLATE
class Car

String color;
String model;
int milage;
int cost;

public void startCar() //Non-Static --> from non-static we can access both static &
non-static
SOP(model+" ");

public void stopCar() //Non-Static


SOP(model+ " ");

public void carDetails()


SOP("color of car is "+color);
SOP("Model of car is "+model);
SOP("milage of car is "+milage);
SOP("cost of car is "+cost);

-->
class Demo
Main method

Car BMW = new Car();


Car AUDI = new Car();// Object creation statement - BMW --> Object reference//car
Class Type - new --> keyword

// Initializing the Objects


BMW.color = "Black";
BMW.model = "A Model";
BMW.milage = 21;
BMW.cost = 2500000;

AUDI.color = "RED";
AUDI.model = "S model";
AUDI.milage = 17;
AUDI.cost = 1900000;

//Access the methods


BMW.startCar();
BMW.stopCar();
BMW.carDetails();

SOP("____________________");

AUDI.startCar();
AUDI.stopCar();
AUDI.carDetails();

SOP("____________________");

--> STRING is a pre-defined class type

.equals()
.length()

###################################################################################
#################################################

CONSTRUCTORS <==> METHODS

Methods - their OWN names


Constructors - similar to class names

class Car

String color;
String model;
int milage;
int cost;

public Car(String clr, String mod, int mil, int ct)


color=clr;
mod=model;
milage-mil;
cost=ct;

public void startCar()


SOP(model+" ");

public void stopCar() //Non-Static


SOP(model+ " ");

public void carDetails()


SOP("color of car is "+color);
SOP("Model of car is "+model);
SOP("milage of car is "+milage);
SOP("cost of car is "+cost);

-->
class Demo

Main method

Car BMW = new Car("BLACK", "A MODEL", 21, 2500000 );


Car AUDI = new Car("RED", "S Model", 18, 170000);
Car HONDA = new Car("BLUE", "CITY", 15, 900000);

BMW.startCar();
BMW.stopCar();
BMW.carDetails();

SOP("____________________");

AUDI.startCar();
AUDI.stopCar();
AUDI.carDetails();

SOP("____________________");

HONDA.startCar();
HONDA.stopCar();
HONDA.carDetails();

SOP("____________________");

this keyword --> when ISNTANCE variable & PARAMETER have same keyword, THIS keyword
is used to differentiate

OVERLOADING --> It is possible to create the multiple methods with the same name in
the class

METHOD OVERLOADING/CONSTRUCTOR OVERLOADING - methods differ in terms of --> Number


of parameters - Different type parameter - Order of parameter

ERROR --> Duplicate Method


INHERITANCE - is a mechanism in which one class(child) acquires the properties of
another class(parent).

--> PURPOSE - to use the properties(Variables & Methods) from one class to another
class

extends - Keyword

public class Pulp //Parent CLASS

String Vincent;
String Pitt;

public void Car() //Method1


SOP(Vincent+" is driving");

public void Gun() //Method2


SOP(Pitt+" is shooting");

public class Fiction extends Pulp //Child CLASS

String QT;

public void film()


SOP(QT+" is directior");

public class Demo

main method

Pulp p = new Pulp();


p.Vincent = "Vega";
p.Pitt = "Brad";

p.Car();
p.Gun();

SOP("___________________________");

Fiction f = new Fiction();


f.QT = "Quentin Tarantino";
f.Vincent = "Actor1";
f.Pitt = "Actor2";

f.film();
f.Car();
f.Gun();

SOP("___________________________");

MODIFIERS - Combinations of Class, Variables, Methods -- Modifiy the behaviou of


classes, varibles & methods

1. Access Modifiers(4) - public, private, protected, default


2. Non-Access Modifiers(3) - Static, Final, Abstract

--> INTERFACES - No implementation, only layout or structure --> implements

ABSTRACT Class - Partial implementation of methods.

-->
public interface Bank //Interface

String ACCOUNTONE = "SAVINGS";


String ACCOUNTTWO = "CURRENT";

public void bankBalance();


public void accountCharges();
public void loans();

-->
public class HDFC implements Bank // implements

public void bankBalance()

public void accountCharges()

public void loans()

public void loanInterest()

-->
main method

Bank b = new HDFC();

b.bankBalance();
b.accountCharges();
b.loans();

--> EXCEPTION HANDLING - Errors which are not idetified during compile time and
occur while the code is being executed.

--> try catch blocks - to handle the exception handling

-->
main method

SOP("BEFORE");

try{
int a=10/0;
}catch(Exception e){
SOP("Handled");
}

SOP("AFTER");

###################################################################################
###################################################################################
############

OOPS --> Class, Methods, Objects(Definition, Different ways, Creating, program)

OOPS - Object Oriented Programming System/Structure


OOP is a programming methodology.
--> CLASS, OBJECTS, METHODS, INHERTANCE, POLYMORPHISM, ABSTRACTION, ENCAPSULATION.

&&&&&&&&&&&&&&&&&&&& CLASS &&&&&&&&&&&&&&&&&&&&&

1. Class is the collection of objects


2. Class is not a real world entity, it is just a template/blueprint.
3. Class does not occupy memory.

--> access modifier class(keyword) ClassName


{
methods
constructors
fields
blocks
Nested class
}

******************************METHODS*******************************

1. set of codes which perform a particular task


a. Code re-useability
b. code optimization

--> access modifier(public) return-type(void) methodname(list of parameters)

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ OBJECTS$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

1. object is an instance of class.


2. object is a real world entity.
3. object occupies memory.
4. Object consists of
a. Identity(Name)
b. State/Attribute(Color, Breed)
c. Behavior(Eat, run, bark, sleep)
5. How :- new keyword, newInstance method()

a. DECLARATION --> Animal choco


b. INSTANTATION --> choco = new
c. INITIALIZATION --> choco = new Animal();

--> Animal choco = new Animal();

class Animal
{
public void eat()
{
SOP("Dog Eating");
}

main method()
{
Animal choco = new Animal();
choco.eat();
choco.run();
}
public void run()
{
SOP("Dog Running");
}
}

1. By Reference variable
2. By Method
3. By Constructor -->
is a block similar to method having same as class name
Does not have any return type, not even void
The only modifiers applicable for constructor are public, protected, default
& private
It executes automatically when we create an object
used to initialize the object

class Employee
{
String name;
int emp_id;

Employee(String name, int emp_id)


{
this.name=name;
this.emp_id=emp_id;
}

main method
Employee e1 = new Employee("ALPHA", 100);

Employee e2 = new Employee("BETA", 101);

a. DEFAULT
b. NO-AURGUMENT
c. PARAMETRIZED

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< INHERITANCE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


Inheriting the properties of parent class into child class

class Animal
{
void eat(){
SOP("EATING");}
}
class Dog extends Animal
{
main method{
Dog d = new Dog();
d.eat(); }
}
// Animal - Parent class
// Dog - child class

TYPES
--> a. Single Inheritance
--> b. Multi Level Inheritance
--> c. Hierarichal Inheritance
XX d. Multiple Inheritance
XX e. Hybrid Inheritance

>>>>>>>>>>>>>>>>>>>>>>>>>>>>> POLYMORPHISM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

POLY - Many --> MORPH - Forms


a. Compile-Time(STATIC) - Method Overloading
b. Run-Time(DYNAMIC) - Method Overriding

OOPS --> Class, Objects&Methods, Inheritance, Polymorphism

Abstraction
Data Hiding
Encapsulation
Tight coupled classes

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ABSTRACTION
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Types
Abstract Class - can achieve 0-100%
Interface - can achieve 100%

1. a method without body is known as Abstract class


2. a method always be declared in an abstract class
3. we cant create object in abstract class

abstract class vehicle{


abstract void start();
}

class car extends Vehicle{


void start(){
SOp("Car starts with key");
}
}

class Scooter extends Vehicle{


void start(){
SOP("Scooter starts with Kick");
}
main method {
car c = new car();
c.start();

Scotter S = new Scooter();


S.start();

INTERFACE

1. Interface was similar to abstract class


2. Interfaces are the blueprint of the class
3 It supports multiple inheritance.

interface Interface name

methods//abstract

fields

interface i1{
public void show();
}
class Test implements I1{
public void show(){
SOP("1");}
}

main method{
Test t = new Test();
t.show();
}

You might also like