Top Banner
Java Constructors
47

Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

May 24, 2018

Download

Documents

lehuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Java Constructors

Page 2: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Constructor

A constructor :

• is used in the creation of an object.

• is a special method with no return type.

• must have the same name as the class it is in.

• is used to initialize the object.

• if not defined will initialize all instance variables to default value.

Page 3: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Constructor

public class Class2 {

int x;

int y;

public Class2() {

x = 10;

y = 11;

}

}

public class Class1 {

public static void main(String[] args) {

Class2 ob = new Class2();

System.out.println("Value of x when

constructor is called: " + ob.x);

System.out.println("Value of y when

constructor is called: " + ob.y);

}

}

Page 4: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

This keyword

• this is a keyword used to reference the current

object within an instance method or a

constructor.

Page 5: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Constructor Overloading

public class Class2 {

int x, y;

public Class2() {

x = 10;

y = 11;

}

public Class2(int z) {

x = z;

y = z;

}

}

public class Class1 {

public static void main(String[] args) {

Class2 ob = new Class2();

Class2 ob = new Class2(5);

}

Page 6: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Java Review

(Packages)

Page 7: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

What is Package?

Java Programming: OOP 7

• A Java package is a mechanism for

organizing Java classes into namespaces

• Programmers use packages to organize classes

belonging to the same category

• Classes in the same package can access each

other's package-access members

Page 8: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Advantage of a Package

Java Programming: OOP 8

• Programmers can easily determine that these classes are related

• Programmers know where to find files of similar types

• The names won't conflict

• You can have define access of the types within the package

Page 9: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Naming Convention of a Package

Java Programming: OOP 9

• Package names are written in all lower case

• Companies use their reversed Internet

domain name to begin their package names—

for example, com.example.mypackage for a package

named mypackage created by a programmer at example.com

Page 10: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Naming Convention of a Package

Legalizing Package Names

Domain Name Package Name Prefix

hyphenated-name.example.org org.example.hyphenated_name

example.int int_.example

123name.example.com com.example._123name

Java Programming: OOP 10

If the domain name contains -

i. a hyphen or a special character

ii. if the package name begins with a digit, illegal character reserved Java

keyword such as "int“

In this event, the suggested convention is to add an underscore

Page 11: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Package

package com.myproject;

Java Programming: OOP 11

Project

classes

com

myproject

MyClass.class

MyClass.java

myproject

com

src

Page 12: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Inheritance

Page 13: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Inheritance

Java Programming: OOP 13

• The child classes inherits all the attributes of the parent class

• They also have their distinctive attributes

Page 14: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Java Programming: OOP 14

Class Animal { // Type : Not Human

// Lives : On Land

// Gives Birth : }

Class Aquatic extends Animal { // Lives : In Water

// Gives Birth : }

Page 15: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Java Programming: OOP 15

package com.edureka.animal;

public class Animal {

public String Type = "Not Human";

public String Lives = "In Water";

public void fun(){

}

}

package com.edureka.animal;

public class Aquatic extends Animal{

String Lives = "In Water";

public void fun(){

System.out.println("defined here");

}

}

Page 16: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Java Programming: OOP 16

package com.edureka.animal;

public class Main {

public static void main(String[] args) {

Aquatic ob = new Aquatic();

System.out.println(ob.Type);

System.out.println(ob.Lives);

ob.fun();

}

}

Page 17: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Super keyword

• super is a keyword used to refer to the

variable or method or constructor of the

immediate parent class.

Page 18: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overloading

Page 19: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overloading

• The overloaded function must differ either by

the number of arguments or operands or

data types.

• The same function name is used for various

instances of function call

Page 20: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overloading

package com.edureka.animal;

public class Animal {

public void fun(){

System.out.println("Without Parameters");

}

}

package com.edureka.animal;

public class Aquatic extends Animal{

public void fun(int num){

System.out.println ("The number

passed is : " + num);

}

}

Page 21: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overloading

package com.edureka.animal;

public class Main {

public static void main(String[] args) {

Aquatic ob = new Aquatic();

//without Parameters, defined in class Animal

ob.fun();

//with Parameter, overloaded function in class Aquatic

ob.fun(10);

}

}

Page 22: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overriding

Page 23: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overriding

• The implementation in the subclass overrides (replaces) the

implementation in the superclass

• It is done by providing a method that has same

1. name, same

2. parameters, and same

3. return type as the method in the parent class

Page 24: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overriding

package com.edureka.animal;

public class Animal {

int fun(int a, int b){

int c = a + b;

return c;

}

}

package com.edureka.animal;

public class Aquatic extends Animal{

int fun(int a, int b){

System.out.println “u y super lass: " + super.fun(a, b));

int c = a * b;

return c;

}

}

Page 25: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Method Overriding

package com.edureka.animal;

public class Main {

public static void main(String[] args) {

Aquatic ob = new Aquatic();

System.out.println Produ t y derived lass : + o .fu , ;

}

}

Page 26: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Abstract Class

Page 27: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Abstract Class

abstract class Shape

class Rectangle extends Shape

class Circle

extends

Shape class Triangle

extends

Shape

Page 28: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

What is an Abstract Class?

• A class that is declared abstract

» Ex - abstract class Demo

» It may or may not use abstract

methods

Page 29: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

What is an Abstract Method ?

Java Programming: OOP

• A method that is declared abstract

• A method that is declared without an

implementation

– Ex - abstract void add(int x, int y);

Page 30: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Example

public class Circle extends Shape {

@Override

void Area() {

Double area = 3.14 * radius*radius;

}

}

public abstract class Shape {

//Abstract method i.e no implementation

abstract void Area();

}

public class Rectangle extends Shape {

@Override

void Area() {

Double area = length * width;

}

}

Page 31: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Interface

Page 32: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Interface

• Interfaces are declared using

the interface keyword

• Interface can contain : » method signature

(no implementation)

» constant declarations

(variable declarations that are declared to be both static and final)

Page 33: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Interface

• A class that implements an interface must implement

all of the methods described in the interface

Interface Class implements

abstract Method1();

abstract Method2();

abstract Method3();

abstract Method1();

abstract Method2();

abstract Method3();

Page 34: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

How to create an Interface

public interface Demo_interface {

int add(int value1, int value2);

void print(int sum);

}

Demo_interface

int add(int value1, intvalue2);

void print(int sum);

Page 35: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

How to implement the Interface

public class Class1 implements Demo_interface {

@Override

public int add(int value1, int value2) {

int sum = value1 + value2;

return sum;

}

@Override

public void print(int sum) {

System.out.println("The sum is : " + sum);

}

}

Class1

public int add(int value1, int value2);

public void print(int sum);

Demo_interface

int add(int value1, intvalue2);

void print(int sum);

Page 36: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Using it in the main class

public class Execute {

public static void main(String[] args) {

Class1 ob = new Class1();

int sum = ob.add(10, 10);

ob.print(sum);

}

}

Execute

ob

sum = ob.add(10,10);

ob.print(sum);

Page 37: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Input And Output

Page 38: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Input and Output

• Input is the data given by the user to the program.

• Output is the data what we receive from the program in the

form of result.

• Stream represents flow of data i.e. sequence of data.

• To give input we use InputStream and to receive output we use

OutputStream.

Page 39: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

How input is read from Keyboard?

System.in InputStream

Reader BufferedReader

It represents keyboard. To read data from keyboard it should be connected to InputStreamReader

It reads data from keyboard and send that data to BufferedReader.

It reads data from InputStreamReader and stores data in buffer. It has got methods so that data can be easily accessed.

connected to

send data to

Page 40: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Reading Input from console

• Input can be given either from file or keyboard.

• Input can be read from console in 3 ways. BufferedReader

StringTokenizer

Scanner

Page 41: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

BufferedReader

BufferedReader bufferedreader = new

BufferedReader(new InputStreamReader(System.in));

int age = bufferedreader.read();

String name = bufferedreader.readLine(); Methods

int read()

String readLine()

Page 42: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

StringTokenizer

• It can be used to accept multiple inputs from

console in single line where as

BufferedReader accepts only one input from a

line.

• It uses delimiter(space, comma) to make the

input into tokens.

Page 43: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

StringTokenizer

BufferedReader bufferedreader = new BufferedReader(new

InputStreamReader(System.in));

String input = bufferedreader.readLine();

StringTokenizer tokenizer = new StringTokenizer(input, , );

String name = tokenizer.nextToken();

int age=Integer.parseInt(tokenizer.nextToken());

delimiter

Page 44: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Scanner

• It accepts multiple inputs from file or keyboard and divides

into tokens.

• It has methods to different types of input( int, float, string,

long, double, byte) where tokenizer does not have.

Scanner scanner = new Scanner(System.in);

int rollno = scanner.nextInt();

String name = scanner.next();

Page 45: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Writing output to console

• The output can be written to console in 2 ways:

print(String)-

System.out.print( hello );

write(int)-

int input= i ;

System.out.write(input);

System.out.write( / );

Page 46: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

•Q& A..?

Page 47: Java Constructors - Instructor-Led Online Training with … Constructors Constructor A constructor : is used in the creation of an object. is a special method with no return type.

Thanks..!