• No results found

Introduction to Class, objects and methods

N/A
N/A
Protected

Academic year: 2020

Share "Introduction to Class, objects and methods"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Traditional Programming Model

Main Program

Function 3 Function 2

Function 1

(3)

Object Oriented Programming

Data

Function

Data

Function

Data

Function

Object A Object B

(4)

Class

Class declaration

class emp

{

----}

Member variable declaration

1.

Public – any class in any package can access the variable or function

Public void test(){}

2. Protected – Any class in the same package as the class in which it is

declared can access protected members.

3. Private – can only be accessed within the declaring class. Only

members of the same class can access them.

4. Static – when we want a method to be a class method. Static variable

(5)

Class

Member method declaration

5. Final- these variables or methods cannot be overloaded or changed.

Example –

class employee {

void salary(int b) {

double hra=(double)b *0.2; double da=(double)b * 1.0; double net=(double) b+hra+da;

System.out.println("Net salary ="+net); }

public static void main(String a[]) {

employee e1=new employee(); e1.salary(1000);

(6)

Class

Member method declaration

5. Final- these variables or methods cannot be overloaded or changed.

Example –

class employee {

void salary(int b) {

double hra=(double)b *0.2; double da=(double)b * 1.0; double net=(double) b+hra+da;

System.out.println("Net salary ="+net); }

}

class useemp {

public static void main(String a[]) {

employee e1=new employee(); e1.salary(1000);

} }

(7)

Constructors

• Constructors are special methods provided in each java class. • These methods are used to initializing new objects.

Example –

Employee e1=new employee();

Declare constructor

class employee {

double basic;

employee(double b) // constructor {

basic=b; }

employee() {

basic=1000; }

void salary(int b) // Constructor

public static void main(String a[]) {

employee e1=new employee(); employee e2=new employee(2000); }

(8)

Problems

1. Write a program to design class stock with

itemno and qty in stock. Declare method

(9)

Inheritance

Inheritance in java is a mechanism in which one object acquires all

the properties and behaviors of parent object.

Types of Inheritance in

Java-1.

Single

2.

Multi level

(10)

Inheritance

Syntax of Java

Inheritance-class SubInheritance-class-name extends SuperInheritance-class-name {

//methods and fields }

class Employee {

float salary=40000; }

class Programmer extends Employee {

int bonus=10000;

public static void main(String args[]){ Programmer p=new Programmer();

System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); }

(11)

Single Inheritance

One parent class and one child class

class Animal {

void eat() {

System.out.println("eating..."); }

}

class Dog extends Animal {

void bark() {

System.out.println("barking..."); }

}

class TestInheritance {

public static void main(String args[]){ Dog d=new Dog();

(12)

Multilevel Inheritance

Ladder type inheritance.

class Animal {

void eat(){System.out.println("eating...");} }

class Dog extends Animal {

void bark() {

System.out.println("barking..."); } }

class BabyDog extends Dog {

void weep() {

System.out.println("weeping..."); } }

class TestInheritance2 {

public static void main(String args[]) {

BabyDog d=new BabyDog(); d.weep();

(13)

Hierarchical Inheritance

class Animal { void eat() {System.out.println("eating...");} }

class Dog extends Animal { void bark() { System.out.println("barking..."); } }

class Cat extends Animal {

void meow(){System.out.println("meowing...");} }

class TestInheritance3 {

public static void main(String args[]) {

Cat c=new Cat(); c.meow();

c.eat();

//c.bark();//C.T.Error }

(14)

Overriding

1.

Overriding is a feature that allows a subclass or child class to provide a

specific implementation of a method that is already provided by one of its

super-classes or parent classes.

(15)

Overriding

class Parent {

void show() {

System.out.println("Parent's show()"); }

}

// Inherited class

class Child extends Parent {

// This method overrides show() of Parent void show()

{

System.out.println("Child's show()"); }

(16)

Overriding

class inherit {

public static void main(String[] args) {

// If a Parent type reference refers // to a Parent object, then Parent's // show is called

Parent obj1 = new Parent();

obj1.show();

// If a Parent type reference refers, to a Child object Child's show() // is called. This is called RUN TIME

// POLYMORPHISM.

Parent obj2 = new Child();

obj2.show(); // Parent method is overridden

} }

(17)

Super Keyword

1. super can be used to refer immediate parent class instance

variable.

2. super can be used to invoke immediate parent class method.

(18)

Super Keyword

1.

super can be used to refer immediate parent class instance variable

We can use super keyword to access the data member or field of parent class. It is

used if parent class and child class have same fields.

class Animal{

String color="white"; }

class Dog extends Animal{ String color="black";

void printColor(){

System.out.println(color);//prints color of Dog class

System.out.println(super.color);//prints color of Animal class }

}

class TestSuper1{

public static void main(String args[]){ Dog d=new Dog();

(19)

Super Keyword

2. super can be used to invoke immediate parent class method.

The super keyword can also be used to invoke parent class method. It should be

used if subclass contains the same method as parent class. In other words, it is used

if method is overridden.

class Animal{

void eat(){System.out.println("eating...");} }

class Dog extends Animal{

void eat(){System.out.println("eating bread...");} void bark(){System.out.println("barking...");} void work(){

super.eat(); bark(); }

}

class TestSuper2{

public static void main(String args[]){ Dog d=new Dog();

(20)

Super Keyword

3. super() can be used to invoke immediate parent class constructor.

The super keyword can also be used to invoke the parent class constructor.

class Animal{

Animal(){System.out.println("animal is created");} }

class Dog extends Animal{ Dog(){

super();

System.out.println("dog is created"); }

}

class TestSuper3 {

public static void main(String args[]){ Dog d=new Dog();

(21)

Super Keyword

class Person{ int id;

String name;

Person(int id,String name){ this.id=id;

this.name=name; }

}

class Emp extends Person{ float salary;

Emp(int id,String name,float salary){

super(id,name);//reusing parent constructor this.salary=salary;

}

void display(){System.out.println(id+" "+name+" "+salary);} }

class TestSuper5{

public static void main(String[] args){ Emp e1=new Emp(1,"ankit",45000f); e1.display();

(22)

Final class or Method

1. Final methods can not be overridden in child class

class Parent {

// Can't be overridden final void show() { } }

class Child extends Parent {

// This would produce error void show() { }

(23)

Final class or Method

2. Static methods can not be overridden(Method Overriding vs Method Hiding) :

class Parent {

// Static method in base class which will be hidden in subclass

static void m1() { System.out.println("From parent static m1()");}

// Non-static method which will be overridden in derived class

void m2() { System.out.println("From parent non-static(instance) m2()"); } }

class Child extends Parent {

// This method hides m1() in Parent

static void m1() { System.out.println("From child static m1()");}

// This method overrides m2() in Parent

@Override

public void m2() { System.out.println("From child non-static(instance) m2()");}

(24)

Final class or Method

class Main {

public static void main(String[] args) {

Parent obj1 = new Child();

// As per overriding rules this should call to class Child static

// overridden method. Since static method can not be overridden, it // calls Parent's m1()

obj1.m1();

// Here overriding works and Child's m2() is called obj1.m2();

} }

Output

From parent static m1()

(25)

Final class or Method

3. Private methods can not be overridden

(26)

Overriding and Overloading

Sno Overriding methods methods

1 This is done in inheritance only. One method is in parent class and other is in child class

This can be done in single class.

2 Return type , name and parameters of the function should be same in both class

Parameters should be different in the methods

3 If the method is final in parent class it cannot be overridden in child class

(27)

Overriding and Overloading

Sno Overriding methods Overloading methods

4 Example-class A {

void show() {

System.out.println(“I am parent”); }

}

class B extends A {

void show() // override A method {

System.out.println(“I am parent”); }

}

class use_B {

public static void main(String ar[]) {

use_B b1=new use_B();

b1.show(); // B method is used }}

class A {

void show() {

System.out.println(“I am show”); }

void show(int a) { System.out.println(“A=”+a); } } class use_A {

public static void main(String ar[]) {

use_A b1=new use_A(); b1.show();

References

Related documents

When Hashem told Moshe that He will destroy the Jewish people and only save him and make a nation of him (exactly like He did to Noach) Moshe said “macheni” which means erase my

The others (e.g. Playing Videos, adding the shutdown button) are not crucial to the camera project but can be done if you’re also interested in exploring these capabilities.

Version Originale – 3 Livre de l’eleve (Unités 1-5) Monique Denyer, Christian Ollivier, Emilie Perrichon, Editions Maisons des Langues, Paris, 2011.. Version Originale –3

Mercer county records search new jersey police arrest order for law for individuals in camden county nj public records in court and available.. Looking for sure your favorite

Checks in the Ultimate Limit States Design Combinations Stress-Strain-Curves Design Internal Forces Design for Bending with or without Longitudinal Force or Longitudinal Force

 monitor key privacy, financial, and security controls  test compliance with regulatory requirements  and identify and mitigate other business risks. before they have a

Eintragung: 18/10/2001 Nizzaer Klassifikation: 35, 36, 37, 38, 39, 40, 41, 42 Verfahrensstand: Eingetragen Veröffentlichung der Eintragung Name des Inhabers: WWF-World Wide Fund

You will fly 33 hours dual with an instructor, including a minimum 3 hours of cross country flying (navigation) and 5 hours of instrumental flight training.. You will also