0% found this document useful (0 votes)
1 views32 pages

For Mid Tern (Viva)

The document provides an overview of classes and objects in Java, explaining their definitions, creation, and usage. It covers key concepts such as constructors, method and constructor overloading, inheritance types, interfaces, and polymorphism. Examples are included to illustrate each concept, demonstrating how to implement them in Java code.

Uploaded by

623292gul
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)
1 views32 pages

For Mid Tern (Viva)

The document provides an overview of classes and objects in Java, explaining their definitions, creation, and usage. It covers key concepts such as constructors, method and constructor overloading, inheritance types, interfaces, and polymorphism. Examples are included to illustrate each concept, demonstrating how to implement them in Java code.

Uploaded by

623292gul
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/ 32

Class and Object in Java

Instructor: Adil Ahmad


Email: adil.ahmad@numl.edu.pk
What is a Class?
• A class is a blueprint or template for creating objects.
• It defines properties (fields/attributes) and behaviors (methods) of objects.
• Example of a class in Java:

class Car {
String brand;
int speed;
void display() {
System.out.println("Brand: " + brand + ",
Speed: " + speed); }
}
What is an Object?
• An object is an instance of a class.
• It has state (attributes) and behavior (methods).
• Objects occupy memory and interact with each other.
• Example of object creation in Java:

public class Main {


public static void main(String[] args) {
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.display();
}
}
Creating and Using Objects
Steps to create an object:
1. Declare the class.
2. Create an object using new keyword.
3. Assign values to object properties.
4. Call methods of the object.
Example:

Person person1 = new Person();


person1.name = "John";
person1.age = 25;
person1.displayInfo();
Constructor in Java
What is a constructor?
• A special method used to initialize objects.
• It has the same name as the class and no return type.
• Constructors are automatically called when an object is created.
• They help in setting initial values for object attributes.
Constructor in Java
Example:

class Person {
String name;
int age;

Person(String n, int a) {
// Constructor
name = n;
age = a;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Constructor Example

public class Main {


public static void main(String[] args) {
Person p1 = new Person("Alice", 30);
p1.display();
}
}
Introduction to Overloading
What is Overloading?
• Overloading in Java allows multiple methods with the same name but
different parameters.
• Improves code readability and reusability.
• Used primarily in method and constructor overloading.
Introduction to Overloading
What is Overloading?

class OverloadExample {
void display(int a) {
System.out.println("Integer: " + a);
}

void display(double a) {
System.out.println("Double: " + a);
}
}
Method Overloading
Method Overloading in Java
• Same method name, different parameter lists.
• Achieved by changing:
• Number of parameters
• Data type of parameters
• Order of parameters
Method Overloading
Example:
class MathOperations {
int sum(int a, int b) {
return a + b;
}

double sum(double a, double b) {


return a + b;
}

int sum(int a, int b, int c) {


return a + b + c;
}
}
Constructor Overloading
Constructor Overloading
• Multiple constructors with different parameters in the same class.
• Useful for initializing objects in different ways.
• Example: class Person {
String name;
int age;

Person(String n) {
name = n;
}

Person(String n, int a) {
name = n;
age = a;
}
}
Importing Header Files in Java
Importing Header Files
• Java does not use traditional "header files" like C++.
• Instead, Java uses import statements to include predefined classes from
packages.
Example:
import java.util.Scanner; // Importing Scanner class

public class InputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
System.out.println("You entered: " + num);
}
}
Introduction to Inheritance
What is Inheritance?
• Inheritance is a fundamental OOP concept in Java.
• It allows a class to inherit properties and methods from another class.
• Promotes code reusability and hierarchical classification.
Diagram Example:

Vehicle (Parent Class)


|
├── Car (Child Class)
├── Bike (Child Class)
How Inheritance Works in Java
How Does Java Implement Inheritance?
• The extends keyword is used to inherit a class.
• The child (subclass) gets all non-private members of the parent class.
How Inheritance Works in Java
Example:

// Parent class
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

// Child class inheriting from Animal


class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
How Inheritance Works in Java
Example:

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
d.makeSound(); // Inherited method
d.bark(); // Own method
}
}
Types of Inheritance in Java
Types of Inheritance in Java
1. Single Inheritance – One class inherits another.
2. Multilevel Inheritance – A class inherits another class, which inherits another.
3. Hierarchical Inheritance – Multiple classes inherit from a single class.
4. Multiple Inheritance (via Interfaces) – Java does not support multiple
inheritance with classes but allows it using interfaces.
Single Inheritance Example
A single parent-child relationship.
Example: class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
class Car extends Vehicle {
void fuelType() {
System.out.println("Car uses petrol or diesel");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.run(); // Inherited method
c.fuelType(); // Own method
}}
Multilevel Inheritance Example
A class inherits from a derived class
class Grandparent {
void familyName() {
System.out.println("Family Name: Smith");
}
}
class Parent extends Grandparent {
void job() {
System.out.println("Parent is an Engineer");
}
}
class Child extends Parent {
void hobby() {
System.out.println("Child loves painting");
}
}
Multilevel Inheritance Example
A class inherits from a derived class

