A Critique of the Proposed National Education Policy Reform
Inheritance.pptx
1. Static keyword
The static variable can be used to refer the
common property of all objects.
The static variable gets memory only once in
class area at the time of class loading.
2. //Program for static keyword
class Exstatic
{
static void evenodd(int num)
{
if(num%2==0)
System.out.println("The given number is even");
else
System.out.println("The given number is odd");
}
class Example
{
public static void main(String args[])
{
Exstatic.evenodd(14);
}
}
3. Inheritance
Inheritance in java is a mechanism in which
one object acquires all the properties and
behaviors of parent object.
Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
5. //Single Inheritance
class Single
{
static int num1=10;
static int num2=5;
}
class Inherit extends Single
{
public static void main(String[] args)
{
int num3=2;
int result=num1+num2+num3;
System.out.println("Result of child class is "+result);
}
}
6. //Multilevel Inheritance
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
} }
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
} }
7. Method Overriding
If subclass (child class) has the same method as
declared in the parent class, it is known as
Method Overriding.
8. //Example of Method Overriding
class Vehicle
{
void run()
{System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
obj.run();
}
9. Dynamic Method Dispatch
Dynamic method dispatch is a mechanism by
which a call to an overridden method is
resolved at runtime.
This is how java implements runtime
polymorphism.
10. Example for Dynamic Method Dispatch
class Game
{
public void type()
{ System.out.println("Indoor & outdoor"); }
}
Class Cricket extends Game
{
public void type()
{ System.out.println("outdoor game"); }
public static void main(String[] args)
{
Game gm = new Game();
Cricket ck = new Cricket();
gm.type();
ck.type();
gm=ck; //gm refers to Cricket object
gm.type(); //calls Cricket's version of type
} }
11. abstract keyword
Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
Syntax:
abstract class classname
{
………
abstract void method();
………}
12. abstract class A
{
abstract void display();
public void method1()
{
System.out.println(“This is concrete method");
}
}
class B extends A
{
void display()
{
System.out.println(“This is abstract");
}
public static void main(String[] args)
{
B b = new B();
b.display();
b.method1();
}
}
13. Example Program
Create an abstract class person. Derive two
classes Employee and Worker from it. Use
proper method to accept and display. The fields
of Employee are Emp_no, Emp_name and
address. Fields for worker are name and working
hours.
14. abstract class Person
{
public abstract void display();
}
class Employee extends Person
{
int Emp_no;
String Address;
String name;
public Employee(int Emp_no,String name,String Address)
{
this.Emp_no = Emp_no;
this.name=name;
this.Address=Address;
}
public void display()
{
System.out.println("Employee Details");
System.out.println("Employee Number"+Emp_no);
System.out.println("Employee Name"+name);
System.out.println("Employee Address"+Address);
} }
15. class Worker extends Person
{
int hours;
String name;
public Worker(String name,int hours)
{
this.name=name;
this.hours=hours;
}
public void display()
{ System.out.println("Worker Details");
System.out.println("Worker Name" +name);
System.out.println("Worker Hours" +hours);
} }
public class EmployeeDetails
{
public static void main(String args[])
{ Employee e=neww Employee(1,"Alok","Bangalore");
e.display();
Worker w=new Worker("Neelima",2);
} }
16. abstract class Shape
{ abstract void draw(); }
}
class Rectangle extends Shape{
void draw(){ System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){ System.out.println("drawing circle");}
}
class Test{
public static void main(String args[]){
Rectangle r=new Rectangle();
Circle1 c= new Circle1();
r.draw();
c.draw();
} }
17. final class
A class declared as final cannot be inherited.
final class A
{…..}
class B extends A
{
……..}
Error – final classes cannot be inherited
19. //Java Program to create an Interface and implement it in class.
import java.io.*;
import java.util.Scanner;
interface Shape
{
final static double pi=3.142;
public void area(float r);
}
class Circle implements Shape
{
float rad;
float area_circle;
public void area(float r)
{
rad=r;
area_circle=(float)pi*rad*rad;
System.out.println("Area is "+area_circle);
}
20. public static void main(String args[])
throws IOException
{
Circle c=new Circle();
float radius;
Scanner s=new Scanner(System.in);
System.out.println("Enter the radius");
radius=s.nextInt();
c.area(radius);
}
}
21. Multiple Inheritance
class Student
{
int roll_no;
void getNumber(int n)
{
roll_no=n;
}
void putNumber()
{
System.out.println("RollNo "+roll_no);
}
class Test extends Student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
25. interface I1
{
public void f1();
}
interface I2 extends I1
{
public void f2();
}
class x implements I2
{
public void f1()
{
System.out.println("Contents of Method f1() in Interface1");
}
public void f2()
{
System.out.println("Contents of Method f2() in Interface2");
}
26. public void f3()
{
System.out.println("Contents of Method f3() of Class X");
}
}
class ExtendingInterface
{
public static void main(String[] args)
{
Interface2 v2; //Reference variable of Interface2
v2 = new x(); //assign object of class x
v2.f1();
v2.f2();
x xl=new x();
xl.f3();
}
}