0% found this document useful (0 votes)
16 views18 pages

Rest Topic Notes

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

Rest Topic Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

*DIFFERENCE BETWEEN STACK AREA AND HEAP AREA

Stack Area Heap Area


• All local members and • Object is created at
static member are loaded runtime dynamically
in Stack Area. inside Heap Area and all
• Main method is executed the non-static members
inside stack area. are loaded inside object.
• Stack Area member will • Object is created
have short life. • Heap area member will
• Stack Area members have long life.
provide faster execution. • Heap area members are
slow in execution.

*CONSTRUCTOR*

Constructor is a block of statement which is used to load all non static members inside the object.
Along-with loading all non static members it is used to initialize the non static members inside
object.

Points:
• The name of constructor must be same as the class name.
• A constructor can have access modifier.
• A constructor cannot have non-access modifier. So a constructor can’t be static, final,
abstract or synchronized.
• A constructor cannot have return type.
• A constructor can accept different arguments at its parameter.
• A constructor call always takes place together with new keyword.
Emmployee e1 = new Emmployee()
*TYPES OF CONSTRUCTOR*

1. Default Constructor
2. User-defined constructor
2.1. No Argument Constructor
2.2. Parameterized Constructor

*1. Default Constructor*

class Emmployee Javac Emmployee.java after compilation


{ class Emmployee
String name ; {
int salary; String name ;
int salary;
public void work() Emmployee()<=is added by compiler as default constructor
{ {

} }
} public void work()
{

}
class }
EmmployeeDriver
{

public static void main(String[] args)


{
Emmployee e1 - new Emmployee();
}
}

A constructor which is added by compiler at compling time when there is no constructor designed
by programmer then it is called Default Constructor. It is added only when there is no user defined
constructor in the program. Default constructor help in object creation when there is no constructor
designed by programmer.

*NO ARGUMENT CONSTRUCTOR*

If a user defined constructor takes no argument at its parameter, then it is called No Argument
constructor.

******* photo of code**********


class Emmployee
{
String name;
int eid;
double salary;
Emmployee()
{
System.out.println("Emmployee object is created");
}

}
class EmmployeeDriver
{
public public static void main (String[] args) {
Emmployee E1 = new Emmployee();
Emmployee E2 = new
Emmployee(); Emmployee
E3 = new Emmployee();
Emmployee E4 = new
Emmployee();
}
}

*PARAMETER CONSTRUCTOR*

If a user defined construtor takes different arguments at its parameter then such cosntructor is called
Parameterized Construtor. Parameterized constructor is useds to load and initialize all non-static
member inside object.

class Emmployee
{
String name;
int eid;
double salary;
Emmployee(String name, int eid, double salary)
{
this.name = name;
this.eid = eid;
this.salary=salary;
}
public void displayEmmployee()
{
System.out.println("The name : " +this.name);
System.out.println("The eid : " +this.eid);
System.out.println("The salary : " +this.salary);
}
}

class EmployeeDriver
{
public static void main(String[] args)
{
Employee E1 = new Employee("Akshit", 101,
23992.2);
E1.displayEmployee();
}
}
^This Keyword^

This is a keywords which is used to refer global non static members of the same class. This
keyword holds the reference of current object under execution

//this keyword example


class Emmployee
{
String name;
int eid;
double salary;
Emmployee(String name, int eid, double salary)
{
this.name = name;
this.eid = eid;
this.salary = salary;
}
public void displayEmmployee();
{
System.out.println("name is: "+this.name);
System.out.println("eid is: "+this.eid);
System.out.println("salary is: "+this.salary);
}
}

class EmmployeeDriver
{
public static void main(String[] args)
{
Emmployee e1 = new Emmployee();
}
}

*Constructor OverLoading*

Constructor Over Loading where we can have multiple constructors inside same class with different
parameters. We can achieve constructor over loading in three different ways.

1. By changing the number of parameters at constructor.


Ex.
Dog(String name)
{
this.name=name;
}
Dog(String name, Double height)
{
this.name= name;
this.height= height;
}
2. By changing the data type of parameter at constructor.
Ex.
Dog(String name)
{
this.name=name;
}
Dog(double height)
{
this.height=height;
}

