Professional Documents
Culture Documents
Chapter 1
Object-Oriented Programming
with JAVA Topics covered:
Introduction to JAVA
Primitive data types
Fundamental Programming Class & Object
Structure Inheritance
Polymorphism
Chapter 1 – part 3
1 2
Constructors
Constructors
Rules for creating Java constructor:
In Java, a constructor is a block of codes 1. Constructor name must be the same as its
similar to the method. It is called when an class name
instance of the object is created, and 2. A Constructor must have no explicit
memory is allocated for the object. return type
3. A Java constructor cannot be abstract,
It is a special type of method which static, final, and synchronized.
is used to initialize the object.
3 4
Chap 1 Constructors in Java Chap 1 Constructors in Java
5 6
class Student{
int id; void display()
String name; {
int age; System.out.println(id+" "+name+" "+age); • Defines a relationship among classes where one
}
Student(int i,String n) class shares the structure and/or behavior on one
{
id = i;
public static void main(String args[])
{
or more classes.
name = n; Student s1 = new Student(111,"Karan"); • The class which inherits the properties of other is
} Student s2 = new Student(222,"Aryan",25);
s1.display(); known as subclass (derived class, child class) and
Student(int i,String n,int a) s2.display();
{ the class whose properties are inherited is known
id = i;
name = n;
} as superclass (base class, parent class)
age=a; }
}
7 8
Chap 1 Inheritance Chap 1 Inheritance
+ bonus : float
9 10
11 12
Chap 1 Inheritance Chap 1 Inheritance
Single Inheritance Multilevel Inheritance
class Animal{
class Animal{
void eat(){ System.out.println("eating.."); }
void eat(){
}
System.out.println("eating...");
}
class Cat extends Animal{
}
void jump(){ System.out.println(“jumping.."); }
}
class Cat extends Animal{
void jump(){
class BabyCat extends Cat{
System.out.println(“jumping...");
void sleep(){ System.out.println(“sleeping.."); }
}
}
}
class TestInheritance{
class TestInheritance{
public static void main(String args[]){
public static void main(String args[]){
BabyCat bc = new BabyCat();
Cat c = new Cat();
bc.sleep();
c.jump();
bc.jump();
c.eat();
bc.eat();
}
}
}
}
13 14
15 16
Inheritance and constructors in Java
Inheritance and constructors in Java
class Base { Inheriting parameterized constructor
Inheriting default constructor int x;
Base(int x) {
class Base { this.x = x;
Base() { }
System.out.println("Base Class Constructor Called "); }
}
} class Derived extends Base {
int y;
Derived(int x, int y) {
class Derived extends Base {
super(x);
Derived() {
this.y = y;
System.out.println("Derived Class Constructor Called "); }
} void Display() {
} System.out.println("x = "+x+", y = "+y);
}
public class Main { }
public static void main(String[] args) {
Derived d = new Derived(); public class Main {
} public static void main(String[] args) {
} Derived d = new Derived(10, 20);
d.Display();
}
}
17 18
class Animal {
public void animalSound() {
Chap 1 Polymorphism }
System.out.println("The animal makes a sound");
Polymorphism }
}
System.out.println("The cat says: meow meow");
19 20