You are on page 1of 5

Chap 1 Fundamental Programming Structure

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

Chap 1 Constructors in Java Chap 1 Constructors in Java

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

Default Constructor Parameterized Constructor A constructor which has a specific


A constructor without any
number of parameters
parameters class Student{
class Bike{ int id; String name;

//creating a parameterized constructor


//creating a default constructor Student(int i,String n){
Bike(){ id = i; name = n;
System.out.println("Bike is created"); }
} //method to display the values
void display(){System.out.println(id+" "+name);}
//main method
public static void main(String args[]){
public static void main(String args[]){
//creating objects and passing values
Student s1 = new Student(111,“Alex");
//calling a default constructor
Bike b = new Bike(); //calling method to display the values of object
s1.display();
}
} }
}

5 6

Chap 1 Constructors in Java


Chap 1 Inheritance
Constructor Overloading having more than one constructor
with different parameter lists

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

Parent-Child Relationship / Is-A Relationship


Employee • Manager is the subclass and
+ salary : float Employee is the superclass.
• The relationship between the
two classes is Manager IS-A
Employee.
• This means that Manager is a
type of Employee
Manager

+ bonus : float

9 10

Chap 1 Inheritance Chap 1 Inheritance


Single Inheritance
class Vehicle {
extends Keyword protected String brand = "Ford";
extends is the keyword used to inherit the public void honk() {
properties of a class. System.out.println("Tuut, tuut!");
}
}
Syntax
class Car extends Vehicle {
private String modelName = "Mustang";
Access_modifier class SuperClassName {
public static void main(String[] args) {
// variables or fields
// methods Car myCar = new Car();
}
Access_modifier class SubClassName extends SuperClassName myCar.honk();
{
// variables or fields System.out.println(myCar.brand + " " + myCar.modelName);
// methods }
} }

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

Chap 1 Inheritance Chap 1 Inheritance


Hierarchical Inheritance
class Animal{
void eat(){System.out.println("eating...");}
Inheritance and constructors in Java
}

class Cow extends Animal{


void moo(){System.out.println(“moowing...");} If a class is inheriting the properties of another
} class, the subclass automatically acquires the
default constructor of the superclass.
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
} To acquire a parameterized constructor of the
superclass, the subclass need the super keyword
class TestInheritance{ as shown below.
public static void main(String args[]){
Cat c = new Cat();
super (parameter list);
c.meow();
c.eat();
//c.moo();
}
}

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");

class Cat extends Animal {


public void animalSound() {

Polymorphism }
}
System.out.println("The cat says: meow meow");

class Duck extends Animal {


public void animalSound() {
 Polymorphism means "many forms“ System.out.println("The duck says:quack quack");
}
 Occurs when there are many classes that are related }
to each other by inheritance class MyMainClass {
 Polymorphism uses the same name methods in these public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
classes to perform different tasks. Animal myCat = new Cat(); // Create a Cat object
Animal myDuck = new Duc(); // Create a Duck object
 Allows to perform a single action in different ways myAnimal.animalSound();
myCat.animalSound();
myDuck.animalSound();
}
}

19 20

You might also like