PART2 - Examples
PART2 - Examples
Constructor Method
A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar
to a method. However, constructors have no explicit return type. Typically, you will use a constructor to give
initial values to the instance variables defined by the class, or to perform any other startup procedures required
to create a fully formed object. All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member variables to zero. However, once you
define your own constructor, the default constructor is no longer used.
Shortly, for creating a java constructor; there are basically two rules defined for the constructor.
Default constructor (no-arg constructor): that is used to provides the default values to the object like 0, null etc.
depending on the type.
Parameterized constructor: that is used to provide different values to the distinct objects
Ex: Write a java program that re-creating the no-arg constructor in the Bike class. It will be invoked at the time
of object creation.
Solution:
class Bike1{
Bike1(){System.out.println("Bike is created");}
Run:
Bike is created
Ex: Write a java program that display the name and id of the student via creating a default constructor which
displays the default values of the student id and his/her name.
Soultion:
class Student3{
int id;
String name;
s1.display();
s2.display();
Run;
0 nul
0 null
Ex: Write a java program that created the constructor of Student class that have two parameters student name
and id.
Solution:
class Student4{
int id;
String name;
id = i;
name = n; }
s1.display();
s2.display();
} }
2
Run:
111 Karan
222 Aryan
Ex: Display the student information using the constuctore method with parameters.
Solution:
class Student5{
int id;
String name;
int age;
id = i;
name = n;
id = i;
name = n;
age=a;
s1.display();
s2.display();
Run:
3
111 Karan 0
222 Aryan 25
Ex: write a java program that defines a simple constructor without parameter then call the constructor in the
main class to initialize the object.
Solution:
// A simple constructor.
class MyClass {
int x;
MyClass() {
x = 10; } }
} }
Run:
10 10
Ex: write a java program that defines a simple constructor with a parameter, then call the constructor in the main
class to initialize the object.
Solution:
// A simple constructor.
class MyClass {
int x;
MyClass(int i ) {
x = i;
4
} }
} }
Run:
10 20
Ex: Write a java program that created an instance variable named count which is incremented in the constructor,
without using static variable for variable count.
Solution:
class Counter{
Counter(){
count++;
System.out.println(count);
} }
Run:
5
Ex: Write a java program that created an instance variable named count which is incremented in the constructor,
using static variable for variable count.
Solution:
class Counter2{
static int count=0;//will get memory only once and retain its value
Counter2(){
count++;
System.out.println(count);
Run:
6
Inheritance
Ex: Write a java program that Programmer is the subclass and Employee is the superclass. Relationship between
two classes is Programmer IS-A Employee.
Solution:
class Employee{
float salary=40000;
int bonus=10000;
Run:
Ex:
Slution:
class Vehicle{
int speed=50;
int speed=100;
7
void display(){
b.display();
Run:
50
EX: The super keyword can also be used to invoke the parent class constructor as given below:
Solution:
class Vehicle{
Vehicle(){System.out.println("Vehicle is created");}
Bike5(){
System.out.println("Bike is created");
} }
Run:
Vehicle is created
Bike is created
8
Ex: Student and Person both classes have message() method if we call message() method from Student
class, it will call the message() method of Student class not of Person class because priority is given to
local. When, the super keyword can also be used to invoke parent class method. It should be used in case
subclass contains the same method as parent class as in the example given below
Solution:
class Person{
void message(){System.out.println("welcome");}
void display(){
s.display();
}}
Run:
welcome to java
welcome
Ex: In the example given below message() method is invoked from the Student class but Student class does not
have message() method, so you can directly call message() method.
Solution:
class Person{
void message(){System.out.println("welcome");}
void display(){
9
message();//will invoke parent class message() method
s.display(); } }
Run:
welcome
Ex: Write a java program that uses this keyword to access the members of a class.
Solution:
This_Example() {
This_Example(int num) {
this();
this.num = num;
System.out.println("Hi Welcome");
this.greet();
obj1.print();
obj2.print();
Run:
Hi Welcome to Tutorialspoint
Hi Welcome
11
Ex:
Solution:
class Student11{
int id;
String name;
this.id = id;
this.name = name;
void display()
{System.out.println(id+" "+name);}
s1.display();
s2.display();
} }
12