Java Presentation
Java Presentation
What is Java?
Java is an Object Oriented Platform Independent Highlevel Programming Language.
2) Behaviour ( Method )
represents the functionalities performed by an object.
example public void m1(){
//implementation
}
KEYWORDS
Members -->
1) State
2) Behaviour
3) Constuctor
4) Blocks
5) Main method
IDENTIFIER RULES
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.
Def :- Data types are basically used in java to describe the data.
CONSTRUCTORS
Types of Constructor-->
DEFAULT CONSTRUCTOR
Eg--
class Tesla{ class Tesla{
Tesla (){
}
} }
Eg--
class pen{ class pen{
Pen(){
Pen(){
}
}
} }
--> ‘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
1) Non-Returning Method
Syntax :- Void MethodName() {
// instructions
}
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.
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.
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
} 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
--> 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.
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
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);}
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
}
}
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
}
}
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.
class Parent {
final void show() {} // Can't be overridden
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 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
class P{
private void m1(){ //Parent class specific method
}
}
class C extends P{
private void m1(){ // Child class specific method
}
}
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.
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
The Process of binding the data and corresponding method into a single unit is nothing but encapsulation.
For every private properties there must be public getter and setter.
The Setter method is used to Write the data as well as we can perform validation.
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.
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
Disadvantage of Encapsulation:
• The main disadvantage is it increases length of code and slow down Execution