Unit-2 ppt - Copy
Unit-2 ppt - Copy
Unit-2
Classes
• A class in Java is a blueprint for creating objects, defining their
attributes (fields/variables) and behaviors (methods).
• It enables code reusability by allowing multiple objects to be
created from the same class.
• Classes support encapsulation, helping to structure and organize
code efficiently.
• Memory is not allocated for a class until an object is instantiated.
• class ClassName {
• // Fields (variables)
• // Methods (functions)
• }
Example
• Example:
• class Car {
• String color; // Attribute (field)
• // Method (behavior)
• void drive() {
• System.out.println("Car is driving");
• }
• }
Objects
• An object is an instance of a class, representing a real-world
entity.
• Example:
• Car myCar = new Car(); // Creating an object of the Car class
Example
• class Car {
• String color;
• void drive() {
• System.out.println("Car is driving");
• }
• }
• class Main {
• public static void main(String[] args) {
• Car myCar = new Car(); // Creating an object
• myCar.color = "Red";
• myCar.drive();
• }
• }
Output:
• Car is driving
Constructor
• 1.A constructor is a special method used to initialize objects.
• 2. It has the same name as the class and no return type (not
even void).
• 3. Automatically called when an object is created using the
new keyword.
• 4. Used to set initial values for object attributes.
• 5. Can be parameterized (accepts values) or default (no
parameters).
• 6. If no constructor is defined, Java provides a default
constructor automatically.
• There are two types of constructors in Java:
• void drive() {
• System.out.println("Car is driving");
• }
• }
• class Main {
• public static void main(String[] args) {
• Car myCar = new Car(); // Constructor is automatically called
• myCar.drive();
• }
• }
Output
• A new car is created!
• Car is driving
Method Overloading
• Method Overloading allows multiple methods in a class to
have the same name but with different parameters (varying in
number or type).
• It improves code readability, reusability, and flexibility by
allowing methods to handle different inputs.
• The return type can be different, but changing only the return
type does not overload a method—the parameters must be
different.
• Overloaded methods must have a unique parameter list,
ensuring Java can distinguish them.
• It is commonly used for handling different types of input data
in a consistent way.
Syntax for Method Overloading:
• class ClassName {
• // First method
• void methodName(int a) {
• // Code
• }
• void display() {
• System.out.println("Public: " + publicVar);
• System.out.println("Private: " + privateVar);
• System.out.println("Protected: " + protectedVar);
• System.out.println("Default: " + defaultVar);
• }
• }
Example
• public class AccessModifiers {
• public static void main(String[] args) {
• Example obj = new Example();
• obj.display();
•
• // Accessing public variable directly
• System.out.println("Accessing Public Variable: " + obj.publicVar);
• }
• }
Output
• Public: 10
• Private: 20
• Protected: 30
• Default: 40
• Accessing Public Variable: 10
Static Method
• A static method belongs to the class rather than an
instance (object).
• void disp() {
• System.out.println("disp() a = " + a + ", b = " + b);
• }
• }
Output
• show() 20
• disp() a = 10, b = 20
String Class
• The java.lang.String class is final, which means no other class can extend
it.
• In Java, String objects are immutable, meaning that once a String is created
and initialized, its value cannot be changed within the same reference.
• If two or more String objects have the same sequence of characters, they
share the same reference in the string pool.
• In this case, str1, str2, and str3 refer to the same String object in the string
pool.
Memory Allocation in String Class
•
str1
str
My Name is Bond
str3
Creating String with new Keyword
• String str5 = new String("My name is Bond");
Example
• class StringExample1 {
• public static void main(String[] args) {
• String s1 = "computer";
• String s2 = "computer";
• String s3 = new String("computer");
• The super keyword allows access to parent class methods and constructors.
object-oriented programming.
Example
• class Parent {
• void display() {
• System.out.println("Parent class method");
• }
• }
• class Main {
• public static void main(String[] args) {
• Child obj = new Child();
• obj.display(); // Inherited from Parent
• obj.show(); // Child class method
• }
• }
Output
• Parent class method
• Child class method
Types of Inheritance in Java
• Single Inheritance
• One class inherits from another.
• 📌 Diagram:
• Parent
• │
• ▼
• Child
Example
• class Parent {
• void display() {
• System.out.println("Parent class method");
• }
• }
• class Main {
• public static void main(String[] args) {
• Child obj = new Child();
• obj.display();
• obj.show();
• }
• }
Output
• Parent class method
• Child class method
Multilevel Inheritance
• In multilevel inheritance, a class inherits from another class, which is
further inherited by a third class.
• 📌 Diagram:
• Grandparent
• │
• ▼
• Parent
• │
• ▼
• Child
Example
• class Grandparent {
• void methodA() {
• System.out.println("Grandparent method");
• }
• }
• class Parent extends Grandparent {
• void methodB() {
• System.out.println("Parent method");
• }
• }
• class Child extends Parent {
• void methodC() {
• System.out.println("Child method");
• }
• }
• class Main {
• public static void main(String[] args) {
• Child obj = new Child();
• obj.methodA();
• obj.methodB();
• obj.methodC();
• }
• }
Output
• Grandparent method
• Parent method
• Child method
Hierarchical Inheritance
• In hierarchical inheritance, a single parent class is inherited by multiple child
classes. This allows different subclasses to share common properties and behaviors
from a common superclass.
• 📌 Diagram:
Parent
/ \
• Child1 Child2
Example
• class Parent {
• void show() {
• System.out.println("Parent method");
• }
• }
• class Child1 extends Parent {
• void display1() {
• System.out.println("Child1 method");
• }
• }
• class Child2 extends Parent {
• void display2() {
• System.out.println("Child2 method");
• }
• }
• class Main {
• public static void main(String[] args) {
• Child1 obj1 = new Child1();
• obj1.show();
• obj1.display1();
• Child2 obj2 = new Child2();
• obj2.show();
• obj2.display2();
• }
• }
Output
• Parent method
• Child1 method
• Parent method
• Child2 method
Multiple Inheritance (Using Interfaces)
• Java does not support multiple inheritance with classes but allows it
using interfaces.
• 📌 Diagram:
• Interface1 Interface2
• │ │
• ▼ ▼
• Class (Implements Both)
Overriding Methods
• Definition: Method Overriding allows a subclass to provide its own implementation of a
method that is already defined in its superclass.
• Key Points:
• Same Method Signature: The method in the subclass must have the same name, same return
type, and same parameter list as the method in the parent class.
• @Override Annotation: Optional but recommended for clarity and to prevent errors.
• Cannot Override final, static, and private methods: These methods cannot be overridden.
• Access Modifier: The access level of the overridden method must be at least as accessible as
the method in the superclass (e.g., public or protected).
• Exception Handling: The subclass method can throw the same or a subclass of the exceptions
declared in the parent method.
Benefits
Promotes polymorphism (same method behaves differently for different
objects).