Traditional Programming Model
Main Program
Function 3 Function 2
Function 1
Object Oriented Programming
Data
Function
Data
Function
Data
Function
Object A Object B
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
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);
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);
} }
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); }
Problems
1. Write a program to design class stock with
itemno and qty in stock. Declare method
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
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); }
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();
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();
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 }
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.
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()"); }
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
} }
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.
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();
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();
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();
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();
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() { }
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()");}
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()
Final class or Method
3. Private methods can not be overridden
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
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();