Classes Inheritance Interface Abstract.pptx
Classes Inheritance Interface Abstract.pptx
1
Contents
2
Introduction
• Java is a true Object Oriented language and therefore
the underlying structure of all Java programs is
classes.
• Anything we wish to represent in Java must be
encapsulated in a class that defines the “state” and
“behavior” of the basic program components known
as objects.
• Classes create objects and objects use methods to
communicate between them.
3
Classes
• A class is a user defined abstract datatype.
class Box { mybox
double width; Widwidth=
10
,length=15,
double length;
Depth=12
double depth;
} to create the object syntax
Box mybox=new Box( );
4
Program to demonstrate working of a
class
5
class Box {
double width; //declared variables
double length;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[ ]) {
Box mybox = new Box(); //an object mybox of the class box is created,new is
the keyword thru which it is created.
Box mybox1=new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.length = 20;
mybox.depth = 15;
mybox1.width = 20;
mybox1.length =30;
mybox1.depth = 45;
}
class BoxDemo5 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
double vol;
17
Use this to resolve naming collisions.
this.length = length;
this.depth = depth;
}
18
class Stack {
int stck[] = new int[10];
int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
20
Summary
22
Program to add two distances
23
class Distance
{
int feet, inches;
Distance()
{
feet=inches=0;
}
Distance(int x)
{
feet=inches=x;
}
Distance(int x1, int x2)
{
feet=x1;
inches=x2;
}
26
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
27
Inheritance
28
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
} 29
}
class SimpleInheritance {
public static void main(String args[]) {
A a = new A();
B b = new B();
a.i = 10;
a.j = 20;
System.out.println("Contents of a: ");
a.showij();
System.out.println();
b.i = 7;
b.j = 8;
b.k = 9;
System.out.println("Contents of b: ");
b.showij();
b.showk();
System.out.println();
Box() {
class BoxWeight extends Box
{
double weight;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
}
}
33
Use of super
34
class Box {
private double width;
private double length;
private double depth;
// default constructor
BoxWeight() {
super(); 36
weight = -1;
}
class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
BoxWeight mybox3 = new BoxWeight(); // default
BoxWeight mycube = new BoxWeight(3, 2);
BoxWeight myclone = new BoxWeight(mybox1);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol = mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
37
System.out.println("Weight of mybox3 is " + mybox3.weight);
System.out.println();
Order of constructor calling in Inheritance
38
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
class CallingCons { 39
40
Overridding Methods
41
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
43
Use of “abstract”
44
Abstract Classes
• Class containing one or more function as abstract is
known as abstract class and therefore its declaration
should be preceded with the keyword abstract.
• An Abstract class is a conceptual class.
• An Abstract class cannot be instantiated – objects cannot
be created.
• A class declared abstract, even with no abstract methods
can not be instantiated.
• A subclass of an abstract class can be instantiated if it
overrides all abstract methods by implementation them.
• A subclass that does not implement all of the superclass
abstract methods is itself abstract; and it cannot be
45
instantiated.
Abstract Class Syntax
abstract class ClassName
{
...
…
abstract Type MethodName1();
…
…
Type Method2()
{
// method body
}
}
• When a class contains one or more abstract methods, it should be declared as abstract class.
• The abstract methods of an abstract class must be defined in its subclass.
• We cannot declare abstract constructors or abstract static methods.
46
For eg:
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
47
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
49
Final Members: A way for
Preventing Overriding of Members
in Subclasses
• All methods and variables can be overridden by
default in subclasses.
• This can be prevented by declaring them as final
using the keyword “final” as a modifier. For
example:
• final int marks = 100;
• final void display();
• This ensures that functionality defined in this
method cannot be altered any. Similarly, the value
of a final variable cannot be altered.
50
FINAL METHOD
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
FINAL CLASS
final class A {
// ...
}
52
Interfaces
interface
• Syntax InterfaceName
(appears like abstract{class):
// Constant/Final Variable Declaration
// Methods Declaration – only method body
}
• Example:
interface Speaker {
public void speak( );
}
56
Implementing Interfaces
57
class Politician implements Speaker {
Implementing Interfaces Example
public void speak(){
System.out.println(“Talk politics”);
}
}
}
Extending Interfaces
interface Shape
{
float PI=3.14f;
float computeArea();
}
}
Java 8 Default Methods
Interfaces in Java always contained method declaration not
their definitions (method body). There was no way of
defining method body / definition in interfaces. This is
because historically Java didn’t allow multiple inheritance of
classes. It allowed multiple inheritance of interfaces as
interface were nothing but method declaration. This solves
the problem of ambiguity in multiple inheritance. Since Java
8 has a new feature called Default Methods. It is now
possible to add method bodies into interfaces!
62
public interface Math
{
int add(int a, int b);
default int multiply(int a, int b)
{
return a * b;
}
}
In above Math interface we added a method
multiply with actual method body.
63
Why we need Default Methods?
or
Why would one want to add methods into
Interfaces?
We’ll it is because interfaces are too tightly coupled
with their implementation classes. i.e. it is not
possible to add a method in interface without
breaking the implementer class. Once you add a
method in interface, all its implemented classes must
declare method body of this new method.
64
What about Multiple Inheritance?
Adding method definitions in interfaces can add ambiguity in multiple inheritance. isn’t it?
Well, it does. However Java 8 handle this issue at Compile type. Consider below example:
interface Person
{
default void sayHello()
{
System.out.println("Hello");
}
}
interface Male
{
default void sayHello()
{
System.out.println("Hi");
}
}
class Sam implements Person, Male
{
65
}
In this example we have same defender method sayHello in
both interfaces Person and Male. Class Sam implements these
interfaces. So which version of sayHello will be inherited?
We’ll if you try to compile this code in Java 8, it will give
following error.