Institute: Uie Department: Cse: Java Programming (20CST-218)
Institute: Uie Department: Cse: Java Programming (20CST-218)
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Classes
2
Constructors
• A constructor in Java is a special method that is used to initialize objects. The
constructor is called when an object of a class is created.
• Constructor is automatically invoked whenever an object of the class is created.
• At the time of calling the constructor, memory for the object is allocated in the
memory.
• Rules to define a constructor
A constructor has the same name as the class name
A constructor should not have a return type
Constructors are called only once at the time of Object creation while method(s)
can be called any number of times.
A class can contain more than one constructor, So it can be overloaded.
Example of constructor
Two types of constructors in java:
5
//Default constructor // Parameterised constructor
import java.io.*; import java.io.*;
class Geek {
class Geek { String name;
int num; int id;
String name; Geek(String name, int id) {
this.name = name;
Geek() { System.out.println("Constructor called"); } this.id = id;
} }}
class GFG {
class GFG {
// main driver method
public static void main(String[] args) { public static void main(String[] args)
Geek geek1 = new Geek(); {
System.out.println(geek1.name);
Geek geek1 = new Geek("adam", 1);
System.out.println(geek1.num); System.out.println("GeekName :" + geek1.name
}} + " and GeekId :" + geek1.id);
}}
OUTPUT:
Constructor called OUTPUT:
null
GeekName :adam and GeekId :1
0
6
super keyword
class Test {
int a, b;
Test(int a, int b) {
this.a = a;
this.b = b; }
void display() {
System.out.println("a = " + a + " b = " + b); }
public static void main(String[] args) {
Test object = new Test(10, 20);
object.display();
} }
this keyword
• Using this() to invoke current class constructor
class Test {
int a,b;
Test() {
this(10, 20);
System.out.println("Inside default constructor \n"); }
Test(int a, int b) {
this.a = a;
this.b = b;
System.out.println("Inside parameterized constructor"); }
public static void main(String[] args) {
Test object = new Test(); } }
this keyword
Using ‘this’ keyword to invoke current class method
class Test {
void display() {
this.show();
System.out.println("Inside display function"); }
void show() {
System.out.println("Inside show funcion"); }
public static void main(String args[]) {
Test t1 = new Test();
t1.display();
}}
super keyword
Use of super with variables:
class Vehicle {
int maxSpeed = 120; }
class Car extends Vehicle {
int maxSpeed = 180;
void display() {
/* print maxSpeed of base class (vehicle) */
System.out.println("Maximum Speed: " + super.maxSpeed); } }
class Test {
public static void main(String[] args) {
Car small = new Car();
small.display(); } }
super keyword
Use of super with methods:
class Person {
void message() {
System.out.println("This is person class"); } }
class Student extends Person {
void message() {
System.out.println("This is student class"); }
void display() {
message();
// will invoke or call parent class message() method
super.message(); } }
class Test {
public static void main(String args[]) {
Student s = new Student();
s.display(); }}
ACCESS CONTROL Problem Statement
Design a secure banking application.
The system should include BankAccount
Class:Define attributes such as
accountNumber, accountHolderName,
CAN YOU THINK OF
balance, and pin.
APPROPRIATE
•accountNumber: Should be publicly
APPROACH HERE?????
accessible for identification purposes.
•accountHolderName: Should be
protected so it can be accessed by derived
classes.
•balance: Should be private to ensure it
cannot be accessed or modified directly
from outside the class.
•pin: Should be private to ensure
confidentiality.
14
ACCESS CONTROL
● Access modifiers define the scope of
the class and its
members (data and methods).
● There are four categories, provided by Java
regarding the visibility of the class
members between classes and packages:
■ Subclasses in the same package
■ Non-subclasses in the same package
■ Subclasses in different packages
■ Classes that are neither in the same
package nor subclasses
ACCESS CONTROL:
ACCESS CONTROL:
QUIZ:
a) Constructors
b) Static blocks and methods
c) Non static methods
Summary:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.