Rest Topic Notes
Rest Topic Notes
*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
} }
} public void work()
{
}
class }
EmmployeeDriver
{
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.
If a user defined constructor takes no argument at its parameter, then it is called No Argument
constructor.
}
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
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.
*Copy Constructor*
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:
*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 {
IMPORTANT QUESTIONS
*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.
Ex.
Public static void test(int a)
{
}
public static void test(int a, int b)
{
Ex.
Public static void test(int a)
{
}
public static void test(double a)
{
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.
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^
1. Composition
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^
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.
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.
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).
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.
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.
*Abstract Class*
If a class in JAVA is delcared with the keyword Abstract then such class is called an Abstract Class
syntax:
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.
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
{
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”);
}
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*
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.
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();
}
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.
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
{