Вы находитесь на странице: 1из 13

Constructors in Java

Presentation by: BCA-5

Content
Introduction and features of Constructors Constructor Overloading Constructors in Inheritance(Overriding)

Constructors
Classes should define one or more methods to create or construct instances of the class Their name is the same as the class name Constructors are differentiated by the number and types of their arguments An example of overloading If you dont define a constructor, a default one will be created.
3

Constructors
1. You need not code them explicitly. Java will automatically place a default constructor 2. You can pass arguments to the constructor 3. They can return only an object of type of that class 4. They can be made private 5. They would be executed always

Constructors
They do not have return type(not even void). A constructor is executed when an object of the class is created.

Example of Constructor
class Demo { int value1; int value2; Demo(){ value1 = 10; value2 = 20; System.out.println("Inside Constructor"); }

public void display(){


System.out.println("Value1 === "+value1); System.out.println("Value2 === "+value2); } public static void main(String args[]){ Demo d1 = new Demo(); d1.display(); } }

Constructor Overloading
Constructor overloading in java allows to have more than one constructor inside one Class In Constructor overloading you have multiple constructor with different signature This is another form of polymorphism in Java. Once you provide constructor on Class in Java , Compiler will not insert or add default no argument constructor

Constructor Overloading
Constructor overloading is similar to method overloading in Java. You can call overloaded constructor by using this() keyword in Java. Make sure you add no argument default constructor because the compiler will not add if you have added any constructor in Java.

Example of Constructor Overloading


//cube class containing 2 constructors
public class Cube1 { int length; int breadth; int height; public int getVolume() { return (length * breadth * height); } Cube1() { length = 10; breadth = 10; height = 10; } Cube1(int l, int b, int h) { length = l; breadth = b; height = h; } public static void main(String[] args) { Cube1 Obj1, Obj2; Obj1 = new Cube1(); Obj2 = new Cube1(10, 20, 30); System.out.println("Volume of Cube1 is : " + Obj1.getVolume()); System.out.println("Volume of Cube1 is : " + Obj2.getVolume()); } }

Constructors in Inheritance
Inheritance is the mechanism through which we can derive classes from other classes. The derived class is called as child class or the subclass or we can say the extended class and the class from which we are deriving the subclass is called the base class or the parent class.

10

Constructors in Inheritance
When we want to call Super Class Constructor from the sub class, then we need to use the super keyword The super is java keyword. As the name suggest super is used to access the members of the super class. It is used in method overriding as super.methodname();
11

Constructors in Inheritance
The second use of the keyword super in java is to call super class constructor in the subclass. This functionality can be achieved just by using the following command. super(param-list); Similar to method overriding, this is constructor overriding. super needs to be the first line of code in the constructor of the child class.
12

Constructors in Inheritance
class A { int a; int b; int c; A(int p, int q, int r) { a=p; b=q; c=r; } } class B extends A { int d; B(int l, int m, int n, int o) { super(l,m,n); d=o; } void Show(){ System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } public static void main(String args[]){ B b = new B(4,3,8,7); b.Show(); } }

13

Вам также может понравиться