0% found this document useful (0 votes)
39 views12 pages

PART2 - Examples

Uploaded by

owronrawan74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views12 pages

PART2 - Examples

Uploaded by

owronrawan74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Examples/Java Basic/PART2

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.

a) Constructor name must be same as its class name.

b) The constructor must have no explicit return type

There are two types of constructors:

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");}

public static void main(String args[]){

Bike1 b=new Bike1();

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;

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Student3 s1=new Student3();

Student3 s2=new Student3();

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;

Student4(int i,String n){

id = i;

name = n; }

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Studnt4 s1 = new Student4(111,"Karan");

Student4 s2 = new Student4(222,"Aryan");

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;

Student5(int i,String n){

id = i;

name = n;

Student5(int i,String n,int a){

id = i;

name = n;

age=a;

void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){

Student5 s1 = new Student5(111,"Karan");

Student5 s2 = new Student5(222,"Aryan",25);

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;

// Following is the constructor

MyClass() {

x = 10; } }

public class ConsDemo {

public static void main(String args[]) {

MyClass t1 = new MyClass();

MyClass t2 = new MyClass();

System.out.println(t1.x + " " + t2.x);

} }

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;

// Following is the constructor

MyClass(int i ) {

x = i;

4
} }

public class ConsDemo {

public static void main(String args[]) {

MyClass t1 = new MyClass( 10 );

MyClass t2 = new MyClass( 20 );

System.out.println(t1.x + " " + t2.x);

} }

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{

int count=0;//will get memory when instance is created

Counter(){

count++;

System.out.println(count);

public static void main(String args[]){

Counter c1=new Counter();

Counter c2=new Counter();

Counter c3=new Counter();

} }

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);

public static void main(String args[]){

Counter2 c1=new Counter2();

Counter2 c2=new Counter2();

Counter2 c3=new Counter2();

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;

class Programmer extends Employee{

int bonus=10000;

public static void main(String args[]){

Programmer p=new Programmer();

System.out.println("Programmer salary is:"+p.salary);

System.out.println("Bonus of Programmer is:"+p.bonus);

Run:

Programmer salary is:40000.0

Bonus of programmer is:10000

Ex:

Slution:

class Vehicle{

int speed=50;

class Bike4 extends Vehicle{

int speed=100;

7
void display(){

System.out.println(super.speed);//will print speed of Vehicle now

public static void main(String args[]){

Bike4 b=new Bike4();

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");}

class Bike5 extends Vehicle{

Bike5(){

super();//will invoke parent class constructor

System.out.println("Bike is created");

public static void main(String args[]){

Bike5 b=new Bike5();

} }

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");}

class Student16 extends Person{

void message(){System.out.println("welcome to java");}

void display(){

message();//will invoke current class message() method

super.message();//will invoke parent class message() method

public static void main(String args[]){

Student16 s=new Student16();

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");}

class Student17 extends Person{

void display(){

9
message();//will invoke parent class message() method

public static void main(String args[]){

Student17 s=new Student17();

s.display(); } }

Run:

welcome

Ex: Write a java program that uses this keyword to access the members of a class.

Solution:

public class This_Example {

// Instance variable num

int num = 10;

This_Example() {

System.out.println("This is an example program on keyword this");

This_Example(int num) {

// Invoking the default constructor

this();

// Assigning the local variable num to the instance variable num

this.num = num;

public void greet() {

System.out.println("Hi Welcome");

public void print() {

// Local variable num

int num = 20;


10
// Printing the instance variable

System.out.println("value of local variable num is : "+num);

// Printing the local variable

System.out.println("value of instance variable num is : "+this.num);

// Invoking the greet method of a class

this.greet();

public static void main(String[] args) {

// Instantiating the class

This_Example obj1 = new This_Example();

// Invoking the print method

obj1.print();

// Passing a new value to the num variable through parametrized constructor

This_Example obj2 = new This_Example(30);

// Invoking the print method again

obj2.print();

Run:

This is an example program on keyword this

value of local variable num is : 20

value of instance variable num is : 10

Hi Welcome to Tutorialspoint

This is an example program on keyword this

value of local variable num is : 20

value of instance variable num is : 30

Hi Welcome

11
Ex:

Solution:

class Student11{

int id;

String name;

Student11(int id,String name){

this.id = id;

this.name = name;

void display()

{System.out.println(id+" "+name);}

public static void main(String args[]){

Student11 s1 = new Student11(111,"Karan");

Student11 s2 = new Student11(222,"Aryan");

s1.display();

s2.display();

} }

12

You might also like