Class Java 10
Class Java 10
aggregation
There are two ways to reuse ( ) שימוש חוזרthe existing classes, namely
aggregation ( )הכלהand inheritance ()ירושה.
1
Inheritance ()ירושה
In OOP we often organize classes in hierarchy ( )היררכיהto avoid
duplication and reduce redundancy.
The classes in the lower hierarchy inherit ( )ירשall the variables
(attributes) and methods from the higher hierarchies.
Redundancy can be greatly reduced or eliminated as these common
variables and methods do not need to be repeated in all the
subclasses.
Person
2
Inheritance - example
public class Person
{
...
} //person
Student
Superclass - a class in
the upper hierarchy
Mstudent
Subclass - a class in
the lower hierarchy
8
Mstudent - test
public static void main(String[ ] args) {
System.out.print( “Enter student's id: “ );
int id=reader.nextInt();
System.out.print( “Enter student's name: ”);
String name=reader.next();
System.out.print( “Enter the number of courses” );
int num =reader.nextInt();
System.out.print( “Enter student's stipend: “ ); Creation subclass
int stipend=reader.nextInt();
Mstudent s1 = new Mstudent(id, name, num, stipend);
System.out.println( “Tuition= "+ s1.calcTuition());
System.out.print( “Enter new student's stipend: “ );
stipend=reader.nextInt();
Subclass method Overriding
s1.setStipend(stipend);
System.out.println( “New tuition= "+ s1.calcTuition()); }
Mstudent - test,cont.
5 x 1000 - 1000
Tuition= 4000
Enter new student's stipend: 2000 5 x 1000 - 2000
New tuition= 3000
10
Overriding ()דריסת שיטת אב
• Method overriding is similar to method
overloading()העמסה, with a small difference. In
overriding, a method in a parent class is overridden in
the child class.
• The method in the child class will have the same
signature as that of the parent class. Since the method
in the child class has the same signature & name as the
method of its parent class, it is termed as overriding.
• When an overridden method is called from within a
subclass, it will always refer to the version of that method
defined by the subclass. The version of the method
defined by the superclass will be hidden.
11
Overriding - example
public class A {
int a, b;
public A(int a, int b) { // A class constructor
this.a = a;
this.b = b;
}
public void show() // display a and b
{
System.out.println( “ a and b: " + this.a + “ " +this.b);
}
} //class A
Method overriding occurs only
public class B extends A {
when the names and the type
int c;
public B(int a, int b, int c) { // B class constructor signatures of the two methods
super(a, b); are identical.
this.c = c;
}
public void show() // display c – this overrides show() method in A
{
System.out.println(“c: " + this.c);
}
} //class B
12
Overriding - testing
public static void main(String args[ ])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
The output produced by this program is shown here:
c: 3
13
Overriding (using the super)
public class B extends A {
int c;
public B(int a, int b, int c) { //constructor
super(a, b);
this.c = c; }
public void show() {
super.show(); // this calls A's show()
System.out.println(“c: " +this.c); public static void main(String args[ ])
} {
} B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in A
}
a and b: 1 2
c: 3
14
Using the super – class Box
public class Box {
private double width;
private double height;
private double depth;
Box(Box ob) {
this.width = ob. width;
Box copy constructor 1
this.height = ob.height;
this.depth = ob.depth; }
Box(double w, double h, double d) {
this.width = w;
this.height = h; Box constructor 2
this.depth = d; }
Box () {
this.width = -1;
this.height = -1; Box default constructor 3
this.depth = -1; }
Box(double len) {
this.width = len; Cube constructor 4
this.height = len;
this.depth = len; }
double volume () {
Box volume calculating
return this.width * this.height * this.depth; }
15
} // class Box
Using the super – class BoxWeight
class BoxWeight extends Box {
double weight; // box weight
// copy constructor 1
BoxWeight(BoxWeight ob) {
super(ob); // call superclass copy constructor
this.weight = ob.weight; }
// constructor 2
BoxWeight(double w, double h, double d, double m) {
super(w, h, d) ; // call superclass constructor
this.weight = m; }
// default constructor 3
BoxWeight() {
super (); // call superclass default constructor
this.weight = -1; }
// cube constructor 4
BoxWeight(double len, double m) {
super(len); // call superclass cube constructor
this.weight = m; }
} // class BoxWeight 16
Using the super – DemoSuperTest
public 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 ();
BoxWeight mycube = new BoxWeight (3, 2);
BoxWeight myclone = new BoxWeight(mybox1);
double vol= mybox1.volume ();
System.out.println("volume myboxl = " + vol) ;
System.out.println("weight myboxl = " + mybox1.weight);
vol = mybox2.volume();
System.out.println("volyme mybox2 = " + vol);
System.out.println("weight of mybox2 = " + mybox2.weight);
vol = mybox3.volume();
System.out.println("volume myboxЗ = " + vol) ;
System.out.println("weight myboxЗ = " + mybox3.weight);
vol = myclone.volume();
System.out.println("volume myclone = " + vol);
System.out.println("weight myclone = " + myclone.weight);
vol = mycube.volume ();
System.out.println("volume mycube = " + vol);
System.out.println("weight mycube = " + mycube.weight);
} // main
17
} // class DemoSuperTest
DemoSuperTest - output
volume mybox1 = 3000.0
weight mybox1 = 34.3
volume mybox2 = 24.0
weight mybox2 = 0.076
volume myboxЗ = -1.0
weight myboxЗ = -1.0
volume myclone = 3000.0
weight myclone = 34.3
volume mycube = 27.0
wight mycube = 2.0
18
Overriding opposite overloading
public class A {
int a, b;
public A(int a, int b) { // A class constructor
this.a = a;
this.b = b; }
public void show() // display a and b
{
System.out.println( “ a and b: " + this.a + “ " +this.b);
}
} // class A
public class B extends A {
int c;
public B(int a, int b, int c) { // B class constructor
super(a, b);
this.c = c;
}
public void show(String msg) // overload show() from superclass
{
System.out.println( msg + this.c);
}
} // class B 19
Overriding oppozite overloading
public static void main(String args[ ]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
20
Polymorphism in Java
Object Oriented Programming (OOP) is actually classified by three
main principles:
1) Encapsulation
2) Inheritance
3) Polymorphism
22
Polymorphism – what it is not
Jump
23
Compile – time polymorphism
This form of polymorphism is called compile-time polymorphism because the
compiler knows after the compile to the byte code which of the add methods it will
execute.
25
Run-time polymorphism - example
public class Person {
public void sayHi)( {
System.out.println("Hello there!");
}
} //class Person
27
Polymorphism – example2
Shape
color
Superclass - a class in
getArea() the upper hierarchy
toString()
29
Class Rectangle
public class Rectangle extends Shape
{
private int length;
private int widht;
public Rectangle(String color, int length, int width)
{
super(color);
this.length=length;
this.width=width;
} // constructor
public double getArea()
{
return this.length * this.width;
}
public String toString()
{
return ( “length= ” + this.length + “width= “+ this.width );
}
} // class Rectangle
30
Class Triangle
public class Triangle extends Shape
{
private int base;
private int height;
public Triangle(String color, int base, int height)
{
super(color);
this.base=base;
this.height=height;
} // constructor
public double getArea() {
return 0.5*this.base * this.height;
}
public String toString()
{
return ( “base= ” + this.base + “height= “+ this.height );
}
} // class Triangle
31
Class Shape implementation -1
public class TestShape1
{
public static void main(String [ ] args) In this program all references are
{ from the superclass Shape.
Shape s1 = new Rectangle(“red”, 4, 5);
System.out.println(s1);
System.out.println(“ Area is “+ s1.getArea());
Shape s2 = new Triangle(“green”, 3, 6);
System.out.println(s1);
System.out.println(“ Area is “+ s2.getArea());
} // main This will produce:
} // class TestShape1
length= 4 width= 5
Area is 20.0
length= 4 width= 5
Area is 9.0 32
Class Shape implementation - 2
public class TestShape2
{
public static void main(String [] args)
{ Shape [ ] arr = new Shape[2];
arr[0] = new Rectangle(“red”, 4, 5);
arr[1] = new Triangle(“green”, 3, 6);
for( int i=0; i<2; i++)
System.out.println(“ Area is “+ arr[ i ]. getArea());
} // main
} // class TestShape2
This will produce:
Area is 20.0
Area is 9.0
Class Shape extending
We can extend our program easily by adding in more subclasses as
Circle, Square, etc. with ease.
Shape
color
getArea()
toString()
35