Automation QA Testing Course Content

Java OOPS - Constructor/Inheritance


Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e.
provides data for the object that is why it is known as constructor.

Rules for creating java constructor

There are basically two rules defined for the constructor.
1.   Constructor name must be same as its class name
2.   Constructor must have no explicit return type
3.   A Java constructor cannot be abstract, static, final, and synchronized

Types of java constructors

There are two types of constructors:
1.   Default constructor (no-arg constructor)
2.   Parameterized constructor
java constructor

Java Default Constructor

A constructor that have no parameter is known as default constructor.

Syntax of default constructor:

1.   Accessmodifier  <class_name>(){l+s}  

NOTE:  If there is no constructor in a class, compiler automatically creates a default

constructor.

If a class has default constructor, while creating object for the class no need to
provide any parameter values.
Classname refvar=new classname();

Q) What is the purpose of default constructor?

Default constructor provides the default values to the object like 0, null etc. depending on
 the type.

Java parameterized constructor

A constructor that have parameters is known as parameterized constructor.
2.   Accessmodifier  <class_name>(datatype p1,datatype p2,…){l+s}  

Why use parameterized constructor?

Parameterized constructor is used to provide different values to the distinct objects
If a class has parameterized constructor, while creating object for the class need to provide
 parameter values.
Classname refvar=new classname(p1value,p2value,p3 value);

Constructor Overloading in Java

Constructor overloading is a technique in Java in which a class can have any
number of  constructors that differ in parameter lists and datatype.The compiler differentiates
 these constructors by taking into account the number of parameters in the list and their
 type.

Difference between constructor and method in java

There are many differences between constructors and methods. They are given below.
Java Constructor
Java Method
Constructor is used to initialize the state of an object.
Method is used to expose behaviour of an object.
Constructor must not have return type.
Method must have return type.
Constructor is invoked implicitly.
Method is invoked explicitly.
The java compiler provides a default constructor if you don't have any constructor.
Method is not provided by compiler in any case.
Constructor name must be same as the class name.
Method name may or may
 not be same as class name.

Quiz On Constructor:
1)public class Test{

void Test{
System.out.println("Like ThisVideo");
}

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


}
1)Like ThisVideo
ii)No Output
iii)Exception will be thrown
iv)compilation issue
2)

INHERITANCE:


Inheritance : Inheritance is a mechanism that allows a class to inherit properties and behaviors from another class. It promotes code reusability and establishes a hierarchical relationship between classes. For example, you can have a "Vehicle" class as a base class, and the "Car" class can inherit from it, acquiring common attributes and methods defined in the "Vehicle" class.

The class which inherits the properties of other is known as subclass

(derived class, child class) and the class whose properties are inherited is known as

 superclass (base class, parent class).

Note:Private members(variables,methods) cannot be accessible in child class eventhough

 you applied inheritance.

extends Keyword

extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

Syntax

class Super {
   .....
   .....
}
class Sub extends Super {
   .....
   .....
}

 

Advantages of Inheritance:

1)reusability of the code

2)to achieve runtime polymorphism or method overriding

Types of Inheritance

There are various types of inheritance as demonstrated below


Ø //how to call parent class static members(variable,static methods) in the

child class?

Ans) directly call staticmethodname()/static variablename

Ø if we create object for parent class in inheritance process inside child class main(),you

 cannot access child class members with that object ref

Ø to access the parent and child class properties and methods create object for

child class only

package oopsexamples;

 

public class Employee {

 

          String empName;

          double salary;

          int empId;

         

         

          public void setEmpName(String empName){

                   this.empName=empName;

          }

         

          public String getEmpName(){

                   return empName;

          }

         

          public void setSalary(double salary){

                   this.salary=salary;

          }

         

          public double getSalary(){

                   return salary;

          }

         

          public void setEmpId(int id){

                   empId=id;

                  

          }

          public int getEmpId(){

                   return empId;

          }

         

          public static void mailCheck(){

                   System.out.println("salary is credited");

          }

         

}

package oopsexamples;

 

public class Manager extends Employee {

 double bonus=10000.566;

 

public void manageTeam(){

System.out.println(“he manages the team”);

}

         

          public static void main(String[] args) {

                   Manager m=new Manager();

                   m.setEmpId(3456);

                   m.setEmpName("Rahul");

                   m.setSalary(237462374.234234);

                   System.out.println("em[ployee name is:"+m.getEmpName());

                   System.out.println("em[ployee id is:"+m.getEmpId());

                   System.out.println("em[ployee salary is:"+m.getSalary());

                   mailCheck();

                   System.out.println("bonus for the manager is:"+m.bonus);

          }

 

}

 

NOTE:private methods and variables are not accessible in child class eventhough inherited

-----------------------------------------------------------------------------------------------------------















No comments:

Post a Comment

Note: Only a member of this blog may post a comment.