object oriented programming using java (continued) · inheritance in java •the process of...

19
Object Oriented Programming using Java (continued)

Upload: others

Post on 09-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Object Oriented Programming

using Java

(continued)

Page 2: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Static Block in Java

• Static block is a set of statements, which will beexecuted by the JVM before execution of main method.

• In real time application static block can be usedwhenever we want to execute any instructions orstatements before execution of main method.

class StaticDemo {

static

{

System.out.println("Hello how are u ?");

}

public static void main(String args[]) {

System.out.println("This is main()");

} }

Page 3: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Constructor in Java• The purpose of constructor is to initialize an object called object

initialization. Initialization is a process of assigning user definedvalues and eliminate default value.

• Constructor has the same name as the class name.

• Constructor cannot return values.

• A class can have more than one constructor as long as they have different signature (i.e., different input arguments syntax).

• There is NO explicit invocation statement needed:

When the object creation statement is

executed, the constructor method will

be executed automatically.

class Sum {

static int a,b;

Sum()

{

a=10; b=20;

}

public static void main(String s[]) {

Sum s1=new Sum();

int c=a+b;

System.out.println("Sum: "+c); } }

Page 4: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Difference between Method and

Constructor

Page 5: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Types of constructors

Default ConstructorParameterized Constructor

Page 6: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

class Student{

int id, age;

String name;

Student(int i,String n)

{

id = i;

name = n;

}

Student(int i,String n,int a)

{

id = i;

name = n;

age=a;

}

void display(){

System.out.println(id+" "+name+" "+age);

}

public static void main(String args[]){

Student s1 = new Student(111,“Raman");

Student s2 = new Student(222,”Mohan",25);

s1.display();

s2.display();

} }

Output

111 Raman 0

222 Mohan 25

Constructor overloading is

a technique in Java in which

a class can have any

number of constructors that

differ in parameter lists. The

compiler differentiates these

constructors by taking the

number of parameters, and

their type.

Constructor Overloading

Page 7: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Types of polymorphism in java

• There are two types of polymorphism in java▫ Runtime polymorphism( Dynamic

polymorphism)

▫ Compile time polymorphism (Static polymorphism).

• Method overriding is a perfect exampleof runtime polymorphism.

• Method overloading is a perfect exampleof compile time polymorphism.

• Operator Overloading (Supported in C++, but not inJava)

Page 8: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Method Overloading in Java• Method Overloading is a feature that allows a class to have two or

more methods having same name, if their argument lists aredifferent. It is also known as Static Polymorphism.

• Argument lists could differ in –1. Number of parameters.

2. Data type of parameters.

3. Sequence of Data type of parameters.

Page 9: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Different Number of

parameters in argument listDifference in data type of arguments

Page 10: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

10

The this keyword

• this keyword can be used to refer to the object itself.It is generally used for accessing class members (fromits own methods) when they have the same name asthose passed as arguments.

public class Circle {public double x,y,r;// Constructor

public Circle (double x, double y, double r) {this.x = x;this.y = y;this.r = r;

}//Methods to return circumference and area

}

Page 11: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Inheritance in Java

• The process of obtaining the data members andmethods from one class to another class is knownas inheritance. It is one of the fundamentalfeatures of object-oriented programming.

• It is a mechanism of deriving a new class fromexisting/old class.

• The old class is known as “base” class, “super” classor “parent” class”; and the new class is known as“sub” class, “derived” class, or “child” class.

• The concept of inheritance is also known as re-usability or extendable classes or sub classing orderivation.

Page 12: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Real time Examples

Parent

Child

Inherited

capability

Page 13: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

13

Inheritance: Introduction

• The inheritance allows subclasses to inherit allproperties (variables and methods) of theirparent classes. The different forms ofinheritance are:▫ Single inheritance (only one super class)▫ Multiple inheritance (several super classes)▫ Multi-Level inheritance (derived from a derived class)▫ Hierarchical inheritance (one super class, many sub

classes)▫ Hybrid inheritance (more than two types)▫ Multi-path inheritance (inheritance of some properties

from two sources).

Page 14: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

14Forms of Inheritance

A

B

(a) Single Inheritance

A

C

(b) Multiple Inheritance

B A

C

(c) Hierarchical Inheritance

B D

A

C

(a) Multi-Level Inheritance

BB

D

(b) Hybrid Inheritance

C

A

B

D

(b) Multipath Inheritance

C

A

Page 15: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Advantage of inheritance

• Application development time is less.

• Application take less memory.

• Application execution time is less.

• Application performance is enhance (improved).

• Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost.

Syntax for Inheritance

class Subclass-Name extends Superclass-Name { //methods and fields }

public class ChildClass extends BaseClass { // derived class methods extend and possibly override }

Page 16: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Single Inheritance

Page 17: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Multilevel inheritance

class Faculty

{ float total_sal=0, salary=30000; }

class HRA extends Faculty

{ float hra=3000; }

class DA extends HRA

{ float da=2000; }

class Science extends DA

{

float bonous=2000;

public static void main(String args[]) {

Science obj=new Science();

obj.total_sal=obj.salary+obj.hra+obj.da+obj.bonous;

System.out.println("Total Salary is:"+obj.total_sal);

}

}

Page 18: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Multiple inheritance

• The concept of multiple inheritance is notsupported in java through concept of classes butit can be supported through the concept ofinterface.

Page 19: Object Oriented Programming using Java (continued) · Inheritance in Java •The process of obtaining the data members and methods from one class to another class is known as inheritance

Exercises1. Design and write a class to represent a bank account

2. Write a program to find area and perimeter of a rectangle using constructor

overloading

3. Write a Java program to calculate the area of square, triangle, circle using method overloading.

4. Write a Program to find maximum of two numbers or strings using method overloading (Hint: Use compareTo())

5. Implement the following using inheritance

a. Data members· Owner name· Account number· Balance amount in the account

b. Methods· To assign initial values (use constructors)· To deposit an amount· To withdraw an amount after checking balance· To display the owner name and balance