3. By changing the sequence of parameters at constructor.


Ex.
Dog(String name, double height)
{
this.name=name;
this.height=height;
}
Dog(double height, String name)
{
this.height=height;
this.name=name;
}

*Copy Constructor*

// prefer class programs :

Copy Constructor is a special type of constructor which is used to copy details of first constructor
inside another constructor.
Ex.
//Copy Constructor example
class Emmployee
{
String name;
int eid;
double salary;
Emmployee(String name, int eid, double salary)
{
this.name = name;
this.eid = eid;
this.salary = salary;
}
//copy constructor logic
Emmployee(Emmployee e)
{
this.name = e.name;
this.eid = e.eid;
this.salary = e.salary;
}
public void displayEmmployee();
{
System.out.println("name is: "+this.name);
System.out.println("eid is: "+this.eid);
System.out.println("salary is: "+this.salary);
}
}

class EmmployeeDriver
{
public static void main(String[] args)
{
Emmployee e1 = new Emmployee();
Emmployee e2 = new Emmployee(e1); //copy constructor object call
}
}

*this() statement*
This call statement is used to call constructors of the same class.

Syntax:

this(varName1, varName2, ...... );

*Constructor Chaining*
Constructor chaining is a process to call one constructor from inside another constructor. Construtor
chaining is achieved with the help of this() statement. Inside constructor the constructor call must
be the first statement. So, this() call must be the first statement inside the constructor.

Example:
class Emmployee{

String name;
int eid;
double salary;
int age;

Emmployee(){
System.out.println("Emmployee object is created");
}
Emmployee(String
name){this();
this.name=name;
}
Emmployee(String name, int
eid){this(name);
this.eid=eid;
}
Emmployee(String name, int eid, double salary){
this(name, eid);
this.salary=salary;
}
Emmployee(String name, int eid, double salary, int age){
this(name, eid, salary);
this.age=age;
}
}

class EmmployeeDriver {

public static void main(String[] args) {


Emmployee e1=new Emmployee("Mohan",101,58326.5,23)
}
}

IMPORTANT QUESTIONS

Question. What is Constructor?


Question. What is differnce between method and constructor?
Question. What is default constructor?
Question. Constructor overloading
Question. Copy Constructor
Question. This keywords
Question. this() Statement
Question. Constructor Chaining

*METHOD OVERLOADING*

If in a class there are multiple methods with same name and different parameters. Then it is called
method overloading. In method overloading Non Access Modifier and Return type does not play
any role.

Different ways to achieve Method Overloading:

1. By changing the number of parameter.

