0% found this document useful (0 votes)
12 views22 pages

Institute: Uie Department: Cse: Java Programming (20CST-218)

The document provides an overview of Java Classes, focusing on constructors, the 'this' and 'super' keywords, and access control. It explains the types of constructors, their usage, and the significance of the 'this' and 'super' keywords in object-oriented programming. Additionally, it discusses access modifiers and their role in defining the visibility of class members.

Uploaded by

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

Institute: Uie Department: Cse: Java Programming (20CST-218)

The document provides an overview of Java Classes, focusing on constructors, the 'this' and 'super' keywords, and access control. It explains the types of constructors, their usage, and the significance of the 'this' and 'super' keywords in object-oriented programming. Additionally, it discusses access modifiers and their role in defining the visibility of class members.

Uploaded by

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

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Classes

DISCOVER . LEARN . EMPOWER


Lecture
Objectives

In this lecture, we will discuss:


• Constructors
• This and super keyword
• Access Control

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:

• No-argument constructor (Default constructor):


If we don’t define a constructor in a class, then the compiler creates
a default constructor(with no arguments) for the class. And if we write
a constructor with arguments or no arguments then the compiler does
not create a default constructor.
• Parameterized Constructor:
A constructor that has parameters is known as parameterized
constructor. If we want to initialize fields of the class with our own
values, then use a parameterized constructor.

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

• The super keyword in java is a reference variable that is used to refer


parent class objects.
• The keyword “super” came into the picture with the concept of
Inheritance.
• The use of super keyword
1) To access the data members of parent class when both parent and child
class have member with same name
2) To explicitly call the no-arg and parameterized constructor of parent
class
3) To access the method of parent class when child class has overridden
that method.
this keyword
• The this keyword refers to the current object in a method or constructor.
• The most common use of the this keyword is to eliminate the confusion
between class attributes and parameters with the same name.
• this can also be used to:
i. Invoke current class constructor
ii. Invoke current class method
iii. Return the current class object
iv. Pass an argument in the method call
v. Pass an argument in the constructor call
this keyword
• Using ‘this’ keyword to refer current class instance variables.

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:

Which of these is used as a default for a member


of a class if no access specifier is used for it?
a) private
b) public
c) public, within its own package
d) protected
QUIZ:
Q2:
“this" keyword can not be used in ---?

a) Constructors
b) Static blocks and methods
c) Non static methods
Summary:

In this session, you were able to :


• Learn about this and super keyword
• Understand access controls
References:

Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Video Lectures : https://www.youtube.com/watch?v=eKoJiCUFEls


https://www.youtube.com/watch?v=1ZDApgnufSI
THANK YOU

You might also like