Rules
for creating java constructor
Types of
java constructors
Java
Default Constructor
A constructor that
have no parameter is known as default constructor.
|
Syntax
of default constructor:
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?
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
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)Like ThisVideo
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);
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.