Ex.
Public static void test(int a)
{

}
public static void test(int a, int b)
{

2. By changing the type of parameter.

Ex.
Public static void test(int a)
{
}
public static void test(double a)
{

3. By changing the sequence of parameter.

Ex.
Public static void test(int a, double b)
{

}
public static void test(double a, int b)
{

Important
Questions. Can we overload main method in JAVA or not ?

Answer. Yes, We can have multiple main methods in JAVA with different parameters.
* At the time of execution only one main method will execute where parameter is String[] args.
Other main method will only execution when it is called inside main method with parameter
String[] args.

// Refer classroom program:


*Relationships*

The connection between two classes or between two objects is called Relationship. It is of two
types :

1. HAS-A Relationship
2. IS-A Relationship(Inheritance)

^HAS-A Relationship^

HAS-A Relationship is used to represent a relationship which may be completely dependent or


partially dependent. HAS-A Relationship is of two types:

1. Composition

If a HAS-A Releationship is a type of relationship where one object is completely dependent


on another object. The dependent object cannot exist without main object.
Example:
Car HAS-A Engine
Mobile HAS-A Screen
Laptop HAS-A Processor
Tree HAS-A Fruit
Examples:

2.Aggregation

If HAS-A Relationship is a week relationship where one object can exist without another
object then such relationship is called Aggregation.
Example:k
Student HAS-A Address
Mobile HAS-A Cover
Car HAS-A Music Player
NON-PRIMITIVE TYPE CASTING

Converting a non-primitive data into another non primitive data is called Non-Primitive Type
Casting. To achieve non primitive type casting there must be Parent child relation ship or IS-A
relationship. It is of two types:

1. UpCasting or Generalization
2. DownCasting or Specialization

^UpCasting or Generalization^

UpCasting is a proccess to convert or store a child type object on parent reference.


In other words if a child object is stored on parent type reference then it is called upcasting. On
upcasting we can access only parent member and child members can not be accessed.

Examples

DownCasting or Specialization^

Converting parent type refernce into child type is called DownCasting or Specialization. On
downcasting we can access parent members as well as child members. To achieve downcasting
there should be upcasting.

Over riding with upcasting is Very Important


*Method OverRiding*

When in parent class & child class there are non static methods with same parameter and same
return type then it is called Method Overriding.
In Method overriding execution of method depends on the type of object created.
The output will change at the runtime as we give refernce of the parent class to child class objects.

Method OverRiding concept is used to change the parent implementation from child class.
*Polymorphism*
If a member behaves differently with the same name then it is called Polymorphism.
Same / One Variable = Different Value
Same / One Method = Different Implementation
Same / One Object = Different Behaviour
Meaning: poly = many, morphism = forms.

*Polymorphism is of two types*

1. Compile Time Polymorphism / Static Polymorphism


2. Run Time Polymorphism / Dynamic Polymorphism

^Compile Time Polymorphism / Static Polymorphism^

If the implementation of the member is decided by compiler and same gets executed then is called
Compile Time Polymorphism. What compiler Binds or sees = same gets executed.
We can achieve Compile Time Polymorphism by:

1. Method Overloading
2. Constructor Overloading
3. Method Shadowing
4. Variable Shadowing
5. Operator Overloading (Operator overloading is not supported in JAVA).

^Run Time Polymorphism / Static Polymorphism^

When the binding done by compiler at compile-time can be changed at run-time by JVM and
implementation is provided by JVM then it is called Run Time Polymorphism. What compiler
Binds or sees = same may not get executed.
Different ways to achieve Run Time Polymorphism :

1. Method OverRiding
2. UpCasting
Question1. Why JAVA does not support operator overloading?

Answer. In JAVA at the place of operator overloading there is a more powerful concept called
Method Overloading. If Operator Overloading was allowed then it will only increase the complexity
of the code, So Operator Overloading is not allowed in JAVA.

*Method Shadowing*
If in parent class and in child class there are static methods with the same name, same return type and
same parameter then it is called Method Shadowing. In method shadowing execution of the method
depends on the type of reference provided and it does not depend on the type of object created (unlike
method overriding).

*Variable Shadowing*
If in parent class and in child class there are variables with the same name either static or non static
then it is called Variable Shadowing. In Variable Shadowing implementation directly depends on
type of reference provided and it does not depend on the type of object created.

Question. Can we Override main method in JAVA or not?


Answer. Method over riding concept is aplicable only for non static methods in parent class and in
child class. As the main method of JAVA is a static method. So method overriding concept is not
applicable for main method.

Note :
1 Compile time polymorphism : Exectuion depends on the reference provided and it does’nt
depend on the type of object created.
2. Run time Polymorphism : Execution depends on the type of object created and it doesn’t
depend on the type of reference provided.
*Abstraction *
Abstraction in JAVA is a mechanism to hide the implementation details and showing only
functionality. In other words abstraction is a mechanism to hide how it does from user and show
what it does to user.

Abstraction in JAVA can be achieved in two ways:

1. By using Abstract class(0% to 100% abstraction).


2. By using interface(100% abstraction).

*Abstract Class*
If a class in JAVA is delcared with the keyword Abstract then such class is called an Abstract Class
syntax:

abstract class vehicle


{
//abstract class
}

Points :
1. If a class is abstract then we cannot create any object of that class.
Vehice v1 = new Vehicle();
2. We cannot create object but we can use reference of an abstract class.
3. Inside an abstract class we can have abstract methods as well as concrete method.

3.1 *Abstract Method*

If a method in JAVA is declared with the keyword abstract then such method is called Abstract
Method. An Abstract method is only allowed inside an abstract class. An abstract method do not
spacify any method body. A static method cannot be abstract. Only a non static method will be
abstract because abstaction is achieved through non static methods.

Example:
abstract class Vehicle
{

public abstract void start();


public abstract void drive();

3.2 *Concrete Method*

If a method in JAVA is not declared with the keyword abstract then it is called concrete method. A
concrete method will always have the method body.
Example
public void stop();
{
sop(“stop the vehicle”);
}
public void accelerate()
{
sop(“accelerate the vehicle”);
}

Percentage of Abstraction : Number of abstract method/total number of methods * 100%

4. A abtract class can extend another abstract class.

5. In the end there should be a child class of abstract class which will extend abstract class. To provide
method body to all the abstract methods with the help of concept method over-riding. This class must
be a normal class for which object will be also created.
*Interface*

Interface is a mechanism in JAVA to achieve complete abstraction of 100% abstraction.

Ques. How to have interface?


Ans. We can have interface by using keyword Interface.

Syntax:
interface Fruit
{
//interface
}

Interface is also a blueprint like class but it have different properties then class.

Points: -
1.Interface in JAVA is a blueprint same as class but it has different characteristics then a class.
2.A class is declared with the keyword class but an interface is declared with the keyword Interface.
3. We cannot create an object of an interface.
4. Inside an interface we cannot have constructor.
5. Inside an interface a variable is always by default public static and final type variable.
6. Inside interface a non static method is always by default abstract.

7. Since JAVA 8.0 an interface can have static and default method.
8. Since JAVA 9.0 and interface can have private methods. (non static methods cannot be private).
Only a static method can be private. The non static method cannon be private because it has to be
visible to other class so that it can be overridden.
9. An interface represents IS-A relationship where one interface can extend another interface.
10. An interface cannot be a child of a class.
In the end there should be a class which implements an interface to provide body to all the abstract methods
of interface. A class always implements when interface.

11.One interface can be implemented by many child class.

Question. How multiple inheritance is supported in JAVA in case of interface ?


Answer. In case of interface , interface can have only abstract type method. A single class can implement multiple
interface so that the implementation provided by class will be common tto all the interfaces and in this manner
ambiguity problem is resolved and interface allows multiple inheritance.
*Some Special types of Interface:-

1. Functional Interface: If an interface has only one abstract method then it is called functional
interface. It can have many static methods but abstract method should be only one. Example:
Runable interface : It is an example of functional interface which has only one abstract method
called run.

Interface Runnable
{
public void run();
}

This interface is used to create threads.

Questions. How many ways are there to create a thread in java?


Find answer online.

2. Marker/Tagged Interface : If an interface has no member then it is called Marker or Tagged


Interface.
In other words an empty interface is called Marker or Tagged Inteface. Example : Clone-able and
Serializ-able Interface are the example of Marker Interface it is used to pass some important pass
some important message to JVM.
*Final Keyword*

Final is a keyword in JAVA which is used to restrict modification of a class member. Final can be:
1. A Variable
2. A Method
3. A Class

1. Final Variable : If a variable in java is declared with the final keyword then such variable is
called a Final Variable. Once a final variable is assigned then we cannot re-asign it again.
Ex
Final int X = 35;
X = 60; //Error can’t reassign x.

If a final variable is only declared then it is called Blank Final Variable. A blank final variable can
be assigned only once. Blank final variable can be only local variable. Global varibale cannot be a
blank final variable.
Ex.
Final int X;//Upper case convention for a final variable.
X = 60;//can only be assigned once

A final type varibale can be inherited from parent class to child class. Final type variable should be
uppercase

*Final Method*
If a method in Java is declared with keyword final then such method is called final method.

public final static void test()


{
sop(“this is a final method”);
}

Properties
1. A final method can be overloaded in same class.
2. A final method can be inherited from parent class to child class.
3. A final method cannot be overridden from child class.

*Final Class*
If a class in java is declared with the keyword final then such class is called a final class.
If a class is final then it cannot have any child class and a final class can not be inherited from a
child class.
Final class test
{

You might also like