1.3 (2)
1.3 (2)
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science &
Engineering)
2
Classes
A class contains variable declarations and method definitions. It provides a sort of
template for an object that serves to define its properties.
Object
• An entity that has state and behavior is known as an object e.g., chair, bike,
marker, pen, table, car, etc. It can be physical or logical (tangible and intangible).
The example of an intangible object is the banking system.
• An object has three characteristics:
a) State: represents the data (value) of an object.
b) Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
c) Identity: An object identity is typically implemented via a unique ID. The value
of the ID is not visible to the external user. However, it is used internally by the
JVM to identify each object uniquely.
Object
How do objects communicate in Java?
Hint
The correct answer is:
(c) They communicate by calling each other’s instance methods.
HOW??????????
• In Java, objects communicate with each other by invoking each other’s instance
methods. This is part of the object-oriented programming paradigm where
objects interact by sending messages (method calls) to each other. Through these
methods, data can be processed or exchanged without directly accessing or
modifying each other's fields.
• Option (a): Modifying fields directly breaks encapsulation, which is not
recommended.
• Option (b): Static variables are shared across all instances of a class and are not
typically used for communication between objects.
• Option (d): Static methods are associated with the class itself, not with specific
instances (objects), so they are not suitable for object-to-object communication. 6
How to declare a class in java??
class Rectangle CLASS DEDCLARATION
{ int area=length*width;
Return (area); RETURN THE VALUE OF THE
METHOD
}
}
EXAMPLE 2
• Class RectArea
•{
• public static void main(String args[])
• {
• int area1;
• Rectangle rect1= new Rectangle();
• rect1.getData(20,12);
• area1= rect1.rectArea();
• System.out.println (“Area = ”+area1);
•} 8
Java Encapsulation
• The meaning of Encapsulation, is to make sure that
"sensitive" data is hidden from users.
• To achieve this, you must:
Declare class variables/attributes as private provide
public get and set methods to access and update the value of
a private variable
Get and Set
• You have learned that private variables can only be accessed
within the same class (an outside class has no access to it).
• However, it is possible to access them if we provide
public get and set methods.
The get method returns the variable value, and the set method
sets the value.
Syntax for both is that they start with either get or set, followed
by the name of the variable, with the first letter in upper case
Example of Encapsulation
Abstraction
• The process of hiding certain details and
showing only essential information to the
user.
• Abstraction can be achieved with
either abstract classes or interfaces (which
you will learn more about in the next
chapter).
• The abstract keyword is a non-access
modifier, used for classes and methods:
• Abstract class: is a restricted class that
cannot be used to create objects (to access
it, it must be inherited from another class).
• Abstract method: can only be used in an
abstract class, and it does not have a body.
The body is provided by the subclass
(inherited from).
Example of abstraction
Creating reference to abstract class
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run()
{
System.out.println("running safely");
}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
13
Points to Remember
14
Inheritance in real World
Example of inheritance
// Parent class
class Animal {
String name;
; // Output: Whiskers says: Meow! Meow!
}
// Constructor for the parent class
}
Animal(String name) {
// Another child class
this.name = name;
class Cat extends Animal {
}
// Constructor for the child class
// Parent class method
Cat(String name) {
void sound() {
super(name);
System.out.println("Animals make sounds");
}
}
}
// Overriding the parent class method
@Override
// Child class inheriting from Parent class
void sound() {
class Dog extends Animal {
System.out.println(name + " says: Meow! Meow!");
String breed;
}
}
// Constructor for the child class
Dog(String name, String breed) {
// Main class to test the program
super(name); // Calling the parent class constructor
public class InheritanceExample {
this.breed = breed;
public static void main(String[] args) {
}
Dog dog = new Dog("Buddy", "Golden Retriever");
Cat cat = new Cat("Whiskers");
// Overriding the parent class method
@Override
// Calling methods
void sound() {
dog.sound(); // Output: Buddy says: Bark! Bark!
System.out.println(name + " says: Bark! Bark!");
cat.sound()
}
}
Types Of Inheritance
Predict the output??
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
POLYMORPHISM
• Polymorphism is one of
the OOPs feature that allows us to
perform a single action in different
ways.
• Polymorphism is the capability of a
method to do different things based
on the object that it is acting upon.
• It allows you define one interface and
have multiple implementations.
Difference between Static and runtime polymorphism
Default: The access level of a Protected: The access level of Public: The access level of a
Private: The access level of a default modifier is only within a protected modifier is within public modifier is
private modifier is only within the package. It cannot be the package and outside the everywhere. It can be
the class. It cannot be accessed from outside the package through child class. If accessed from within the
accessed from outside the package. If you do not specify you do not make the child class, outside the class, within
class. any access level, it will be the class, it cannot be accessed the package and outside the
default. from outside the package. package.
Demonstrating Access Modifiers
}
// Parent class
// Subclass in the same package
class Parent {
class Child extends Parent {
public String publicField = "Public Field"; // Accessible public void display() {
everywhere System.out.println("Accessing fields from Child class:");
System.out.println("Public Field: " + publicField); // Allowed
protected String protectedField = "Protected Field"; // Accessible
System.out.println("Protected Field: " + protectedField); // Allowed
within the package and subclasses
System.out.println("Default Field: " + defaultField); // Allowed
String defaultField = "Default Field"; // Accessible within the // System.out.println("Private Field: " + privateField); // Not allowed - Compile-time error
package }
}
private String privateField = "Private Field"; // Accessible only
within this class
// Class in the same package
public class AccessModifiersExample {
public static void main(String[] args) {
// Method to demonstrate private access Parent parent = new Parent();
private void privateMethod() {
// Accessing fields from the Parent class
System.out.println("This is a private method"); System.out.println("Accessing fields from Main class:");
} System.out.println("Public Field: " + parent.publicField); // Allowed
System.out.println("Protected Field: " + parent.protectedField); // Allowed
System.out.println("Default Field: " + parent.defaultField); // Allowed
// System.out.println("Private Field: " + parent.privateField); // Not allowed - Compile-time
// Public method to access private field and method
error
public void accessPrivate() {
parent.accessPrivate(); // Accessing private field/method through a public method
System.out.println("Accessing: " + privateField);
privateMethod(); // Creating Child object
Child child = new Child();
} child.display(); // Access fields through Child class
} 24
}
Non-access modifiers
Books:
1. Balaguruswamy, Java.
2. Modern Java in Action by Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft
3. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies,John P. Flynt Thomson, Java Programming.
Video Lectures :
https://www.youtube.com/watch?v=-HafzawNlUo
https://www.youtube.com/watch?v=7GwptabrYyk&t=45s
THANK YOU