0% found this document useful (0 votes)
2 views14 pages

OOPs Using Java 02 Class Notes

Uploaded by

Parul
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)
2 views14 pages

OOPs Using Java 02 Class Notes

Uploaded by

Parul
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

CS & IT

ENGINEERING
Java With OOPS
OOPs using Java

Lecture No-02 By- Aditya sir


Recap of Previous Lecture

OOP
Topic Intro to

Class
object
Examples
Constructors
Topics to be Covered

Topic Constructors, this & Intro to Inheritance

mm

3
Topic : Constructors

Definition:
• Special method named after the class, no return type.
Default Constructor:
• Supplied by compiler if none declared; initializes fields to Java defaults (0, false,
null).
Parameterized Constructor:
• Accepts arguments to set up object state at creation, enables early validation.
Copy Constructor (Custom):
• Takes another instance of same type to clone its state.
Topic : Constructors

Snippet ([Link]): @Override


public class Point { public String toString() {
int x, y; return "(" + x + ", " + y + ")";
// Default constructor }
public Point() {
public static void main(String[] args)
this.x = 0;
{
this.y = 0;
Point p1 = new Point();
}
// Parameterized constructor Point p2 = new Point(3, 4);
public Point(int x, int y) { [Link]("p1 = " + p1);
this.x = x; [Link]("p2 = " + p2);
this.y = y; }
} }
Topic : Constructors

Output:
p1 = (0, 0)
classwork
p2 = (3, 4)
PI 3,4
Op

ordinate is 3 and
Mp n co

coordinate is 4
I
Topic : Theory – this Keyword

Field Disambiguation:
• [Link] = parameter; distinguishes instance variables from parameters.
Constructor Chaining:

o
this(args…) invokes another constructor in same class; must be first statement.
Passing Current Object:
• Methods can accept this (someMethod(this);).
Fluent APIs:
• Return this to enable chaining of setters.
Topic : Theory – this Keyword
Snippet ([Link]):
public class Student {
private String name;
// Parameterized constructor
public Student(String name) {
this args
[Link] = name;
}
// No-arg constructor calls the parameterized one
public Student() {
this("Unknown");
} mm

public void printName() { [Link]("Name: " + [Link]); }


public static void main(String[] args) {
new Student().printName();
new Student("Alice").printName();
}
}
Topic : Theory – this Keyword

Output:
Name: Unknown
Name: Alice
base
Parent class Super

child class Sub derived


Topic : Theory – Inheritance & super

Single Inheritance:
• Use extends to derive a subclass from one superclass.
Constructor Invocation:

0
super(args…) calls a superclass constructor; must be first in subclass constructor.
Method Overriding: am
me

• Subclass can override non-final methods of superclass; use @Override.


Accessing Superclass Members:
• [Link]() or [Link] bypasses overridden definitions.
50

T
Topic : Theory – Inheritance & super

Snippet ([Link]): class Dog extends Animal {


class Animal { public Dog(String name) {
String name; super(name);
public Animal(String name) { }
[Link] = name; @Override
} public void sound() {
public void sound() { [Link](name + " says: Woof!");
[Link]("Some sound"); }
} } E
} public class Main {
public static void main(String[] args) {
Dog d = new Dog("Buddy");
Son sound
}
[Link]();

}
Woof
Topic : Theory – Inheritance & super

Output:
Buddy says: Woof!
THANK - YOU

You might also like