public class Main {


public static void main(String[] args) {
Child obj = new Child();
obj.familyName();
obj.job();
obj.hobby();
}
}
Hierarchical Inheritance Example
Multiple classes inherit from a common base class
class Vehicle {
void start() {
System.out.println("Vehicle is
starting");
}
}
class Car extends Vehicle {
void speed() {
System.out.println("Car is moving at
100 km/h"); }
}
class Bike extends Vehicle {
void mileage() {
System.out.println("Bike gives 50 km/l
mileage");
}
}
Hierarchical Inheritance Example
Multiple classes inherit from a common base class

public class Main {


public static void main(String[] args) {
Car myCar = new Car();
myCar.start(); myCar.speed();
Bike myBike = new Bike();
myBike.start(); myBike.mileage();
}
}
Multiple Inheritance

Note*: Not supported through classes in Java, but achievable via


interfaces
Interfaces in Java
What is an Interface?
• An interface in Java is a blueprint of a class.
• It contains only abstract methods (before Java 8).
• Used for multiple inheritance and abstraction.
• Defined using the interface keyword.
Interface Syntax
• interface Animal {
• void sound(); // Abstract method
• }

• class Dog implements Animal {


• public void sound() {
• System.out.println("Dog barks");
• }
• }

• public class InterfaceExample {


• public static void main(String args[]) {
• Dog d = new Dog();
• d.sound();
• }
• }
Multiple Inheritance using Interfaces
• interface Printable {
• void print();
• }

• interface Showable {
• void show();
• }

• class Demo implements Printable, Showable {


• public void print() {
• System.out.println("Print Method");
• }

• public void show() {


• System.out.println("Show Method");
• }
• }
Multiple Inheritance using Interfaces
• public class MultipleInterfaceExample {
• public static void main(String args[]) {
• Demo obj = new Demo();
• obj.print();
• obj.show();
• }
• }
Polymorphism in Java
What is Polymorphism?
Polymorphism means "many forms" in Java.
Types:
• Compile-time (Method Overloading)
✓ Same method name, different parameters.
• Runtime (Method Overriding)
✓ Parent class method is overridden in the child class.
✓ Happens during runtime.
Method Overloading
• class OverloadExample {
• void add(int a, int b) {
• System.out.println("Addition: " + (a + b));
• }

• void add(double a, double b) {
• System.out.println("Addition: " + (a + b));
• }
• }

• public class MethodOverloading {


• public static void main(String args[]) {
• OverloadExample obj = new OverloadExample();
• obj.add(5, 10);
• obj.add(5.5, 2.5);
• }
• }
Method Overriding
• class Parent {
• void show() {
• System.out.println("Parent class method");
• }
• }

• class Child extends Parent {


• @Override
• void show() {
• System.out.println("Child class method");
• }
• }

• public class MethodOverriding {


• public static void main(String args[]) {
• Parent obj = new Child(); // Runtime Polymorphism
• obj.show();
• }
• }
Difference Between Method Overloading and Overriding

Feature Method Overloading Method Overriding


Same method name, same
Definition Same method name, different parameters
parameters
Occurrence Compile-time Runtime
Inheritance Not required Required
Access Modifier Can be different Cannot reduce visibility

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy